geopandas.sindex.SpatialIndex.query#

SpatialIndex.query(geometry, predicate=None, sort=False, distance=None, output_format='indices')[来源]#

返回每个输入几何形状和树几何形状的所有组合,其中每个输入几何形状的边界框与树几何形状的边界框相交。

结果可以作为“索引”的数组或布尔值“稀疏”或“密集”数组返回。这可以使用 output_format 关键字进行控制。选项如下。

'indices'

如果输入几何是标量,则返回形状为 (n, ) 的数组,其中包含匹配的树状几何体的索引。如果输入几何是类数组,这将返回形状为 (2,n) 的数组,其中子数组对应于输入几何的索引和与每个输入几何相关的树状几何的索引。要生成输入几何索引和树状几何索引的配对数组,只需转置结果。

'sparse'

如果输入几何体是一个标量,这将返回一个布尔值的 scipy.sparse COO 数组,形状为 (len(tree), ),布尔值标记树中几何体的边界框是否与给定标量的边界框相交。如果输入几何体是一个类似数组的类型,这将返回一个布尔值的 scipy.sparse COO 数组,形状为 (len(tree), n),布尔值标记树中几何体的边界框是否与给定标量的边界框相交。

'dense'

如果输入几何是一个标量,则返回一个布尔 numpy 数组,形状为 (len(tree), ),其布尔值标记树中的几何体的边界框是否与给定标量的边界框相交。如果输入几何是一个类似数组的对象,则返回一个布尔 numpy 数组,形状为 (len(tree), n),其布尔值标记树中的几何体的边界框是否与给定标量的边界框相交。

如果提供了谓词,树状几何体将首先根据输入几何体的边界框进行查询,然后进一步过滤满足谓词的那些几何体,当比较输入几何体与树状几何体时:predicate(geometry, tree_geometry)

‘dwithin’ 谓词需要 GEOS >= 3.10。

边界框限于二维并且与轴对齐(相当于几何体的 bounds 属性);在查询树时,输入几何体中的任何 Z 值都会被忽略。

任何输入的几何图形为 None 或空时,将永远不会与树中的几何图形匹配。

Parameters:
geometryshapely.Geometry or array-like of geometries (numpy.ndarray, GeoSeries, GeometryArray)

一个单一的Shapely几何体或几何体数组,用于查询空间索引。对于类似数组的内容,接受GeoPandas几何可迭代对象(GeoSeries, GeometryArray)或Shapely几何体的numpy数组。

predicate{None, “contains”, “contains_properly”, “covered_by”, “covers”, “crosses”, “intersects”, “overlaps”, “touches”, “within”, “dwithin”}, optional

如果提供了谓词,输入几何体将使用谓词函数对树中每个与输入几何体的包络线相交的项进行测试:predicate(input_geometry, tree_geometry)。如果可能,将使用准备好的几何体来帮助加速谓词操作。

sortbool, default False

如果为True,则结果将按升序排序。如果是2D数组,结果将使用几何体的索引作为主键,使用sindex的索引作为次键进行字典序排序。如果为False,则不应用额外的排序(结果通常是排序的,但没有保证)。仅适用于output_format=”indices”。

distancenumber or array_like, optional

在每个输入几何形状周围的距离,用于查询树的“dwithin”谓词。如果是类似数组,则形状必须可以广播到几何形状的形状。 如果predicate='dwithin',则这是必需的。

output_format{“indices”, “sparse”, “dense”}, default “indices”

表示查询结果的输出格式类型。

Returns:
If geometry is a scalar:
ndarray with shape (n,)

用于匹配空间索引树几何体的整数索引。如果 output_format="indices"

OR
scipy.sparse COO array with shape (len(tree), )

与树中几何图形数组对齐的布尔数组。
如果 output_format="sparse"

OR
ndarray with shape (len(tree), )

与树中几何图形数组对齐的布尔数组。 如果 output_format="dense"

If geometry is an array_like:
ndarray with shape (2, n)

第一个子数组包含输入几何体的整数索引。 第二个子数组包含树几何体的整数索引。 如果 output_format="indices"

OR
scipy.sparse COO array with shape (len(tree), n)

与树中沿轴0的几何体数组对齐的布尔数组,以及沿轴1的 geometry。如果output_format="sparse"

OR
ndarray with shape (len(tree), n)

与树中几何体数组在轴 0 上对齐的布尔数组,以及在轴 1 上与 geometry 对齐。 如果 output_format="dense"

笔记

在空间连接的上下文中,输入几何体是“左”几何体,它们决定结果的顺序,而树几何体是与左几何体连接的“右”几何体。这实际上执行了内连接,仅返回那些可以根据重叠的边界框或可选谓词连接的几何体组合。

示例

>>> from shapely.geometry import Point, box
>>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
>>> s
0    POINT (0 0)
1    POINT (1 1)
2    POINT (2 2)
3    POINT (3 3)
4    POINT (4 4)
5    POINT (5 5)
6    POINT (6 6)
7    POINT (7 7)
8    POINT (8 8)
9    POINT (9 9)
dtype: geometry

使用标量几何查询树:

>>> s.sindex.query(box(1, 1, 3, 3))
array([1, 2, 3])
>>> s.sindex.query(box(1, 1, 3, 3), predicate="contains")
array([2])

使用几何图形数组查询树:

>>> s2 = geopandas.GeoSeries([box(2, 2, 4, 4), box(5, 5, 6, 6)])
>>> s2
0    POLYGON ((4 2, 4 4, 2 4, 2 2, 4 2))
1    POLYGON ((6 5, 6 6, 5 6, 5 5, 6 5))
dtype: geometry
>>> s.sindex.query(s2)
array([[0, 0, 0, 1, 1],
       [2, 3, 4, 5, 6]])
>>> s.sindex.query(s2, predicate="contains")
array([[0],
       [3]])
>>> s.sindex.query(box(1, 1, 3, 3), predicate="dwithin", distance=0)
array([1, 2, 3])
>>> s.sindex.query(box(1, 1, 3, 3), predicate="dwithin", distance=2)
array([0, 1, 2, 3, 4])

返回布尔数组:

>>> s.sindex.query(box(1, 1, 3, 3), output_format="sparse")
<COOrdinate sparse array of dtype 'bool'
    with 3 stored elements and shape (10,)>
>>> s.sindex.query(box(1, 1, 3, 3), output_format="dense")
array([False,  True,  True,  True, False, False, False, False, False,
       False])
>>> s.sindex.query(s2, output_format="sparse")
<COOrdinate sparse array of dtype 'bool'
    with 5 stored elements and shape (10, 2)>
>>> s.sindex.query(s2, output_format="dense")
array([[False, False],
       [False, False],
       [ True, False],
       [ True, False],
       [ True, False],
       [False,  True],
       [False,  True],
       [False, False],
       [False, False],
       [False, False]])