QgsSpatialIndex class

A spatial index for QgsFeature objects.

QgsSpatialIndex objects are implicitly shared and can be inexpensively copied.

Base classes

class QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.

Constructors, destructors, conversion operators

QgsSpatialIndex()
Constructor for QgsSpatialIndex.
QgsSpatialIndex(const QgsFeatureIterator& fi, QgsFeedback* feedback = nullptr) explicit
Constructor - creates R-tree and bulk loads it with features from the iterator.
QgsSpatialIndex(const QgsFeatureSource& source, QgsFeedback* feedback = nullptr) explicit
Constructor - creates R-tree and bulk loads it with features from the source.
QgsSpatialIndex(const QgsSpatialIndex& other)
Copy constructor.
~QgsSpatialIndex() override
Destructor finalizes work with spatial index.

Public functions

auto addFeature(QgsFeature& feature, QgsFeatureSink::Flags flags = nullptr) -> bool override
Adds a feature to the index.
auto addFeature(QgsFeatureId id, const QgsRectangle& bounds) -> bool
Add a feature id to the index with a specified bounding box.
auto addFeatures(QgsFeatureList& features, QgsFeatureSink::Flags flags = nullptr) -> bool override
Adds a list of features to the index.
auto deleteFeature(const QgsFeature& feature) -> bool
Removes a feature from the index.
auto insertFeature(const QgsFeature& feature) -> Q_DECL_DEPRECATED bool deprecated
Adds a feature to the index.
auto insertFeature(QgsFeatureId id, const QgsRectangle& bounds) -> Q_DECL_DEPRECATED bool deprecated
Add a feature id to the index with a specified bounding box.
auto intersects(const QgsRectangle& rectangle) const -> QList<QgsFeatureId>
Returns a list of features with a bounding box which intersects the specified rectangle.
auto nearestNeighbor(const QgsPointXY& point, int neighbors) const -> QList<QgsFeatureId>
Returns nearest neighbors to a point.
auto operator=(const QgsSpatialIndex& other) -> QgsSpatialIndex&
Implement assignment operator.
auto refs() const -> QAtomicInt
Gets reference count - just for debugging!

Function documentation

QgsSpatialIndex::QgsSpatialIndex()

Constructor for QgsSpatialIndex.

Creates an empty R-tree index.

QgsSpatialIndex::QgsSpatialIndex(const QgsFeatureIterator& fi, QgsFeedback* feedback = nullptr) explicit

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 cancelation 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.

QgsSpatialIndex::QgsSpatialIndex(const QgsFeatureSource& source, QgsFeedback* feedback = nullptr) explicit

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 cancelation 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.

bool QgsSpatialIndex::addFeature(QgsFeature& feature, QgsFeatureSink::Flags flags = nullptr) override

Adds a feature to the index.

The flags argument is ignored.

bool QgsSpatialIndex::addFeature(QgsFeatureId id, const QgsRectangle& bounds)

Add a feature id to the index with a specified bounding box.

Returns true if feature was successfully added to index.

bool QgsSpatialIndex::addFeatures(QgsFeatureList& features, QgsFeatureSink::Flags flags = nullptr) override

Adds a list of features to the index.

The flags argument is ignored.

Q_DECL_DEPRECATED bool QgsSpatialIndex::insertFeature(const QgsFeature& feature)

Adds a feature to the index.

Q_DECL_DEPRECATED bool QgsSpatialIndex::insertFeature(QgsFeatureId id, const QgsRectangle& bounds)

Add a feature id to the index with a specified bounding box.

Returns true if feature was successfully added to index.

QList<QgsFeatureId> QgsSpatialIndex::intersects(const QgsRectangle& rectangle) const

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

QList<QgsFeatureId> QgsSpatialIndex::nearestNeighbor(const QgsPointXY& point, int neighbors) const

Returns nearest neighbors to a point.

The number of neighbours returned is specified by the neighbours argument.