28QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource *source,
bool ownSource,
const QgsFeatureRequest &request )
31 if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mSource->mCrs )
33 mTransform =
QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs(), mRequest.transformContext() );
37 mFilterRect = filterRectToSourceCrs( mTransform );
46 if ( !mSource->mSubsetString.isEmpty() )
48 mSubsetExpression = std::make_unique< QgsExpression >( mSource->mSubsetString );
49 mSubsetExpression->prepare( mSource->expressionContext() );
53 switch ( mRequest.spatialFilterType() )
63 mSelectRectEngine->prepareGeometry();
68 if ( !mRequest.referenceGeometry().isEmpty() )
70 mDistanceWithinGeom = mRequest.referenceGeometry();
72 mDistanceWithinEngine->prepareGeometry();
79 if ( !mFilterRect.isNull() && mSource->mSpatialIndex )
81 mUsingFeatureIdList =
true;
82 mFeatureIdList = mSource->mSpatialIndex->intersects( mFilterRect );
83 QgsDebugMsgLevel(
"Features returned by spatial index: " + QString::number( mFeatureIdList.count() ), 2 );
87 mUsingFeatureIdList =
true;
88 const QgsFeatureMap::const_iterator it = mSource->mFeatures.constFind( mRequest.filterFid() );
89 if ( it != mSource->mFeatures.constEnd() )
90 mFeatureIdList.append( mRequest.filterFid() );
94 mUsingFeatureIdList =
true;
96 mFeatureIdList = QList<QgsFeatureId>( filterFids.begin(), filterFids.end() );
100 mUsingFeatureIdList =
false;
106QgsMemoryFeatureIterator::~QgsMemoryFeatureIterator()
111bool QgsMemoryFeatureIterator::fetchFeature(
QgsFeature &feature )
118 if ( mUsingFeatureIdList )
119 return nextFeatureUsingList( feature );
121 return nextFeatureTraverseAll( feature );
125bool QgsMemoryFeatureIterator::nextFeatureUsingList(
QgsFeature &feature )
127 bool hasFeature =
false;
130 while ( mFeatureIdListIterator != mFeatureIdList.constEnd() )
132 feature = mSource->mFeatures.value( *mFeatureIdListIterator );
133 if ( !mFilterRect.isNull() )
141 else if ( mSource->mSpatialIndex )
159 if ( hasFeature && mSubsetExpression )
161 mSource->expressionContext()->setFeature( feature );
162 if ( !mSubsetExpression->evaluate( mSource->expressionContext() ).toBool() )
169 geometryToDestinationCrs( feature, mTransform );
174 hasFeature = mDistanceWithinEngine->distance( feature.
geometry().
constGet() ) <= mRequest.distanceWithin();
177 ++mFeatureIdListIterator;
192bool QgsMemoryFeatureIterator::nextFeatureTraverseAll(
QgsFeature &feature )
194 bool hasFeature =
false;
197 while ( mSelectIterator != mSource->mFeatures.constEnd() )
200 feature = *mSelectIterator;
201 if ( mFilterRect.isNull() )
222 if ( hasFeature && mSubsetExpression )
224 mSource->expressionContext()->setFeature( feature );
225 if ( !mSubsetExpression->evaluate( mSource->expressionContext() ).toBool() )
232 geometryToDestinationCrs( feature, mTransform );
237 hasFeature = mDistanceWithinEngine->distance( feature.
geometry().
constGet() ) <= mRequest.distanceWithin();
260bool QgsMemoryFeatureIterator::rewind()
265 if ( mUsingFeatureIdList )
266 mFeatureIdListIterator = mFeatureIdList.constBegin();
268 mSelectIterator = mSource->mFeatures.constBegin();
273bool QgsMemoryFeatureIterator::close()
286QgsMemoryFeatureSource::QgsMemoryFeatureSource(
const QgsMemoryProvider *p )
287 : mFields( p->mFields )
288 , mFeatures( p->mFeatures )
289 , mSpatialIndex( p->mSpatialIndex ? std::make_unique<
QgsSpatialIndex >( *p->mSpatialIndex ) : nullptr )
290 , mSubsetString( p->mSubsetString )
304 if ( !mExpressionContext )
306 mExpressionContext = std::make_unique< QgsExpressionContext >(
307 QList<QgsExpressionContextScope *>()
310 mExpressionContext->setFields( mFields );
312 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)
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)