Merge pull request #70 from jwass/singlegeomfix

BUG: Fix GeoSeries constructor for multi geoms
This commit is contained in:
Kelsey Jordahl
2013-11-13 05:21:43 -08:00
2 changed files with 37 additions and 1 deletions
+10
View File
@@ -27,13 +27,20 @@ def _is_empty(x):
except:
return False
def _convert_array_args(args):
if len(args) == 1 and isinstance(args[0], BaseGeometry):
args = ([args[0]],)
return args
class GeoSeries(Series):
"""A Series object designed to store shapely geometry objects."""
_metadata = ['name', 'crs']
def __new__(cls, *args, **kwargs):
if OLD_PANDAS:
args = _convert_array_args(args)
kwargs.pop('crs', None)
arr = Series.__new__(cls, *args, **kwargs)
if type(arr) is GeoSeries:
return arr
@@ -41,7 +48,10 @@ class GeoSeries(Series):
return arr.view(GeoSeries)
def __init__(self, *args, **kwargs):
if not OLD_PANDAS:
args = _convert_array_args(args)
crs = kwargs.pop('crs', None)
super(GeoSeries, self).__init__(*args, **kwargs)
self.crs = crs
+27 -1
View File
@@ -4,7 +4,8 @@ import tempfile
import numpy as np
from numpy.testing import assert_array_equal
from pandas import Series
from shapely.geometry import Polygon, Point, LineString
from shapely.geometry import (Polygon, Point, LineString,
MultiPoint, MultiLineString, MultiPolygon)
from shapely.geometry.base import BaseGeometry
from geopandas import GeoSeries
from .util import unittest, geom_equals, geom_almost_equals
@@ -39,6 +40,31 @@ class TestSeries(unittest.TestCase):
def tearDown(self):
shutil.rmtree(self.tempdir)
def test_single_geom_constructor(self):
p = Point(1,2)
line = LineString([(2, 3), (4, 5), (5, 6)])
poly = Polygon([(0, 0), (1, 0), (1, 1)],
[[(.1, .1), (.9, .1), (.9, .9)]])
mp = MultiPoint([(1, 2), (3, 4), (5, 6)])
mline = MultiLineString([[(1, 2), (3, 4), (5, 6)], [(7, 8), (9, 10)]])
poly2 = Polygon([(1, 1), (1, -1), (-1, -1), (-1, 1)],
[[(.5, .5), (.5, -.5), (-.5, -.5), (-.5, .5)]])
mpoly = MultiPolygon([poly, poly2])
geoms = [p, line, poly, mp, mline, mpoly]
index = ['a', 'b', 'c', 'd']
for g in geoms:
gs = GeoSeries(g)
self.assert_(len(gs) == 1)
self.assert_(gs.iloc[0] is g)
gs = GeoSeries(g, index=index)
self.assert_(len(gs) == len(index))
for x in gs:
self.assert_(x is g)
def test_area(self):
self.assertTrue(type(self.g1.area) is Series)
assert_array_equal(self.g1.area.values, np.array([0.5, 1.0]))