Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
QgsSpatialIndexKDBush is implicitly shared for fast copies
- Loading branch information
1 parent
0df1056
commit 5c552dd
Showing
6 changed files
with
235 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsspatialindexkdbush.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
class QgsSpatialIndexKDBush | ||
{ | ||
%Docstring | ||
|
||
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! | ||
- supports true "distance based" searches, i.e. return all points within a radius | ||
from a search point | ||
|
||
.. seealso:: :py:class:`QgsSpatialIndex` | ||
|
||
.. versionadded:: 3.4 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsspatialindexkdbush.h" | ||
%End | ||
public: | ||
|
||
explicit QgsSpatialIndexKDBush( QgsFeatureIterator &fi, QgsFeedback *feedback = 0 ); | ||
%Docstring | ||
Constructor - creates KDBush index and bulk loads it with features from the iterator. | ||
|
||
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. | ||
|
||
Any non-single point features encountered during iteration will be ignored and not included in the index. | ||
%End | ||
|
||
explicit QgsSpatialIndexKDBush( const QgsFeatureSource &source, QgsFeedback *feedback = 0 ); | ||
%Docstring | ||
Constructor - creates KDBush index and bulk loads it with features from the source. | ||
|
||
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. | ||
|
||
Any non-single point features encountered during iteration will be ignored and not included in the index. | ||
%End | ||
|
||
QgsSpatialIndexKDBush( const QgsSpatialIndexKDBush &other ); | ||
%Docstring | ||
Copy constructor | ||
%End | ||
|
||
|
||
~QgsSpatialIndexKDBush(); | ||
|
||
QList<QgsFeatureId> intersect( const QgsRectangle &rectangle ) const; | ||
%Docstring | ||
Returns a list of features which fall within the specified ``rectangle``. | ||
%End | ||
|
||
QList<QgsFeatureId> within( const QgsPointXY &point, double radius ) const; | ||
%Docstring | ||
Returns a list of features which are within the given search ``radius`` | ||
of ``point``. | ||
%End | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsspatialindexkdbush.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/*************************************************************************** | ||
qgsspatialindexkdbush_p.h | ||
----------------- | ||
begin : July 2018 | ||
copyright : (C) 2018 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSSPATIALINDEXKDBUSH_PRIVATE_H | ||
#define QGSSPATIALINDEXKDBUSH_PRIVATE_H | ||
|
||
/// @cond PRIVATE | ||
|
||
// | ||
// W A R N I N G | ||
// ------------- | ||
// | ||
// This file is not part of the QGIS API. It exists purely as an | ||
// implementation detail. This header file may change from version to | ||
// version without notice, or even be removed. | ||
// | ||
|
||
#include "qgsfeature.h" | ||
|
||
#include "qgsfeatureiterator.h" | ||
#include "qgsfeedback.h" | ||
#include "qgsfeaturesource.h" | ||
#include <memory> | ||
#include <QList> | ||
#include "kdbush.hpp" | ||
|
||
class PointXYKDBush : public kdbush::KDBush< std::pair<double, double>, QgsFeatureId > | ||
{ | ||
public: | ||
|
||
explicit PointXYKDBush( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr ) | ||
{ | ||
fillFromIterator( fi, feedback ); | ||
} | ||
|
||
explicit PointXYKDBush( const QgsFeatureSource &source, QgsFeedback *feedback ) | ||
{ | ||
points.reserve( source.featureCount() ); | ||
ids.reserve( source.featureCount() ); | ||
QgsFeatureIterator it = source.getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) ); | ||
fillFromIterator( it, feedback ); | ||
} | ||
|
||
void fillFromIterator( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr ) | ||
{ | ||
QgsFeatureId size = 0; | ||
|
||
QgsFeature f; | ||
while ( fi.nextFeature( f ) ) | ||
{ | ||
if ( feedback && feedback->isCanceled() ) | ||
return; | ||
|
||
if ( !f.hasGeometry() ) | ||
continue; | ||
|
||
if ( QgsWkbTypes::flatType( f.geometry().wkbType() ) == QgsWkbTypes::Point ) | ||
{ | ||
const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( f.geometry().constGet() ); | ||
points.emplace_back( point->x(), point->y() ); | ||
} | ||
else | ||
{ | ||
// not a point | ||
continue; | ||
} | ||
|
||
ids.push_back( f.id() ); | ||
size++; | ||
} | ||
|
||
sortKD( 0, size - 1, 0 ); | ||
} | ||
|
||
}; | ||
|
||
class QgsSpatialIndexKDBushPrivate | ||
{ | ||
public: | ||
|
||
explicit QgsSpatialIndexKDBushPrivate( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr ) | ||
: index( qgis::make_unique < PointXYKDBush >( fi, feedback ) ) | ||
{} | ||
|
||
explicit QgsSpatialIndexKDBushPrivate( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr ) | ||
: index( qgis::make_unique < PointXYKDBush >( source, feedback ) ) | ||
{} | ||
|
||
QAtomicInt ref = 1; | ||
std::unique_ptr< PointXYKDBush > index; | ||
}; | ||
|
||
/// @endcond | ||
|
||
#endif // QGSSPATIALINDEXKDBUSH_PRIVATE_H |