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;
91 class QgsNearestNeighborComparator :
public INearestNeighborComparator
95 QgsNearestNeighborComparator(
const QHash< QgsFeatureId, QgsGeometry > *geometries,
const QgsPointXY &point,
double maxDistance )
96 : mGeometries( geometries )
98 , mMaxDistance( maxDistance )
102 const QHash< QgsFeatureId, QgsGeometry > *mGeometries =
nullptr;
104 double mMaxDistance = 0;
105 QSet< QgsFeatureId > mFeaturesOutsideMaxDistance;
107 double getMinimumDistance(
const IShape &query,
const IShape &entry )
override 109 return query.getMinimumDistance( entry );
112 double getMinimumDistance(
const IShape &query,
const IData &data )
override 116 data.getShape( &pS );
117 double dist = query.getMinimumDistance( *pS );
125 if ( mGeometries && ( mMaxDistance <= 0.0 || dist <= mMaxDistance ) )
127 QgsGeometry other = mGeometries->value( data.getIdentifier() );
131 if ( mMaxDistance > 0 && dist > mMaxDistance )
141 mFeaturesOutsideMaxDistance.insert( data.getIdentifier() );
142 return mMaxDistance + 0.00000001;
154 class QgsFeatureIteratorDataStream :
public IDataStream
158 explicit QgsFeatureIteratorDataStream(
const QgsFeatureIterator &fi,
QgsFeedback *feedback =
nullptr, QgsSpatialIndex::Flags flags =
nullptr )
160 , mFeedback( feedback )
166 ~QgsFeatureIteratorDataStream()
override 172 IData *getNext()
override 174 if ( mFeedback && mFeedback->isCanceled() )
177 RTree::Data *ret = mNextData;
184 bool hasNext()
override {
return nullptr != mNextData; }
187 uint32_t size()
override { Q_ASSERT(
false &&
"not available" );
return 0; }
190 void rewind()
override { Q_ASSERT(
false &&
"not available" ); }
192 QHash< QgsFeatureId, QgsGeometry > geometries;
198 SpatialIndex::Region r;
200 while ( mFi.nextFeature( f ) )
202 if ( QgsSpatialIndex::featureInfo( f, r,
id ) )
204 mNextData =
new RTree::Data( 0,
nullptr, r,
id );
214 RTree::Data *mNextData =
nullptr;
216 QgsSpatialIndex::Flags mFlags =
nullptr;
227 class QgsSpatialIndexData :
public QSharedData
230 QgsSpatialIndexData( QgsSpatialIndex::Flags flags )
236 QgsSpatialIndex::Flags mFlags =
nullptr;
238 QHash< QgsFeatureId, QgsGeometry > mGeometries;
251 QgsFeatureIteratorDataStream fids( fi, feedback, mFlags );
254 mGeometries = fids.geometries;
257 QgsSpatialIndexData(
const QgsSpatialIndexData &other )
258 : QSharedData( other )
259 , mFlags( other.mFlags )
260 , mGeometries( other.mGeometries )
262 QMutexLocker locker( &other.mMutex );
267 double low[] = { std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest() };
268 double high[] = { std::numeric_limits<double>::max(), std::numeric_limits<double>::max() };
269 SpatialIndex::Region query( low, high, 2 );
271 other.mRTree->intersectsWithQuery( query, visitor );
274 ~QgsSpatialIndexData()
280 QgsSpatialIndexData &operator=(
const QgsSpatialIndexData &rh ) =
delete;
282 void initTree( IDataStream *inputStream =
nullptr )
285 mStorage = StorageManager::createNewMemoryStorageManager();
288 double fillFactor = 0.7;
289 unsigned long indexCapacity = 10;
290 unsigned long leafCapacity = 10;
291 unsigned long dimension = 2;
292 RTree::RTreeVariant variant = RTree::RV_RSTAR;
295 SpatialIndex::id_type indexId;
297 if ( inputStream && inputStream->hasNext() )
298 mRTree = RTree::createAndBulkLoadNewRTree( RTree::BLM_STR, *inputStream, *mStorage, fillFactor, indexCapacity,
299 leafCapacity, dimension, variant, indexId );
301 mRTree = RTree::createNewRTree( *mStorage, fillFactor, indexCapacity,
302 leafCapacity, dimension, variant, indexId );
306 SpatialIndex::IStorageManager *mStorage =
nullptr;
309 SpatialIndex::ISpatialIndex *mRTree =
nullptr;
311 mutable QMutex mMutex;
322 d =
new QgsSpatialIndexData( flags );
327 d =
new QgsSpatialIndexData( fi, feedback, flags );
346 if (
this != &other )
351 SpatialIndex::Region QgsSpatialIndex::rectToRegion(
const QgsRectangle &rect )
355 return SpatialIndex::Region( pt1, pt2, 2 );
361 if ( !featureInfo( f, rect,
id ) )
364 r = rectToRegion( rect );
382 if ( !featureInfo( feature, rect,
id ) )
389 QMutexLocker locker( &d->mMutex );
390 d->mGeometries.insert( feature.
id(), feature.
geometry() );
399 QgsFeatureList::iterator fIt = features.begin();
401 for ( ; fIt != features.end(); ++fIt )
421 SpatialIndex::Region r( rectToRegion( bounds ) );
423 QMutexLocker locker( &d->mMutex );
431 catch ( Tools::Exception &e )
434 QgsDebugMsg( QStringLiteral(
"Tools::Exception caught: " ).arg( e.what().c_str() ) );
436 catch (
const std::exception &e )
439 QgsDebugMsg( QStringLiteral(
"std::exception caught: " ).arg( e.what() ) );
443 QgsDebugMsg( QStringLiteral(
"unknown spatial index exception caught" ) );
451 SpatialIndex::Region r;
453 if ( !featureInfo( f, r,
id ) )
456 QMutexLocker locker( &d->mMutex );
459 d->mGeometries.remove( f.
id() );
465 QList<QgsFeatureId> list;
468 SpatialIndex::Region r = rectToRegion( rect );
470 QMutexLocker locker( &d->mMutex );
471 d->mRTree->intersectsWithQuery( r, visitor );
478 QList<QgsFeatureId> list;
481 double pt[2] = { point.
x(), point.
y() };
484 QMutexLocker locker( &d->mMutex );
486 point, maxDistance );
487 d->mRTree->nearestNeighborQuery( neighbors, p, visitor, nnc );
489 if ( maxDistance > 0 )
492 list.erase( std::remove_if( list.begin(), list.end(),
495 return nnc.mFeaturesOutsideMaxDistance.contains(
id );
504 QMutexLocker locker( &d->mMutex );
505 return d->mGeometries.value(
id );
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
QgsGeometry geometry(QgsFeatureId id) const
Returns the stored geometry for the indexed feature with matching id.
QgsSpatialIndex & operator=(const QgsSpatialIndex &other)
Implement assignment operator.
double distance(const QgsGeometry &geom) const
Returns the minimum distance between this geometry and another geometry, using GEOS.
QList< QgsFeature > QgsFeatureList
A class to represent a 2D point.
void visitNode(const INode &n) override
A geometry is the spatial representation of a feature.
Q_DECL_DEPRECATED bool insertFeature(const QgsFeature &feature)
Adds a feature to the index.
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 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
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void visitData(const IData &d) override
void visitData(std::vector< const IData *> &v) override
QgsSpatialIndex(QgsSpatialIndex::Flags flags=nullptr)
Constructor for QgsSpatialIndex.
void visitData(const IData &d) override
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
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.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
#define FID_TO_NUMBER(fid)
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a feature to the index.
QList< QgsFeatureId > nearestNeighbor(const QgsPointXY &point, int neighbors=1, double maxDistance=0) const
Returns nearest neighbors to a point.
~QgsSpatialIndex() override
Destructor finalizes work with spatial index.
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
Indicates that the spatial index should also store feature geometries. This requires more memory...
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!