26 #include "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 );
306 if ( !featureInfo( f, rect,
id ) )
314 SpatialIndex::Region r( rectToRegion( rect ) );
316 QMutexLocker locker( &d->mMutex );
324 catch ( Tools::Exception &e )
327 QgsDebugMsg( QString(
"Tools::Exception caught: " ).arg( e.what().c_str() ) );
329 catch (
const std::exception &e )
332 QgsDebugMsg( QString(
"std::exception caught: " ).arg( e.what() ) );
336 QgsDebugMsg(
"unknown spatial index exception caught" );
344 SpatialIndex::Region r;
346 if ( !featureInfo( f, r,
id ) )
349 QMutexLocker locker( &d->mMutex );
356 QList<QgsFeatureId> list;
359 SpatialIndex::Region r = rectToRegion( rect );
361 QMutexLocker locker( &d->mMutex );
362 d->mRTree->intersectsWithQuery( r, visitor );
369 QList<QgsFeatureId> list;
372 double pt[2] = { point.
x(), point.
y() };
375 QMutexLocker locker( &d->mMutex );
376 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.
QList< QgsFeatureId > nearestNeighbor(const QgsPointXY &point, int neighbors) const
Returns nearest neighbors to a point.
A class to represent a 2D point.
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
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...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
QgsSpatialIndexCopyVisitor(SpatialIndex::ISpatialIndex *newIndex)
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 cancelation of something running in a worker thread...
QgisVisitor(QList< QgsFeatureId > &list)
void visitData(std::vector< const IData *> &v) override
#define FID_TO_NUMBER(fid)
Data of spatial index that may be implicitly shared.
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.
void visitData(std::vector< const IData *> &v) override
Utility class for bulk loading of R-trees.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
QgsSpatialIndexData(const QgsFeatureIterator &fi, QgsFeedback *feedback=nullptr)
Constructor for QgsSpatialIndexData which bulk loads features from the specified feature iterator fi...
void visitData(const IData &d) override
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
~QgsFeatureIteratorDataStream() override
double xMaximum() const
Returns the x maximum value (right side of rectangle).
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.
~QgsSpatialIndex()
Destructor finalizes work with spatial index.
An interface for objects which provide features via a getFeatures method.
Custom visitor that adds found features to list.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
QList< int > QgsAttributeList
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
QAtomicInt refs() const
Gets reference count - just for debugging!