geopandas.GeoDataFrame.sindex#

property GeoDataFrame.sindex[来源]#

生成空间索引

基于 shapely.STRtree 创建 R-tree 空间索引。

请注意,空间索引可能在第一次使用之前未完全初始化。

示例

>>> from shapely.geometry import box
>>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(5), range(5)))
>>> s
0    POINT (0 0)
1    POINT (1 1)
2    POINT (2 2)
3    POINT (3 3)
4    POINT (4 4)
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(1, 1, 3, 3), box(4, 4, 5, 5)])
>>> s2
0    POLYGON ((3 1, 3 3, 1 3, 1 1, 3 1))
1    POLYGON ((5 4, 5 5, 4 5, 4 4, 5 4))
dtype: geometry
>>> s.sindex.query(s2)
array([[0, 0, 0, 1],
       [1, 2, 3, 4]])

根据谓词查询包含几何图形的空间索引:

>>> s.sindex.query(s2, predicate="contains")
array([[0],
       [2]])