32 QMutexLocker locker( &layer->mFeatureSourceConstructorMutex );
55 if ( L->editBuffer()->addedFeatures().contains( request.filterFid() ) )
56 mAddedFeatures.insert( request.filterFid(), L->editBuffer()->addedFeatures()[ request.filterFid()] );
58 if ( L->editBuffer()->changedGeometries().contains( request.filterFid() ) )
59 mChangedGeometries.insert( request.filterFid(), L->editBuffer()->changedGeometries()[ request.filterFid()] );
61 if ( L->editBuffer()->deletedFeatureIds().contains( request.filterFid() ) )
64 if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
65 mChangedAttributeValues.insert( request.filterFid(), L->editBuffer()->changedAttributeValues()[ request.filterFid()] );
67 if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
68 mChangedFeaturesRequest.setFilterFids(
QgsFeatureIds() << request.filterFid() );
119 , mFetchedFid( false )
173 QSet<int> providerSubset;
176 Q_FOREACH (
int attrIndex, subset )
178 if ( attrIndex < 0 || attrIndex >= nPendingFields )
231 changedIds << attIt.key();
300 if ( res && postProcessFeature( f ) )
374 if ( !postProcessFeature( f ) )
421 mInterruptionChecker = interruptionChecker;
447 if ( !postProcessFeature( f ) )
501 if ( postProcessFeature( f ) )
592 int sourceLayerIndex;
594 Q_ASSERT( joinInfo );
620 mFetchJoinInfo[ joinInfo ].attributes.push_back( sourceLayerIndex );
637 exp->
prepare( mExpressionContext.get() );
657 if ( !mPreparedFields.contains( dependentFieldIdx ) && !mFieldsToPrepare.contains( dependentFieldIdx ) )
658 mFieldsToPrepare << dependentFieldIdx;
669 mPreparedFields.clear();
670 mFieldsToPrepare.clear();
672 mOrderedJoinInfoList.clear();
681 while ( !mFieldsToPrepare.isEmpty() )
683 int fieldIdx = mFieldsToPrepare.takeFirst();
684 if ( mPreparedFields.contains( fieldIdx ) )
687 mPreparedFields << fieldIdx;
694 createOrderedJoinList();
698 void QgsVectorLayerFeatureIterator::createOrderedJoinList()
701 if ( mOrderedJoinInfoList.size() < 2 )
706 QSet<int> resolvedFields;
709 QList< int >::const_iterator prepFieldIt = mPreparedFields.constBegin();
710 for ( ; prepFieldIt != mPreparedFields.constEnd(); ++prepFieldIt )
714 resolvedFields.insert( *prepFieldIt );
721 int maxIterations = ( mOrderedJoinInfoList.size() + 1 ) * mOrderedJoinInfoList.size() / 2.0;
722 int currentIteration = 0;
724 for (
int i = 0; i < mOrderedJoinInfoList.size() - 1; ++i )
726 if ( !resolvedFields.contains( mOrderedJoinInfoList.at( i ).targetField ) )
728 mOrderedJoinInfoList.append( mOrderedJoinInfoList.at( i ) );
729 mOrderedJoinInfoList.removeAt( i );
734 int offset = mOrderedJoinInfoList.at( i ).indexOffset;
735 int joinField = mOrderedJoinInfoList.at( i ).joinField;
738 for (
int n = 0; n < attributes.size(); n++ )
740 if ( n != joinField )
742 resolvedFields.insert( joinField < n ? n + offset - 1 : n + offset );
748 if ( currentIteration >= maxIterations )
755 bool QgsVectorLayerFeatureIterator::postProcessFeature(
QgsFeature &feature )
757 bool result = checkGeometryValidity( feature );
763 bool QgsVectorLayerFeatureIterator::checkGeometryValidity(
const QgsFeature &feature )
828 QList< FetchJoinInfo >::const_iterator joinIt = mOrderedJoinInfoList.constBegin();
829 for ( ; joinIt != mOrderedJoinInfoList.constEnd(); ++joinIt )
831 QVariant targetFieldValue = f.
attribute( joinIt->targetField );
832 if ( !targetFieldValue.isValid() )
835 const QHash< QString, QgsAttributes> &memoryCache = joinIt->joinInfo->cachedAttributes;
836 if ( memoryCache.isEmpty() )
837 joinIt->addJoinedAttributesDirect( f, targetFieldValue );
839 joinIt->addJoinedAttributesCached( f, targetFieldValue );
853 QList< int > fetchedVirtualAttributes;
855 QMap<const QgsVectorLayerJoinInfo *, FetchJoinInfo>::const_iterator joinIt =
mFetchJoinInfo.constBegin();
862 fetchedVirtualAttributes << joinIt->targetField;
875 if ( fetchedVirtualAttributes.contains( it.key() ) )
888 mExpressionContext->setFeature( f );
889 QVariant val = exp->
evaluate( mExpressionContext.get() );
901 Q_UNUSED( simplifyMethod );
907 Q_UNUSED( methodType );
914 const QHash<QString, QgsAttributes> &memoryCache = joinInfo->cachedAttributes;
915 QHash<QString, QgsAttributes>::const_iterator it = memoryCache.find( joinValue.toString() );
916 if ( it == memoryCache.constEnd() )
919 int index = indexOffset;
922 for (
int i = 0; i < featureAttributes.count(); ++i )
933 QString subsetString;
935 QString joinFieldName = joinInfo->joinFieldName();
937 subsetString.append( QStringLiteral(
"\"%1\"" ).arg( joinFieldName ) );
939 if ( joinValue.isNull() )
941 subsetString += QLatin1String(
" IS NULL" );
945 QString v = joinValue.toString();
946 switch ( joinValue.type() )
949 case QVariant::LongLong:
950 case QVariant::Double:
954 case QVariant::String:
955 v.replace(
'\'', QLatin1String(
"''" ) );
956 v.prepend(
'\'' ).append(
'\'' );
959 subsetString +=
'=' + v;
964 QVector<int> subsetIndices;
965 if ( joinInfo->hasSubset() )
983 int index = indexOffset;
985 if ( joinInfo->hasSubset() )
987 for (
int i = 0; i < subsetIndices.count(); ++i )
988 f.
setAttribute( index++, attr.at( subsetIndices.at( i ) ) );
993 for (
int i = 0; i < attr.count(); ++i )
995 if ( i == joinField )
1029 if ( iter->id() == featureId )
1071 for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
1072 attrs[it.key()] = it.value();
1083 bool QgsVectorLayerFeatureIterator::prepareOrderBy(
const QList<QgsFeatureRequest::OrderByClause> &orderBys )
1085 Q_UNUSED( orderBys );
1096 , mSelectedFeatureIds( layer->selectedFeatureIds() )
1097 , mWkbType( layer->wkbType() )
1098 , mName( layer->name() )
1123 return QgsFeatureIterator(
new QgsVectorLayerSelectedFeatureIterator( mSelectedFeatureIds, req, mSource ) );
1128 return mSource.
crs();
1143 return mSelectedFeatureIds.count();
1154 return mLayer->createExpressionContextScope();
1166 , mSelectedFeatureIds( selectedFeatureIds )
1179 bool QgsVectorLayerSelectedFeatureIterator::rewind()
1181 return mIterator.
rewind();
1184 bool QgsVectorLayerSelectedFeatureIterator::close()
1186 return mIterator.close();
1189 bool QgsVectorLayerSelectedFeatureIterator::fetchFeature(
QgsFeature &f )
1191 while ( mIterator.nextFeature( f ) )
1193 if ( mSelectedFeatureIds.contains( f.
id() ) )
void setAreaUnits(QgsUnitTypes::AreaUnit unit)
Sets the desired areal units for calculations involving geomCalculator(), e.g., "$area".
QgsAbstractFeatureSource * mProviderFeatureSource
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Returns an iterator for the features in the source.
Wrapper for iterator of features from vector data provider or vector layer.
void addJoinedAttributesDirect(QgsFeature &f, const QVariant &joinValue) const
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
bool isGeosValid(QgsGeometry::ValidityFlags flags=nullptr) const
Checks validity of the geometry using GEOS.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
bool fetchNextAddedFeature(QgsFeature &f)
QgsVectorLayerJoinBuffer * mJoinBuffer
const Flags & flags() const
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
QSet< QgsFeatureId > QgsFeatureIds
QgsFeatureIds deletedFeatureIds() const
Returns a list of deleted feature IDs which are not committed.
bool fetchNextChangedGeomFeature(QgsFeature &f)
void setInterruptionChecker(QgsFeedback *interruptionChecker) override
Attach an object that can be queried regularly by the iterator to check if it must stopped...
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
QString sourceName() const override
Returns a friendly display name for the source.
void createJoinCaches()
Calls cacheJoinLayer() for all vector joins.
QgsFeatureIterator mProviderIterator
QMap< int, QgsExpression * > mExpressionFieldInfo
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
void addJoinedAttributes(QgsFeature &f)
const QgsVectorLayerJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
void addExpressionAttribute(QgsFeature &f, int attrIndex)
Adds an expression based attribute to a feature.
QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt
QgsGeometryMap::ConstIterator mFetchChangedGeomIt
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const SIP_THROW(QgsCsException)
Returns a rectangle representing the original request's QgsFeatureRequest::filterRect().
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
virtual bool isValid() const
Will return if this iterator is valid.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
QSet< int > referencedAttributeIndexes(const QgsFields &fields) const
Returns a list of field name indexes obtained from the provided fields.
QgsVectorLayerFeatureIterator(QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request)
QgsVectorLayerSelectedFeatureSource(QgsVectorLayer *layer)
Constructor for QgsVectorLayerSelectedFeatureSource, for selected features from the specified layer...
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) override
Gets an iterator for features matching the specified request.
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
QVariant evaluate()
Evaluate the feature and return the result.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system for features retrieved from this source.
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
QList< QgsField > mAddedAttributes
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
bool mClosed
Sets to true, as soon as the iterator is closed.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
const QgsAttributeList & attributeIndexes
QgsVectorLayer * joinLayer
Resolved pointer to the joined layer.
QgsFields fields() const
Returns the fields that will be available for features that are retrieved from this source...
bool isClosed() const
find out whether the iterator is still valid or closed already
It has not been specified where the field comes from.
QgsFields fields() const override
Returns the fields associated with features in the source.
QgsAttributeList deletedAttributeIds() const
Returns a list of deleted attributes fields which are not committed.
QgsVectorLayerJoinBuffer * clone() const
Create a copy of the join buffer.
int joinField
Index of field (of the joined layer) must have equal value.
QgsFeatureRequest mChangedFeaturesRequest
virtual QgsAbstractFeatureSource * featureSource() const =0
Returns feature source object that can be used for querying provider's data.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
bool containsJoins() const
Quick way to test if there is any join at all.
void updateChangedAttributes(QgsFeature &f)
Update feature with uncommitted attribute updates.
QgsExpressionContextScope mLayerScope
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets feature ID that should be fetched.
bool exists(int i) const
Returns if a field index is valid.
InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
~QgsVectorLayerFeatureIterator() override
Type
The WKB type describes the number of dimensions a geometry has.
bool fetchNextChangedAttributeFeature(QgsFeature &f)
void iteratorClosed()
to be called by from subclass in close()
QgsVectorLayerFeatureSource * mSource
void useAddedFeature(const QgsFeature &src, QgsFeature &f)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QStringList * joinFieldNamesSubset() const
Gets subset of fields to be used from joined layer.
QgsFeatureRequest & disableFilter()
Disables filter conditions.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
Internal feature iterator to be implemented within data providers.
void updateFeatureGeometry(QgsFeature &f)
Update feature with uncommitted geometry updates.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool prepareSimplification(const QgsSimplifyMethod &simplifyMethod) override
Setup the simplification of geometries to fetch using the specified simplify method.
friend class QgsVectorLayerFeatureIterator
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Defines left outer join from our vector layer to some other vector layer.
void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
QMap< int, QVariant > QgsAttributeMap
int fieldOriginIndex(int fieldIdx) const
Gets field's origin index (its meaning is specific to each type of origin)
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
bool fetchFeature(QgsFeature &feature) override
fetch next feature, return true on success
QgsExpressionContextScope * createExpressionContextScope() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFeatureRequest mProviderRequest
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
void prepareExpression(int fieldIdx)
QSet< QString > CORE_EXPORT usedAttributes() const
Returns a set of used attributes.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QgsGeometryMap mChangedGeometries
int count() const
Returns number of items.
QgsWkbTypes::Type wkbType() const override
Returns the geometry type for features returned by this source.
FilterType filterType() const
Returns the filter type which is currently set on this request.
QgsFeatureIds mDeletedFeatureIds
QgsAttributeList mDeletedAttributeIds
No invalid geometry checking.
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
void setId(QgsFeatureId id)
Sets the feature ID for this feature.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsExpressionFieldBuffer * mExpressionFieldBuffer
void prepareField(int fieldIdx)
QgsGeometryMap changedGeometries() const
Returns a map of features with changed geometries which are not committed.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
void geometryToDestinationCrs(QgsFeature &feature, const QgsCoordinateTransform &transform) const
Transforms feature's geometry according to the specified coordinate transform.
QSet< QgsFeatureId > mFetchConsidered
QgsCoordinateTransformContext transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is requi...
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsFeatureMap addedFeatures() const
Returns a map of new features which are not committed.
Partial snapshot of vector layer's state (only the members necessary for access to features) ...
QString id() const
Returns the layer id of the source layer.
bool nextFeatureFid(QgsFeature &f)
int indexOffset
At what position the joined fields start.
QgsCoordinateTransformContext transformContext
void useChangedAttributeFeature(QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f)
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void setValid(bool validity)
Sets the validity of the feature.
~QgsVectorLayerFeatureSource() override
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
long featureCount() const override
Returns the number of features contained in the source, or -1 if the feature count is unknown...
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QgsFeatureRequest mRequest
A copy of the feature request.
QgsChangedAttributesMap changedAttributeValues() const
Returns a map of features with changed attributes values which are not committed. ...
void setGeomCalculator(const QgsDistanceArea *calc)
Sets the geometry calculator used for distance and area calculations in expressions.
int targetField
Index of field (of this layer) that drives the join.
Buffers information about expression fields for a vector layer.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QList< QgsField > addedAttributes() const
Returns a list of added attributes fields which are not committed.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
QgsFeatureMap mAddedFeatures
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool rewind() override
reset the iterator to the starting position
void setDistanceUnits(QgsUnitTypes::DistanceUnit unit)
Sets the desired distance units for calculations involving geomCalculator(), e.g., "$length" and "$perimeter".
static QgsProject * instance()
Returns the QgsProject singleton instance.
QMap< const QgsVectorLayerJoinInfo *, QgsVectorLayerFeatureIterator::FetchJoinInfo > mFetchJoinInfo
Information about joins used in the current select() statement.
This class represents a coordinate reference system (CRS).
QList< QgsExpressionFieldBuffer::ExpressionField > expressions() const
bool close() override
end of iterating: free the resources / lock
static QVector< int > joinSubsetIndices(QgsVectorLayer *joinLayer, const QStringList &joinFieldsSubset)
Returns a vector of indices for use in join based on field names from the layer.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field's origin (value from an enumeration)
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
OrderBy orderBy() const
Returns a list of order by clauses specified for this feature request.
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
QgsVectorLayer * joinLayer() const
Returns joined layer (may be null if the reference was set by layer ID and not resolved yet) ...
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
QgsCoordinateReferenceSystem mCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
void addJoinedAttributesCached(QgsFeature &f, const QVariant &joinValue) const
Custom exception class for Coordinate Reference System related exceptions.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider.
QList< int > QgsAttributeList
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
bool nextFeature(QgsFeature &f)
bool mHasVirtualAttributes
QgsChangedAttributesMap mChangedAttributeValues
Geometry is not required. It may still be returned if e.g. required for a filter condition.
void prepareJoin(int fieldIdx)
Represents a vector layer which manages a vector based data sets.
bool isValid() const override
Returns if this iterator is valid.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordi...
Field is calculated from an expression.
QgsCoordinateTransform mTransform
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
QgsVectorLayerFeatureSource(const QgsVectorLayer *layer)
Constructor for QgsVectorLayerFeatureSource.
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
QgsCoordinateReferenceSystem crs
int joinedFieldsOffset(const QgsVectorLayerJoinInfo *info, const QgsFields &fields)
Find out what is the first index of the join within fields.
void addVirtualAttributes(QgsFeature &f)
Adds attributes that don't source from the provider but are added inside QGIS Includes.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureIterator mChangedFeaturesIterator
Helper template that cares of two things: 1.
QgsExpression * filterExpression() const
Returns the filter expression if set.