28QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource *source,
bool ownSource,
const QgsFeatureRequest &request )
31 mTransform = mRequest.calculateTransform( mSource->mCrs );
35 mFilterRect = filterRectToSourceCrs( mTransform );
44 if ( !mSource->mSubsetString.isEmpty() )
46 mSubsetExpression = std::make_unique< QgsExpression >( mSource->mSubsetString );
47 mSubsetExpression->prepare( mSource->expressionContext() );
51 switch ( mRequest.spatialFilterType() )
61 mSelectRectEngine->prepareGeometry();
66 if ( !mRequest.referenceGeometry().isEmpty() )
68 mDistanceWithinGeom = mRequest.referenceGeometry();
70 mDistanceWithinEngine->prepareGeometry();
77 if ( !mFilterRect.isNull() && mSource->mSpatialIndex )
79 mUsingFeatureIdList =
true;
80 mFeatureIdList = mSource->mSpatialIndex->intersects( mFilterRect );
81 QgsDebugMsgLevel(
"Features returned by spatial index: " + QString::number( mFeatureIdList.count() ), 2 );
85 mUsingFeatureIdList =
true;
86 const QgsFeatureMap::const_iterator it = mSource->mFeatures.constFind( mRequest.filterFid() );
87 if ( it != mSource->mFeatures.constEnd() )
88 mFeatureIdList.append( mRequest.filterFid() );
92 mUsingFeatureIdList =
true;
94 mFeatureIdList = QList<QgsFeatureId>( filterFids.begin(), filterFids.end() );
98 mUsingFeatureIdList =
false;
104QgsMemoryFeatureIterator::~QgsMemoryFeatureIterator()
109bool QgsMemoryFeatureIterator::fetchFeature(
QgsFeature &feature )
116 if ( mUsingFeatureIdList )
117 return nextFeatureUsingList( feature );
119 return nextFeatureTraverseAll( feature );
123bool QgsMemoryFeatureIterator::nextFeatureUsingList(
QgsFeature &feature )
125 bool hasFeature =
false;
128 while ( mFeatureIdListIterator != mFeatureIdList.constEnd() )
130 feature = mSource->mFeatures.value( *mFeatureIdListIterator );
131 if ( !mFilterRect.isNull() )
139 else if ( mSource->mSpatialIndex )
157 if ( hasFeature && mSubsetExpression )
159 mSource->expressionContext()->setFeature( feature );
160 if ( !mSubsetExpression->evaluate( mSource->expressionContext() ).toBool() )
167 geometryToDestinationCrs( feature, mTransform );
172 hasFeature = mDistanceWithinEngine->distance( feature.
geometry().
constGet() ) <= mRequest.distanceWithin();
175 ++mFeatureIdListIterator;
190bool QgsMemoryFeatureIterator::nextFeatureTraverseAll(
QgsFeature &feature )
192 bool hasFeature =
false;
195 while ( mSelectIterator != mSource->mFeatures.constEnd() )
198 feature = *mSelectIterator;
199 if ( mFilterRect.isNull() )
220 if ( hasFeature && mSubsetExpression )
222 mSource->expressionContext()->setFeature( feature );
223 if ( !mSubsetExpression->evaluate( mSource->expressionContext() ).toBool() )
230 geometryToDestinationCrs( feature, mTransform );
235 hasFeature = mDistanceWithinEngine->distance( feature.
geometry().
constGet() ) <= mRequest.distanceWithin();
258bool QgsMemoryFeatureIterator::rewind()
263 if ( mUsingFeatureIdList )
264 mFeatureIdListIterator = mFeatureIdList.constBegin();
266 mSelectIterator = mSource->mFeatures.constBegin();
271bool QgsMemoryFeatureIterator::close()
284QgsMemoryFeatureSource::QgsMemoryFeatureSource(
const QgsMemoryProvider *p )
285 : mFields( p->mFields )
286 , mFeatures( p->mFeatures )
287 , mSpatialIndex( p->mSpatialIndex ? std::make_unique<
QgsSpatialIndex >( *p->mSpatialIndex ) : nullptr )
288 , mSubsetString( p->mSubsetString )
302 if ( !mExpressionContext )
304 mExpressionContext = std::make_unique< QgsExpressionContext >(
305 QList<QgsExpressionContextScope *>()
308 mExpressionContext->setFields( mFields );
310 return mExpressionContext.get();
@ Fid
Filter using feature ID.
@ Fids
Filter using feature IDs.
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
@ DistanceWithin
Filter by distance to reference geometry.
@ BoundingBox
Filter using a bounding box.
@ NoFilter
No spatial filtering of features.
Helper template that cares of two things: 1.
Custom exception class for Coordinate Reference System related exceptions.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
void setValid(bool validity)
Sets the validity of the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
bool boundingBoxIntersects(const QgsRectangle &rectangle) const
Returns true if the bounding box of this geometry intersects with a rectangle.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
A spatial index for QgsFeature objects.
QSet< QgsFeatureId > QgsFeatureIds
#define QgsDebugMsgLevel(str, level)