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 Q_FOREACH (
int attrIndex, subset )
179 if ( attrIndex < 0 || attrIndex >= nPendingFields )
232 changedIds << attIt.key();
301 if ( res && postProcessFeature( f ) )
375 if ( !postProcessFeature( f ) )
422 mInterruptionChecker = interruptionChecker;
448 if ( !postProcessFeature( f ) )
502 if ( postProcessFeature( f ) )
593 int sourceLayerIndex;
595 Q_ASSERT( joinInfo );
621 mFetchJoinInfo[ joinInfo ].attributes.push_back( sourceLayerIndex );
638 exp->
prepare( mExpressionContext.get() );
658 if ( !mPreparedFields.contains( dependentFieldIdx ) && !mFieldsToPrepare.contains( dependentFieldIdx ) )
659 mFieldsToPrepare << dependentFieldIdx;
670 mPreparedFields.clear();
671 mFieldsToPrepare.clear();
673 mOrderedJoinInfoList.clear();
682 while ( !mFieldsToPrepare.isEmpty() )
684 int fieldIdx = mFieldsToPrepare.takeFirst();
685 if ( mPreparedFields.contains( fieldIdx ) )
688 mPreparedFields << fieldIdx;
695 createOrderedJoinList();
699 void QgsVectorLayerFeatureIterator::createOrderedJoinList()
702 if ( mOrderedJoinInfoList.size() < 2 )
707 QSet<int> resolvedFields;
710 QList< int >::const_iterator prepFieldIt = mPreparedFields.constBegin();
711 for ( ; prepFieldIt != mPreparedFields.constEnd(); ++prepFieldIt )
715 resolvedFields.insert( *prepFieldIt );
722 int maxIterations = ( mOrderedJoinInfoList.size() + 1 ) * mOrderedJoinInfoList.size() / 2.0;
723 int currentIteration = 0;
725 for (
int i = 0; i < mOrderedJoinInfoList.size() - 1; ++i )
727 if ( !resolvedFields.contains( mOrderedJoinInfoList.at( i ).targetField ) )
729 mOrderedJoinInfoList.append( mOrderedJoinInfoList.at( i ) );
730 mOrderedJoinInfoList.removeAt( i );
735 int offset = mOrderedJoinInfoList.at( i ).indexOffset;
736 int joinField = mOrderedJoinInfoList.at( i ).joinField;
739 for (
int n = 0; n < attributes.size(); n++ )
741 if ( n != joinField )
743 resolvedFields.insert( joinField < n ? n + offset - 1 : n + offset );
749 if ( currentIteration >= maxIterations )
756 bool QgsVectorLayerFeatureIterator::postProcessFeature(
QgsFeature &feature )
758 bool result = checkGeometryValidity( feature );
764 bool QgsVectorLayerFeatureIterator::checkGeometryValidity(
const QgsFeature &feature )
829 QList< FetchJoinInfo >::const_iterator joinIt = mOrderedJoinInfoList.constBegin();
830 for ( ; joinIt != mOrderedJoinInfoList.constEnd(); ++joinIt )
832 QVariant targetFieldValue = f.
attribute( joinIt->targetField );
833 if ( !targetFieldValue.isValid() )
836 const QHash< QString, QgsAttributes> &memoryCache = joinIt->joinInfo->cachedAttributes;
837 if ( memoryCache.isEmpty() )
838 joinIt->addJoinedAttributesDirect( f, targetFieldValue );
840 joinIt->addJoinedAttributesCached( f, targetFieldValue );
854 QList< int > fetchedVirtualAttributes;
856 QMap<const QgsVectorLayerJoinInfo *, FetchJoinInfo>::const_iterator joinIt =
mFetchJoinInfo.constBegin();
863 fetchedVirtualAttributes << joinIt->targetField;
876 if ( fetchedVirtualAttributes.contains( it.key() ) )
889 mExpressionContext->setFeature( f );
890 QVariant val = exp->
evaluate( mExpressionContext.get() );
902 Q_UNUSED( simplifyMethod );
908 Q_UNUSED( methodType );
915 const QHash<QString, QgsAttributes> &memoryCache = joinInfo->cachedAttributes;
916 QHash<QString, QgsAttributes>::const_iterator it = memoryCache.find( joinValue.toString() );
917 if ( it == memoryCache.constEnd() )
920 int index = indexOffset;
923 for (
int i = 0; i < featureAttributes.count(); ++i )
934 QString subsetString;
936 QString joinFieldName = joinInfo->joinFieldName();
938 subsetString.append( QStringLiteral(
"\"%1\"" ).arg( joinFieldName ) );
940 if ( joinValue.isNull() )
942 subsetString += QLatin1String(
" IS NULL" );
946 QString v = joinValue.toString();
947 switch ( joinValue.type() )
950 case QVariant::LongLong:
951 case QVariant::Double:
955 case QVariant::String:
956 v.replace(
'\'', QLatin1String(
"''" ) );
957 v.prepend(
'\'' ).append(
'\'' );
960 subsetString +=
'=' + v;
965 QVector<int> subsetIndices;
966 if ( joinInfo->hasSubset() )
984 int index = indexOffset;
986 if ( joinInfo->hasSubset() )
988 for (
int i = 0; i < subsetIndices.count(); ++i )
989 f.
setAttribute( index++, attr.at( subsetIndices.at( i ) ) );
994 for (
int i = 0; i < attr.count(); ++i )
996 if ( i == joinField )
1030 if ( iter->id() == featureId )
1072 for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
1073 attrs[it.key()] = it.value();
1084 bool QgsVectorLayerFeatureIterator::prepareOrderBy(
const QList<QgsFeatureRequest::OrderByClause> &orderBys )
1086 Q_UNUSED( orderBys );
1097 , mSelectedFeatureIds( layer->selectedFeatureIds() )
1098 , mWkbType( layer->wkbType() )
1099 , mName( layer->name() )
1124 return QgsFeatureIterator(
new QgsVectorLayerSelectedFeatureIterator( mSelectedFeatureIds, req, mSource ) );
1129 return mSource.
crs();
1144 return mSelectedFeatureIds.count();
1155 return mLayer->createExpressionContextScope();
1167 , mSelectedFeatureIds( selectedFeatureIds )
1180 bool QgsVectorLayerSelectedFeatureIterator::rewind()
1182 return mIterator.
rewind();
1185 bool QgsVectorLayerSelectedFeatureIterator::close()
1187 return mIterator.close();
1190 bool QgsVectorLayerSelectedFeatureIterator::fetchFeature(
QgsFeature &f )
1192 while ( mIterator.nextFeature( f ) )
1194 if ( mSelectedFeatureIds.contains( f.
id() ) )
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
void setAreaUnits(QgsUnitTypes::AreaUnit unit)
Sets the desired areal units for calculations involving geomCalculator(), e.g., "$area".
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.
QSet< QString > CORE_EXPORT usedAttributes() const
Returns a set of used attributes.
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 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.
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.
bool isGeosValid() const
Checks validity of the geometry using GEOS.
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 cancelation 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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be null 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.
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.
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
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).
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 null.
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
Gets 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.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.