26 #include <spatialindex/SpatialIndex.h> 28 #include <QMutexLocker> 51 mList.append( d.getIdentifier() );
54 void visitData( std::vector<const IData *> &v )
override 58 QList<QgsFeatureId> &mList;
70 : mNewIndex( newIndex ) {}
77 SpatialIndex::IShape *shape =
nullptr;
79 mNewIndex->insertData( 0,
nullptr, *shape, d.getIdentifier() );
83 void visitData( std::vector<const IData *> &v )
override 87 SpatialIndex::ISpatialIndex *mNewIndex =
nullptr;
103 , mFeedback( feedback )
116 if ( mFeedback && mFeedback->isCanceled() )
119 RTree::Data *ret = mNextData;
126 bool hasNext()
override {
return nullptr != mNextData; }
129 uint32_t
size()
override { Q_ASSERT(
false &&
"not available" );
return 0; }
132 void rewind()
override { Q_ASSERT(
false &&
"not available" ); }
138 SpatialIndex::Region r;
140 while ( mFi.nextFeature( f ) )
142 if ( QgsSpatialIndex::featureInfo( f, r,
id ) )
144 mNextData =
new RTree::Data( 0,
nullptr, r,
id );
152 RTree::Data *mNextData =
nullptr;
186 : QSharedData( other )
188 QMutexLocker locker( &other.
mMutex );
193 double low[] = { std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest() };
194 double high[] = { std::numeric_limits<double>::max(), std::numeric_limits<double>::max() };
195 SpatialIndex::Region query( low, high, 2 );
197 other.
mRTree->intersectsWithQuery( query, visitor );
208 void initTree( IDataStream *inputStream =
nullptr )
211 mStorage = StorageManager::createNewMemoryStorageManager();
214 double fillFactor = 0.7;
215 unsigned long indexCapacity = 10;
216 unsigned long leafCapacity = 10;
217 unsigned long dimension = 2;
218 RTree::RTreeVariant variant = RTree::RV_RSTAR;
221 SpatialIndex::id_type indexId;
223 if ( inputStream && inputStream->hasNext() )
224 mRTree = RTree::createAndBulkLoadNewRTree( RTree::BLM_STR, *inputStream, *mStorage, fillFactor, indexCapacity,
225 leafCapacity, dimension, variant, indexId );
227 mRTree = RTree::createNewRTree( *mStorage, fillFactor, indexCapacity,
228 leafCapacity, dimension, variant, indexId );
232 SpatialIndex::IStorageManager *mStorage =
nullptr;
235 SpatialIndex::ISpatialIndex *mRTree =
nullptr;
270 if (
this != &other )
275 SpatialIndex::Region QgsSpatialIndex::rectToRegion(
const QgsRectangle &rect )
279 return SpatialIndex::Region( pt1, pt2, 2 );
285 if ( !featureInfo( f, rect,
id ) )
288 r = rectToRegion( rect );
310 if ( !featureInfo( feature, rect,
id ) )
318 QgsFeatureList::iterator fIt = features.begin();
320 for ( ; fIt != features.end(); ++fIt )
340 SpatialIndex::Region r( rectToRegion( bounds ) );
342 QMutexLocker locker( &d->mMutex );
350 catch ( Tools::Exception &e )
353 QgsDebugMsg( QStringLiteral(
"Tools::Exception caught: " ).arg( e.what().c_str() ) );
355 catch (
const std::exception &e )
358 QgsDebugMsg( QStringLiteral(
"std::exception caught: " ).arg( e.what() ) );
362 QgsDebugMsg( QStringLiteral(
"unknown spatial index exception caught" ) );
370 SpatialIndex::Region r;
372 if ( !featureInfo( f, r,
id ) )
375 QMutexLocker locker( &d->mMutex );
382 QList<QgsFeatureId> list;
385 SpatialIndex::Region r = rectToRegion( rect );
387 QMutexLocker locker( &d->mMutex );
388 d->mRTree->intersectsWithQuery( r, visitor );
395 QList<QgsFeatureId> list;
398 double pt[2] = { point.
x(), point.
y() };
401 QMutexLocker locker( &d->mMutex );
402 d->mRTree->nearestNeighborQuery( neighbors, p, visitor );
bool hasNext() override
returns true if there are more items in the stream.
IData * getNext() override
returns a pointer to the next entry in the stream or 0 at the end of the stream.
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
QgsSpatialIndex & operator=(const QgsSpatialIndex &other)
Implement assignment operator.
SpatialIndex::ISpatialIndex * mRTree
R-tree containing spatial index.
bool isFinite() const
Returns true if the rectangle has finite boundaries.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
QList< QgsFeature > QgsFeatureList
A class to represent a 2D point.
void visitData(std::vector< const IData * > &v) override
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QgsFeatureIteratorDataStream(const QgsFeatureIterator &fi, QgsFeedback *feedback=nullptr)
constructor - needs to load all data to a vector for later access when bulk loading ...
void visitNode(const INode &n) override
Q_DECL_DEPRECATED bool insertFeature(const QgsFeature &feature)
Adds a feature to the index.
void initTree(IDataStream *inputStream=nullptr)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsSpatialIndexCopyVisitor(SpatialIndex::ISpatialIndex *newIndex)
void visitData(std::vector< const IData * > &v) override
void rewind() override
sets the stream pointer to the first entry, if possible.
QgsSpatialIndexData(const QgsSpatialIndexData &other)
void visitNode(const INode &n) override
Base class for feedback objects to be used for cancellation of something running in a worker thread...
QgisVisitor(QList< QgsFeatureId > &list)
QList< QgsFeatureId > nearestNeighbor(const QgsPointXY &point, int neighbors) const
Returns nearest neighbors to a point.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Data of spatial index that may be implicitly shared.
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void visitData(const IData &d) override
QgsSpatialIndex()
Constructor for QgsSpatialIndex.
Utility class for bulk loading of R-trees.
QgsSpatialIndexData(const QgsFeatureIterator &fi, QgsFeedback *feedback=nullptr)
Constructor for QgsSpatialIndexData which bulk loads features from the specified feature iterator fi...
QAtomicInt refs() const
Gets reference count - just for debugging!
void visitData(const IData &d) override
~QgsFeatureIteratorDataStream() override
uint32_t size() override
returns the total number of entries available in the stream.
bool deleteFeature(const QgsFeature &feature)
Removes a feature from the index.
A spatial index for QgsFeature objects.
An interface for objects which provide features via a getFeatures method.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) override
Adds a list of features to the index.
Custom visitor that adds found features to list.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
#define FID_TO_NUMBER(fid)
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a feature to the index.
~QgsSpatialIndex() override
Destructor finalizes work with spatial index.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.