ENH: Implementation of relate method (#853)

This commit is contained in:
YuichiNotoya
2018-12-09 09:28:28 +01:00
committed by Joris Van den Bossche
parent 4f933d38f1
commit 8aae84e2c7
2 changed files with 43 additions and 3 deletions
+28 -3
View File
@@ -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):
"""
+15
View File
@@ -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)