33 QMutexLocker locker( &layer->mFeatureSourceConstructorMutex );
56 if ( L->editBuffer()->addedFeatures().contains( request.filterFid() ) )
57 mAddedFeatures.insert( request.filterFid(), L->editBuffer()->addedFeatures()[ request.filterFid()] );
59 if ( L->editBuffer()->changedGeometries().contains( request.filterFid() ) )
60 mChangedGeometries.insert( request.filterFid(), L->editBuffer()->changedGeometries()[ request.filterFid()] );
62 if ( L->editBuffer()->deletedFeatureIds().contains( request.filterFid() ) )
65 if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
66 mChangedAttributeValues.insert( request.filterFid(), L->editBuffer()->changedAttributeValues()[ request.filterFid()] );
68 if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
69 mChangedFeaturesRequest.setFilterFids(
QgsFeatureIds() << request.filterFid() );
120 , mFetchedFid( false )
174 QSet<int> providerSubset;
177 const auto constSubset = subset;
178 for (
int attrIndex : constSubset )
180 if ( attrIndex < 0 || attrIndex >= nPendingFields )
194 for (
int attrIndex : usedAttributeIndices )
196 providerSubset << attrIndex;
207 for (
const QString &field : constReferencedColumns )
235 changedIds << attIt.key();
304 if ( res && postProcessFeature( f ) )
378 if ( !postProcessFeature( f ) )
425 mInterruptionChecker = interruptionChecker;
451 if ( !postProcessFeature( f ) )
505 if ( postProcessFeature( f ) )
596 int sourceLayerIndex;
598 Q_ASSERT( joinInfo );
624 mFetchJoinInfo[ joinInfo ].attributes.push_back( sourceLayerIndex );
632 std::unique_ptr<QgsExpression> exp = qgis::make_unique<QgsExpression>( exps[oi].cachedExpression );
637 exp->setGeomCalculator( &da );
641 exp->prepare( mExpressionContext.get() );
642 const auto referencedColumns = exp->referencedColumns();
643 for (
const QString &col : referencedColumns )
652 for (
const QString &col : referencedColumns )
660 if ( !mPreparedFields.contains( dependentFieldIdx ) && !mFieldsToPrepare.contains( dependentFieldIdx ) )
661 mFieldsToPrepare << dependentFieldIdx;
664 if ( exp->needsGeometry() )
674 mPreparedFields.clear();
675 mFieldsToPrepare.clear();
677 mOrderedJoinInfoList.clear();
686 while ( !mFieldsToPrepare.isEmpty() )
688 int fieldIdx = mFieldsToPrepare.takeFirst();
689 if ( mPreparedFields.contains( fieldIdx ) )
692 mPreparedFields << fieldIdx;
699 createOrderedJoinList();
703 void QgsVectorLayerFeatureIterator::createOrderedJoinList()
706 if ( mOrderedJoinInfoList.size() < 2 )
711 QSet<int> resolvedFields;
714 QList< int >::const_iterator prepFieldIt = mPreparedFields.constBegin();
715 for ( ; prepFieldIt != mPreparedFields.constEnd(); ++prepFieldIt )
719 resolvedFields.insert( *prepFieldIt );
726 int maxIterations = ( mOrderedJoinInfoList.size() + 1 ) * mOrderedJoinInfoList.size() / 2.0;
727 int currentIteration = 0;
729 for (
int i = 0; i < mOrderedJoinInfoList.size() - 1; ++i )
731 if ( !resolvedFields.contains( mOrderedJoinInfoList.at( i ).targetField ) )
733 mOrderedJoinInfoList.append( mOrderedJoinInfoList.at( i ) );
734 mOrderedJoinInfoList.removeAt( i );
739 int offset = mOrderedJoinInfoList.at( i ).indexOffset;
740 int joinField = mOrderedJoinInfoList.at( i ).joinField;
743 for (
int n = 0; n < attributes.size(); n++ )
745 if ( n != joinField )
747 resolvedFields.insert( joinField < n ? n + offset - 1 : n + offset );
753 if ( currentIteration >= maxIterations )
760 bool QgsVectorLayerFeatureIterator::postProcessFeature(
QgsFeature &feature )
762 bool result = checkGeometryValidity( feature );
768 bool QgsVectorLayerFeatureIterator::checkGeometryValidity(
const QgsFeature &feature )
833 QList< FetchJoinInfo >::const_iterator joinIt = mOrderedJoinInfoList.constBegin();
834 for ( ; joinIt != mOrderedJoinInfoList.constEnd(); ++joinIt )
836 QVariant targetFieldValue = f.
attribute( joinIt->targetField );
837 if ( !targetFieldValue.isValid() )
840 const QHash< QString, QgsAttributes> &memoryCache = joinIt->joinInfo->cachedAttributes;
841 if ( memoryCache.isEmpty() )
842 joinIt->addJoinedAttributesDirect( f, targetFieldValue );
844 joinIt->addJoinedAttributesCached( f, targetFieldValue );
858 QList< int > fetchedVirtualAttributes;
860 QMap<const QgsVectorLayerJoinInfo *, FetchJoinInfo>::const_iterator joinIt =
mFetchJoinInfo.constBegin();
867 fetchedVirtualAttributes << joinIt->targetField;
880 if ( fetchedVirtualAttributes.contains( it.key() ) )
893 mExpressionContext->setFeature( f );
894 QVariant val = exp->
evaluate( mExpressionContext.get() );
906 Q_UNUSED( simplifyMethod )
912 Q_UNUSED( methodType )
919 const QHash<QString, QgsAttributes> &memoryCache = joinInfo->cachedAttributes;
920 QHash<QString, QgsAttributes>::const_iterator it = memoryCache.find( joinValue.toString() );
921 if ( it == memoryCache.constEnd() )
924 int index = indexOffset;
927 for (
int i = 0; i < featureAttributes.count(); ++i )
938 QString subsetString;
940 QString joinFieldName = joinInfo->joinFieldName();
942 subsetString.append( QStringLiteral(
"\"%1\"" ).arg( joinFieldName ) );
944 if ( joinValue.isNull() )
946 subsetString += QLatin1String(
" IS NULL" );
950 QString v = joinValue.toString();
951 switch ( joinValue.type() )
954 case QVariant::LongLong:
955 case QVariant::Double:
959 case QVariant::String:
960 v.replace(
'\'', QLatin1String(
"''" ) );
961 v.prepend(
'\'' ).append(
'\'' );
964 subsetString +=
'=' + v;
969 QVector<int> subsetIndices;
970 if ( joinInfo->hasSubset() )
988 int index = indexOffset;
990 if ( joinInfo->hasSubset() )
992 for (
int i = 0; i < subsetIndices.count(); ++i )
993 f.
setAttribute( index++, attr.at( subsetIndices.at( i ) ) );
998 for (
int i = 0; i < attr.count(); ++i )
1000 if ( i == joinField )
1034 if ( iter->id() == featureId )
1076 for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
1077 attrs[it.key()] = it.value();
1088 bool QgsVectorLayerFeatureIterator::prepareOrderBy(
const QList<QgsFeatureRequest::OrderByClause> &orderBys )
1090 Q_UNUSED( orderBys )
1101 , mSelectedFeatureIds( layer->selectedFeatureIds() )
1102 , mWkbType( layer->wkbType() )
1103 , mName( layer->name() )
1128 return QgsFeatureIterator(
new QgsVectorLayerSelectedFeatureIterator( mSelectedFeatureIds, req, mSource ) );
1133 return mSource.
crs();
1148 return mSelectedFeatureIds.count();
1159 return mLayer->createExpressionContextScope();
1171 , mSelectedFeatureIds( selectedFeatureIds )
1184 bool QgsVectorLayerSelectedFeatureIterator::rewind()
1186 return mIterator.
rewind();
1189 bool QgsVectorLayerSelectedFeatureIterator::close()
1191 return mIterator.close();
1194 bool QgsVectorLayerSelectedFeatureIterator::fetchFeature(
QgsFeature &f )
1196 while ( mIterator.nextFeature( f ) )
1198 if ( mSelectedFeatureIds.contains( f.
id() ) )
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QList< QgsExpressionFieldBuffer::ExpressionField > expressions() const
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.
void addJoinedAttributesDirect(QgsFeature &f, const QVariant &joinValue) const
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
void geometryToDestinationCrs(QgsFeature &feature, const QgsCoordinateTransform &transform) const
Transforms feature's geometry according to the specified coordinate transform.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
bool fetchNextAddedFeature(QgsFeature &f)
QgsVectorLayerJoinBuffer * mJoinBuffer
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system for features retrieved from this source.
bool containsJoins() const
Quick way to test if there is any join at all.
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
QSet< QgsFeatureId > QgsFeatureIds
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field's origin (value from an enumeration)
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.
const Flags & flags() const
void createJoinCaches()
Calls cacheJoinLayer() for all vector joins.
QgsFeatureIterator mProviderIterator
QMap< int, QgsExpression * > mExpressionFieldInfo
void addJoinedAttributes(QgsFeature &f)
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().
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
bool exists(int i) const
Returns if a field index is valid.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
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.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) override
Gets an iterator for features matching the specified request.
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
QVariant evaluate()
Evaluate the feature and return the result.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordi...
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.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
FilterType filterType() const
Returns the filter type which is currently set on this request.
Container of fields for a vector layer.
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
QSet< int > referencedAttributeIndexes(const QgsFields &fields) const
Returns a list of field name indexes obtained from the provided fields.
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.
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
QList< QgsField > mAddedAttributes
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
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.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsChangedAttributesMap changedAttributeValues() const
Returns a map of features with changed attributes values which are not committed. ...
const QgsAttributeList & attributeIndexes
bool hasGeometry() const
Returns true if the feature has an associated geometry.
QgsVectorLayer * joinLayer
Resolved pointer to the joined layer.
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
int count() const
Returns number of items.
QgsFeatureIds deletedFeatureIds() const
Returns a list of deleted feature IDs which are not committed.
It has not been specified where the field comes from.
QgsFields fields() const override
Returns the fields associated with features in the source.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
int joinField
Index of field (of the joined layer) must have equal value.
QgsFeatureRequest mChangedFeaturesRequest
QgsExpression * filterExpression() const
Returns the filter expression if set.
int fieldOriginIndex(int fieldIdx) const
Gets field's origin index (its meaning is specific to each type of origin)
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...
void updateChangedAttributes(QgsFeature &f)
Update feature with uncommitted attribute updates.
QgsExpressionContextScope mLayerScope
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets feature ID that should be fetched.
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
QgsGeometryMap changedGeometries() const
Returns a map of features with changed geometries which are not committed.
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()
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
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...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
bool isGeosValid(QgsGeometry::ValidityFlags flags=nullptr) const
Checks validity of the geometry using GEOS.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be nullptr if the reference was set by layer ID and not resolved yet) ...
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
QgsFields fields() const
Returns the fields that will be available for features that are retrieved from this source...
QgsFeatureRequest & disableFilter()
Disables filter conditions.
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
void addJoinedAttributesCached(QgsFeature &f, const QVariant &joinValue) const
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
bool fetchFeature(QgsFeature &feature) override
fetch next feature, return true on success
QgsAttributeList deletedAttributeIds() const
Returns a list of deleted attributes fields which are not committed.
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)
QgsGeometryMap mChangedGeometries
QgsWkbTypes::Type wkbType() const override
Returns the geometry type for features returned by this source.
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.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsExpressionFieldBuffer * mExpressionFieldBuffer
void prepareField(int fieldIdx)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QSet< QgsFeatureId > mFetchConsidered
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
Partial snapshot of vector layer's state (only the members necessary for access to features) ...
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...
QgsFeatureMap addedFeatures() const
Returns a map of new features which are not committed.
QgsFeatureRequest mRequest
A copy of the feature request.
int targetField
Index of field (of this layer) that drives the join.
Buffers information about expression fields for a vector layer.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
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
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).
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.
InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
const QgsVectorLayerJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
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.
Custom exception class for Coordinate Reference System related exceptions.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
QList< int > QgsAttributeList
QString id() const
Returns the layer id of the source layer.
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
OrderBy orderBy() const
Returns a list of order by clauses specified for this feature request.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
void prepareJoin(int fieldIdx)
QList< QgsField > addedAttributes() const
Returns a list of added attributes fields which are not committed.
Represents a vector layer which manages a vector based data sets.
bool isValid() const override
Returns if this iterator is valid.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Field is calculated from an expression.
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
QgsCoordinateTransform mTransform
QgsCoordinateTransformContext transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is requi...
virtual bool isValid() const
Will return if this iterator is valid.
bool isClosed() const
find out whether the iterator is still valid or closed already
QgsVectorLayerFeatureSource(const QgsVectorLayer *layer)
Constructor for QgsVectorLayerFeatureSource.
QStringList * joinFieldNamesSubset() const
Returns the subset of fields to be used from joined layer.
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
QgsCoordinateReferenceSystem crs
int joinedFieldsOffset(const QgsVectorLayerJoinInfo *info, const QgsFields &fields)
Find out what is the first index of the join within fields.
QgsVectorLayerJoinBuffer * clone() const
Create a copy of the join buffer.
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.
QSet< int > CORE_EXPORT usedAttributeIndices(const QgsFields &fields) const
Returns a set of used, validated attribute indices.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.