29QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource *source,
bool ownSource,
const QgsFeatureRequest &request )
32 if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mSource->mCrs )
34 mTransform =
QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs(), mRequest.transformContext() );
38 mFilterRect = filterRectToSourceCrs( mTransform );
47 if ( !mSource->mSubsetString.isEmpty() )
49 mSubsetExpression = std::make_unique< QgsExpression >( mSource->mSubsetString );
50 mSubsetExpression->prepare( mSource->expressionContext() );
54 switch ( mRequest.spatialFilterType() )
64 mSelectRectEngine->prepareGeometry();
69 if ( !mRequest.referenceGeometry().isEmpty() )
71 mDistanceWithinGeom = mRequest.referenceGeometry();
73 mDistanceWithinEngine->prepareGeometry();
80 if ( !mFilterRect.isNull() && mSource->mSpatialIndex )
82 mUsingFeatureIdList =
true;
83 mFeatureIdList = mSource->mSpatialIndex->intersects( mFilterRect );
84 QgsDebugMsgLevel(
"Features returned by spatial index: " + QString::number( mFeatureIdList.count() ), 2 );
88 mUsingFeatureIdList =
true;
89 const QgsFeatureMap::const_iterator it = mSource->mFeatures.constFind( mRequest.filterFid() );
90 if ( it != mSource->mFeatures.constEnd() )
91 mFeatureIdList.append( mRequest.filterFid() );
95 mUsingFeatureIdList =
true;
97 mFeatureIdList = QList<QgsFeatureId>( filterFids.begin(), filterFids.end() );
101 mUsingFeatureIdList =
false;
107QgsMemoryFeatureIterator::~QgsMemoryFeatureIterator()
112bool QgsMemoryFeatureIterator::fetchFeature(
QgsFeature &feature )
119 if ( mUsingFeatureIdList )
120 return nextFeatureUsingList( feature );
122 return nextFeatureTraverseAll( feature );
126bool QgsMemoryFeatureIterator::nextFeatureUsingList(
QgsFeature &feature )
128 bool hasFeature =
false;
131 while ( mFeatureIdListIterator != mFeatureIdList.constEnd() )
133 feature = mSource->mFeatures.value( *mFeatureIdListIterator );
134 if ( !mFilterRect.isNull() )
142 else if ( mSource->mSpatialIndex )
160 if ( hasFeature && mSubsetExpression )
162 mSource->expressionContext()->setFeature( feature );
163 if ( !mSubsetExpression->evaluate( mSource->expressionContext() ).toBool() )
170 geometryToDestinationCrs( feature, mTransform );
175 hasFeature = mDistanceWithinEngine->distance( feature.
geometry().
constGet() ) <= mRequest.distanceWithin();
178 ++mFeatureIdListIterator;
193bool QgsMemoryFeatureIterator::nextFeatureTraverseAll(
QgsFeature &feature )
195 bool hasFeature =
false;
198 while ( mSelectIterator != mSource->mFeatures.constEnd() )
201 feature = *mSelectIterator;
202 if ( mFilterRect.isNull() )
223 if ( hasFeature && mSubsetExpression )
225 mSource->expressionContext()->setFeature( feature );
226 if ( !mSubsetExpression->evaluate( mSource->expressionContext() ).toBool() )
233 geometryToDestinationCrs( feature, mTransform );
238 hasFeature = mDistanceWithinEngine->distance( feature.
geometry().
constGet() ) <= mRequest.distanceWithin();
261bool QgsMemoryFeatureIterator::rewind()
266 if ( mUsingFeatureIdList )
267 mFeatureIdListIterator = mFeatureIdList.constBegin();
269 mSelectIterator = mSource->mFeatures.constBegin();
274bool QgsMemoryFeatureIterator::close()
287QgsMemoryFeatureSource::QgsMemoryFeatureSource(
const QgsMemoryProvider *p )
288 : mFields( p->mFields )
289 , mFeatures( p->mFeatures )
290 , mSpatialIndex( p->mSpatialIndex ? std::make_unique<
QgsSpatialIndex >( *p->mSpatialIndex ) : nullptr )
291 , mSubsetString( p->mSubsetString )
305 if ( !mExpressionContext )
307 mExpressionContext = std::make_unique< QgsExpressionContext >(
308 QList<QgsExpressionContextScope *>()
311 mExpressionContext->setFields( mFields );
313 return mExpressionContext.get();
@ 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).
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
@ FilterFid
Filter using feature ID.
@ FilterFids
Filter using feature IDs.
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.
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool boundingBoxIntersects(const QgsRectangle &rectangle) const
Returns true if the bounding box of this geometry intersects with a rectangle.
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine representing the specified geometry.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool intersects(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle intersects with other rectangle.
A spatial index for QgsFeature objects.
QSet< QgsFeatureId > QgsFeatureIds
#define QgsDebugMsgLevel(str, level)