Class: QgsSpatialIndexKDBush

A very fast static spatial index for 2D points based on a flat KD-tree.

Compared to QgsSpatialIndex, this index:

  • supports single point features only (no multipoints)

  • is static (features cannot be added or removed from the index after construction)

  • is much faster!

  • allows direct retrieval of the original feature’s points, without requiring additional feature requests

  • supports true “distance based” searches, i.e. return all points within a radius from a search point

QgsSpatialIndexKDBush objects are implicitly shared and can be inexpensively copied.

See also

QgsSpatialIndex which is an general, mutable index for geometry bounding boxes.

Added in version 3.4.

List of all members, including inherited members

Methods

addFeature

Adds a single feature to the index.

finalize

Finalizes the index after manually adding features.

intersects

Returns the list of features which fall within the specified rectangle.

size

Returns the size of the index, i.e. the number of points contained within the index.

within

Returns the list of features which are within the given search radius of point.

class qgis.core.QgsSpatialIndexKDBush[source]

Bases: object

__init__(fi: QgsFeatureIterator, feedback: QgsFeedback | None = None)

Constructor - creates KDBush index and bulk loads it with features from the iterator.

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.

Any non-single point features encountered during iteration will be ignored and not included in the index.

Parameters:
__init__(source: QgsFeatureSource, feedback: QgsFeedback | None = None)

Constructor - creates KDBush index and bulk loads it with features from the source.

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.

Any non-single point features encountered during iteration will be ignored and not included in the index.

Parameters:
__init__()

Constructs an empty index.

The index must be manually populated by calls to addFeature() followed by finalize().

Added in version 4.0.

__init__(other: QgsSpatialIndexKDBush)
Parameters:

other (QgsSpatialIndexKDBush)

addFeature(self, id: int, point: QgsPointXY) bool[source]

Adds a single feature to the index.

Note

A call to finalize() must be made after all features have been added. It is not possible to add features after finalizing the spatial index.

Return type:

bool

Returns:

True if feature was successfully added to index.

Added in version 4.0.

Parameters:
finalize(self)[source]

Finalizes the index after manually adding features.

This method must be called after adding all features via addFeature() and before performing any queries on the index.

Added in version 4.0.

intersects(self, rectangle: QgsRectangle) list[QgsSpatialIndexKDBushData]

Returns the list of features which fall within the specified rectangle.

Parameters:

rectangle (QgsRectangle)

Return type:

list[QgsSpatialIndexKDBushData]

size(self) int[source]

Returns the size of the index, i.e. the number of points contained within the index.

Return type:

int

within(self, point: QgsPointXY, radius: float) list[QgsSpatialIndexKDBushData]

Returns the list of features which are within the given search radius of point.

Parameters:
Return type:

list[QgsSpatialIndexKDBushData]