geopandas.sindex.SpatialIndex.nearest#
- SpatialIndex.nearest(geometry, return_all=True, max_distance=None, return_distance=False, exclusive=False)[来源]#
返回树中每个输入几何体的最近几何体在
geometry
。如果多个树形几何图形与输入几何图形的距离相同,默认情况下将返回多个结果。指定
return_all=False
仅获取一个最近的几何图形(返回的最近几何图形是非确定性的)。在空间连接的上下文中,输入几何体是“左”几何体,决定结果的顺序,而树几何体是“右”几何体,与左几何体进行连接。如果
max_distance
未设置,这将有效地成为一个左连接,因为geometry
中的每个几何体都将在树中找到一个最近的几何体。然而,如果使用max_distance
,这将变成一个内连接,因为geometry
中某些几何体可能在树中没有匹配。出于性能考虑,强烈建议您设置 the
max_distance
参数。- Parameters:
- geometry{shapely.geometry, GeoSeries, GeometryArray, numpy.array of Shapely geometries}
一个单一的shapely几何体,GeoPandas几何可迭代对象中的一个 (GeoSeries, GeometryArray),或者一个Shapely几何体的numpy数组,以便查询 空间索引。
- return_allbool, default True
如果存在多个等距或相交的最近几何体,则返回所有这些几何体,而不是单个最近几何体。
- max_distancefloat, optional
查询树中最近项目的最大距离。 必须大于0。 默认值为None,表示没有距离限制。
- return_distancebool, optional
如果为真,将返回距离以及索引。默认值为假
- exclusivebool, optional
如果为True,则不会返回与输入几何体相等的最近几何体。默认值为False。 需要Shapely >= 2.0。
- Returns:
- Indices or tuple of (indices, distances)
indices是形状为(2,n)的ndarray,而distances(如果存在)是形状为(n)的ndarray。 indices的第一个子数组包含输入几何体的索引。 indices的第二个子数组包含树几何体的索引。
示例
>>> from shapely.geometry import Point, box >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10))) >>> s.head() 0 POINT (0 0) 1 POINT (1 1) 2 POINT (2 2) 3 POINT (3 3) 4 POINT (4 4) dtype: geometry
>>> s.sindex.nearest(Point(1, 1)) array([[0], [1]])
>>> s.sindex.nearest([box(4.9, 4.9, 5.1, 5.1)]) array([[0], [5]])
>>> s2 = geopandas.GeoSeries(geopandas.points_from_xy([7.6, 10], [7.6, 10])) >>> s2 0 POINT (7.6 7.6) 1 POINT (10 10) dtype: geometry
>>> s.sindex.nearest(s2) array([[0, 1], [8, 9]])