mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-10 12:00:21 +08:00
Symmetric union operation
This commit is contained in:
+9
-15
@@ -161,32 +161,26 @@ class GeoSeries(Series):
|
||||
return Series([s.contains(other) for s in self],
|
||||
index=self.index)
|
||||
|
||||
# TODO: refactor to eliminate replications
|
||||
def difference(self, other):
|
||||
"""
|
||||
Return a GeoSeries of differences
|
||||
Operates on either a GeoSeries or a Shapely geometry
|
||||
"""
|
||||
if isinstance(other, GeoSeries):
|
||||
# TODO: align series
|
||||
return GeoSeries([s[0].difference(s[1]) for s in zip(self, other)],
|
||||
index=self.index)
|
||||
else:
|
||||
return GeoSeries([s.difference(other) for s in self],
|
||||
index=self.index)
|
||||
return self._geo_op(other, 'difference')
|
||||
|
||||
def symmetric_difference(self, other):
|
||||
"""
|
||||
Return a GeoSeries of differences
|
||||
Operates on either a GeoSeries or a Shapely geometry
|
||||
"""
|
||||
return self._geo_op(other, 'symmetric_difference')
|
||||
|
||||
def union(self, other):
|
||||
"""
|
||||
Return a GeoSeries of unions
|
||||
Operates on either a GeoSeries or a Shapely geometry
|
||||
"""
|
||||
if isinstance(other, GeoSeries):
|
||||
# TODO: align series
|
||||
return GeoSeries([s[0].union(s[1]) for s in zip(self, other)],
|
||||
index=self.index)
|
||||
else:
|
||||
return GeoSeries([s.union(other) for s in self],
|
||||
index=self.index)
|
||||
return self._geo_op(other, 'union')
|
||||
|
||||
def buffer(self, distance, resolution=16):
|
||||
return GeoSeries([geom.buffer(distance, resolution) for geom in self],
|
||||
|
||||
@@ -12,6 +12,8 @@ class TestSeries(unittest.TestCase):
|
||||
self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
|
||||
self.g1 = GeoSeries([self.t1, self.sq])
|
||||
self.g2 = GeoSeries([self.sq, self.t1])
|
||||
self.g3 = GeoSeries([self.t1, self.t2])
|
||||
self.g4 = GeoSeries([self.t2, self.t1])
|
||||
|
||||
def test_area(self):
|
||||
assert np.allclose(self.g1.area.values, np.array([0.5, 1.0]))
|
||||
@@ -43,6 +45,16 @@ class TestSeries(unittest.TestCase):
|
||||
assert u[0].is_empty
|
||||
assert u[1].equals(self.t2)
|
||||
|
||||
def test_symmetric_difference_series(self):
|
||||
u = self.g3.symmetric_difference(self.g4)
|
||||
assert u[0].equals(self.sq)
|
||||
assert u[1].equals(self.sq)
|
||||
|
||||
def test_symmetric_difference_poly(self):
|
||||
u = self.g3.symmetric_difference(self.t1)
|
||||
assert u[0].is_empty
|
||||
assert u[1].equals(self.sq)
|
||||
|
||||
def test_difference_poly(self):
|
||||
u = self.g1.difference(self.t2)
|
||||
assert u[0].equals(self.t1)
|
||||
|
||||
Reference in New Issue
Block a user