Class: QgsSpatialIndex

class qgis.core.QgsSpatialIndex

Bases: qgis._core.QgsFeatureSink

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

QgsSpatialIndex(fi: QgsFeatureIterator, feedback: QgsFeedback = None) 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) 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.

See also

QgsSpatialIndexKDBush

Enums

Methods

addFeature

Adds a feature to the index.

addFeatures

Adds a list of features to the index.

deleteFeature

Removes a feature from the index.

insertFeature

Adds a feature to the index.

intersects

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

nearestNeighbor

Returns nearest neighbors to a point.

refs

Gets reference count - just for debugging!

Signals

Attributes

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.

Returns

true if feature was successfully added to index.

New in version 3.4.

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

Adds a list of features to the index.

The flags argument is ignored.

See also

addFeature()

deleteFeature(self, feature: QgsFeature) → bool

Removes a feature from the index.

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.

Returns

true if feature was successfully added to index.

Deprecated since version Use: addFeature() instead

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.

nearestNeighbor(self, point: QgsPointXY, neighbors: int) → object

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

Note

The nearest neighbour 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 neighbours.

refs(self) → int

Gets reference count - just for debugging!