diff --git a/geopandas/base.py b/geopandas/base.py index d28cfea..a13eec9 100644 --- a/geopandas/base.py +++ b/geopandas/base.py @@ -53,8 +53,18 @@ def _binary_geo(op, left, right): def _binary_op(op, this, other, *args, **kwargs): # type: (str, GeoSeries, GeoSeries, args/kwargs) -> Series[bool] """Binary operation on GeoSeries objects that returns a Series""" - null_value = False if op not in ['distance', 'project'] else np.nan - dtype = 'bool' if op not in ['distance', 'project'] else float + if op in ['distance', 'project']: + null_value = np.nan + elif op == 'relate': + null_value = None + else: + null_value = False + if op in ['distance', 'project']: + dtype = float + elif op == 'relate': + dtype = object + else: + dtype = bool if isinstance(other, GeoPandasBase): @@ -564,7 +574,22 @@ class GeoPandasBase(object): index=self.index, crs=self.crs) def relate(self, other): - raise NotImplementedError + """ + Returns the DE-9IM intersection matrices for the geometries + + Parameters + ---------- + other : BaseGeometry or GeoSeries + The other geometry to computed + the DE-9IM intersection matrices from. + + Returns + ---------- + spatial_relations: Series of strings + The DE-9IM intersection matrices which describe + the spatial relations of the other geometry. + """ + return _binary_op('relate', self, other) def project(self, other, normalized=False): """ diff --git a/geopandas/tests/test_geom_methods.py b/geopandas/tests/test_geom_methods.py index 10677e3..63f986e 100644 --- a/geopandas/tests/test_geom_methods.py +++ b/geopandas/tests/test_geom_methods.py @@ -297,6 +297,21 @@ class TestGeomMethods: expected = [False, False, False, False, False, True] assert_array_dtype_equal(expected, self.g0.disjoint(self.t1)) + def test_relate(self): + expected = Series(['212101212', + '212101212', + '212FF1FF2', + '2FFF1FFF2', + 'FF2F112F2', + 'FF0FFF212'], + index=self.g0.index) + assert_array_dtype_equal(expected, self.g0.relate(self.inner_sq)) + + expected = Series(['FF0FFF212', + None], + index=self.g6.index) + assert_array_dtype_equal(expected, self.g6.relate(self.na_none)) + def test_distance(self): expected = Series(np.array([np.sqrt((5 - 1)**2 + (5 - 1)**2), np.nan]), self.na_none.index)