geopandas.sindex.SpatialIndex.query#

SpatialIndex.query(geometry, predicate=None, sort=False)[source]#

Return the index of all geometries in the tree with extents that intersect the envelope of the input geometry.

When using the rtree package, this is not a vectorized function. If speed is important, please use PyGEOS.

Parameters
geometryshapely geometry

A single shapely geometry to query against the spatial index.

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

If predicate is provided, the input geometry is tested using the predicate function against each item in the tree whose extent intersects the envelope of the input geometry: predicate(input_geometry, tree_geometry). If possible, prepared geometries are used to help speed up the predicate operation.

sortbool, default False

If True, the results will be sorted in ascending order. If False, results are often sorted but there is no guarantee.

Returns
matchesndarray of shape (n_results, )

Integer indices for matching geometries from the spatial index.

Examples

>>> from shapely.geometry import Point, box
>>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
>>> s
0    POINT (0.00000 0.00000)
1    POINT (1.00000 1.00000)
2    POINT (2.00000 2.00000)
3    POINT (3.00000 3.00000)
4    POINT (4.00000 4.00000)
5    POINT (5.00000 5.00000)
6    POINT (6.00000 6.00000)
7    POINT (7.00000 7.00000)
8    POINT (8.00000 8.00000)
9    POINT (9.00000 9.00000)
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])