Class: QgsSpatialIndex

class qgis.core.QgsSpatialIndex(flags: Union[QgsSpatialIndex.Flags, QgsSpatialIndex.Flag] = 0)

Bases: QgsFeatureSink

Constructor for QgsSpatialIndex. Creates an empty R-tree index.

QgsSpatialIndex(fi: QgsFeatureIterator, feedback: QgsFeedback = None, flags: Union[QgsSpatialIndex.Flags, QgsSpatialIndex.Flag] = 0) Constructor - creates R-tree and bulk loads it with features from the iterator. This is much faster approach than creating an empty index and then inserting features one by one.

The optional feedback object can be used to allow cancellation of bulk feature loading. Ownership of feedback is not transferred, and callers must take care that the lifetime of feedback exceeds that of the spatial index construction.

New in version 2.8.

QgsSpatialIndex(source: QgsFeatureSource, feedback: QgsFeedback = None, flags: Union[QgsSpatialIndex.Flags, QgsSpatialIndex.Flag] = 0) Constructor - creates R-tree and bulk loads it with features from the source. This is much faster approach than creating an empty index and then inserting features one by one.

The optional feedback object can be used to allow cancellation of bulk feature loading. Ownership of feedback is not transferred, and callers must take care that the lifetime of feedback exceeds that of the spatial index construction.

New in version 3.0.

QgsSpatialIndex(other: QgsSpatialIndex) Copy constructor

A spatial index for QgsFeature objects.

QgsSpatialIndex objects are implicitly shared and can be inexpensively copied.

Note

While the underlying libspatialindex is not thread safe on some platforms, the QgsSpatialIndex class implements its own locks and accordingly, a single QgsSpatialIndex object can safely be used across multiple threads.

Parameters

flags

class Flag

Bases: int

FlagStoreFeatureGeometries = 1
class Flags

Bases: sip.wrapper

QgsSpatialIndex.Flags(Union[QgsSpatialIndex.Flags, QgsSpatialIndex.Flag]) QgsSpatialIndex.Flags(QgsSpatialIndex.Flags)

addFeature(self, feature: QgsFeature, flags: Union[QgsFeatureSink.Flags, QgsFeatureSink.Flag] = 0) → bool

Adds a feature to the index.

The flags argument is ignored.

New in version 3.4.

addFeature(self, id: int, bounds: QgsRectangle) -> bool Add a feature id to the index with a specified bounding box.

Return type

bool

Returns

True if feature was successfully added to index.

New in version 3.4.

Parameters
addFeatures(self, features: Iterable[QgsFeature], flags: Union[QgsFeatureSink.Flags, QgsFeatureSink.Flag] = 0) → bool

Adds a list of features to the index.

The flags argument is ignored.

See also

addFeature()

Parameters
Return type

bool

deleteFeature(self, feature: QgsFeature) → bool

Removes a feature from the index.

Parameters

feature (QgsFeature) –

Return type

bool

geometry(self, id: int) → QgsGeometry

Returns the stored geometry for the indexed feature with matching id. A KeyError will be raised if no geometry with the specified feature id exists in the index.

Geometry is only stored if the QgsSpatialIndex was created with the FlagStoreFeatureGeometries flag.

New in version 3.6.

Parameters

id (int) –

Return type

QgsGeometry

insertFeature(self, feature: QgsFeature) → bool

Adds a feature to the index.

Deprecated since version Use: addFeature() instead

insertFeature(self, id: int, bounds: QgsRectangle) -> bool Add a feature id to the index with a specified bounding box.

Return type

bool

Returns

True if feature was successfully added to index.

Deprecated since version Use: addFeature() instead

Parameters

feature (QgsFeature) –

intersects(self, rectangle: QgsRectangle) → object

Returns a list of features with a bounding box which intersects the specified rectangle.

Note

The intersection test is performed based on the feature bounding boxes only, so for non-point geometry features it is necessary to manually test the returned features for exact geometry intersection when required.

Parameters

rectangle (QgsRectangle) –

Return type

object

nearestNeighbor(self, point: QgsPointXY, neighbors: int = 1, maxDistance: float = 0) → object

Returns nearest neighbors to a point. The number of neighbors returned is specified by the neighbors argument.

If the maxDistance argument is greater than 0, then only features within the specified distance of point will be considered.

Note that in some cases the number of returned features may differ from the requested number of neighbors. E.g. if not enough features exist within the maxDistance of the search point. If multiple features are equidistant from the search point then the number of returned feature IDs may exceed neighbors.

Warning

If this QgsSpatialIndex object was not constructed with the FlagStoreFeatureGeometries flag, then the nearest neighbor test is performed based on the feature bounding boxes ONLY, so for non-point geometry features this method is not guaranteed to return the actual closest neighbors.

nearestNeighbor(self, geometry: QgsGeometry, neighbors: int = 1, maxDistance: float = 0) -> object Returns nearest neighbors to a geometry. The number of neighbors returned is specified by the neighbors argument.

If the maxDistance argument is greater than 0, then only features within the specified distance of point will be considered.

Note that in some cases the number of returned features may differ from the requested number of neighbors. E.g. if not enough features exist within the maxDistance of the search point. If multiple features are equidistant from the search point then the number of returned feature IDs may exceed neighbors.

Warning

If this QgsSpatialIndex object was not constructed with the FlagStoreFeatureGeometries flag, then the nearest neighbor test is performed based on the feature bounding boxes ONLY, so for non-point geometry features this method is not guaranteed to return the actual closest neighbors.

New in version 3.8.

Parameters
  • point (QgsPointXY) –

  • neighbors (int = 1) –

  • maxDistance (float = 0) –

Return type

object

refs(self) → int

Gets reference count - just for debugging!

Return type

int