32 QMutexLocker locker( &layer->mFeatureSourceConstructorMutex );
54 if ( L->editBuffer()->addedFeatures().contains( request.filterFid() ) )
55 mAddedFeatures.insert( request.filterFid(), L->editBuffer()->addedFeatures()[ request.filterFid()] );
57 if ( L->editBuffer()->changedGeometries().contains( request.filterFid() ) )
58 mChangedGeometries.insert( request.filterFid(), L->editBuffer()->changedGeometries()[ request.filterFid()] );
60 if ( L->editBuffer()->deletedFeatureIds().contains( request.filterFid() ) )
63 if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
64 mChangedAttributeValues.insert( request.filterFid(), L->editBuffer()->changedAttributeValues()[ request.filterFid()] );
66 if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
67 mChangedFeaturesRequest.setFilterFids(
QgsFeatureIds() << request.filterFid() );
113 , mFetchedFid( false )
167 QSet<int> providerSubset;
170 Q_FOREACH (
int attrIndex, subset )
172 if ( attrIndex < 0 || attrIndex >= nPendingFields )
218 changedIds << attIt.key();
287 if ( res && postProcessFeature( f ) )
361 if ( !postProcessFeature( f ) )
408 mInterruptionChecker = interruptionChecker;
434 if ( !postProcessFeature( f ) )
488 if ( postProcessFeature( f ) )
579 int sourceLayerIndex;
581 Q_ASSERT( joinInfo );
607 mFetchJoinInfo[ joinInfo ].attributes.push_back( sourceLayerIndex );
615 QgsExpression *exp =
new QgsExpression( exps[oi].cachedExpression );
620 exp->setGeomCalculator( &da );
624 exp->prepare( mExpressionContext.get() );
625 Q_FOREACH (
const QString &col, exp->referencedColumns() )
636 Q_FOREACH (
const QString &col, exp->referencedColumns() )
644 if ( !mPreparedFields.contains( dependentFieldIdx ) && !mFieldsToPrepare.contains( dependentFieldIdx ) )
645 mFieldsToPrepare << dependentFieldIdx;
648 if ( exp->needsGeometry() )
656 mPreparedFields.clear();
657 mFieldsToPrepare.clear();
659 mOrderedJoinInfoList.clear();
668 while ( !mFieldsToPrepare.isEmpty() )
670 int fieldIdx = mFieldsToPrepare.takeFirst();
671 if ( mPreparedFields.contains( fieldIdx ) )
674 mPreparedFields << fieldIdx;
681 createOrderedJoinList();
685 void QgsVectorLayerFeatureIterator::createOrderedJoinList()
688 if ( mOrderedJoinInfoList.size() < 2 )
693 QSet<int> resolvedFields;
696 QList< int >::const_iterator prepFieldIt = mPreparedFields.constBegin();
697 for ( ; prepFieldIt != mPreparedFields.constEnd(); ++prepFieldIt )
701 resolvedFields.insert( *prepFieldIt );
708 int maxIterations = ( mOrderedJoinInfoList.size() + 1 ) * mOrderedJoinInfoList.size() / 2.0;
709 int currentIteration = 0;
711 for (
int i = 0; i < mOrderedJoinInfoList.size() - 1; ++i )
713 if ( !resolvedFields.contains( mOrderedJoinInfoList.at( i ).targetField ) )
715 mOrderedJoinInfoList.append( mOrderedJoinInfoList.at( i ) );
716 mOrderedJoinInfoList.removeAt( i );
721 int offset = mOrderedJoinInfoList.at( i ).indexOffset;
722 int joinField = mOrderedJoinInfoList.at( i ).joinField;
725 for (
int n = 0; n < attributes.size(); n++ )
727 if ( n != joinField )
729 resolvedFields.insert( joinField < n ? n + offset - 1 : n + offset );
735 if ( currentIteration >= maxIterations )
742 bool QgsVectorLayerFeatureIterator::postProcessFeature(
QgsFeature &feature )
744 bool result = checkGeometryValidity( feature );
750 bool QgsVectorLayerFeatureIterator::checkGeometryValidity(
const QgsFeature &feature )
815 QList< FetchJoinInfo >::const_iterator joinIt = mOrderedJoinInfoList.constBegin();
816 for ( ; joinIt != mOrderedJoinInfoList.constEnd(); ++joinIt )
818 QVariant targetFieldValue = f.
attribute( joinIt->targetField );
819 if ( !targetFieldValue.isValid() )
822 const QHash< QString, QgsAttributes> &memoryCache = joinIt->joinInfo->cachedAttributes;
823 if ( memoryCache.isEmpty() )
824 joinIt->addJoinedAttributesDirect( f, targetFieldValue );
826 joinIt->addJoinedAttributesCached( f, targetFieldValue );
840 QList< int > fetchedVirtualAttributes;
842 QMap<const QgsVectorLayerJoinInfo *, FetchJoinInfo>::const_iterator joinIt =
mFetchJoinInfo.constBegin();
849 fetchedVirtualAttributes << joinIt->targetField;
862 if ( fetchedVirtualAttributes.contains( it.key() ) )
875 mExpressionContext->setFeature( f );
876 QVariant val = exp->evaluate( mExpressionContext.get() );
888 Q_UNUSED( simplifyMethod );
894 Q_UNUSED( methodType );
901 const QHash<QString, QgsAttributes> &memoryCache = joinInfo->cachedAttributes;
902 QHash<QString, QgsAttributes>::const_iterator it = memoryCache.find( joinValue.toString() );
903 if ( it == memoryCache.constEnd() )
906 int index = indexOffset;
909 for (
int i = 0; i < featureAttributes.count(); ++i )
920 QString subsetString;
922 QString joinFieldName = joinInfo->joinFieldName();
924 subsetString.append( QStringLiteral(
"\"%1\"" ).arg( joinFieldName ) );
926 if ( joinValue.isNull() )
928 subsetString += QLatin1String(
" IS NULL" );
932 QString v = joinValue.toString();
933 switch ( joinValue.type() )
936 case QVariant::LongLong:
937 case QVariant::Double:
941 case QVariant::String:
942 v.replace(
'\'', QLatin1String(
"''" ) );
943 v.prepend(
'\'' ).append(
'\'' );
946 subsetString +=
'=' + v;
951 QVector<int> subsetIndices;
952 if ( joinInfo->hasSubset() )
970 int index = indexOffset;
972 if ( joinInfo->hasSubset() )
974 for (
int i = 0; i < subsetIndices.count(); ++i )
975 f.
setAttribute( index++, attr.at( subsetIndices.at( i ) ) );
980 for (
int i = 0; i < attr.count(); ++i )
982 if ( i == joinField )
1016 if ( iter->id() == featureId )
1058 for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
1059 attrs[it.key()] = it.value();
1070 bool QgsVectorLayerFeatureIterator::prepareOrderBy(
const QList<QgsFeatureRequest::OrderByClause> &orderBys )
1072 Q_UNUSED( orderBys );
1083 , mSelectedFeatureIds( layer->selectedFeatureIds() )
1084 , mWkbType( layer->wkbType() )
1085 , mName( layer->name() )
1109 return mSource.
crs();
1124 return mSelectedFeatureIds.count();
1135 return mLayer->createExpressionContextScope();
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
QList< QgsExpressionFieldBuffer::ExpressionField > expressions() const
QgsAbstractFeatureSource * mProviderFeatureSource
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...
FieldOrigin fieldOrigin(int fieldIdx) const
Get 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
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
bool exists(int i) const
Return if a field index is valid.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QSet< QgsFeatureId > QgsFeatureIds
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
Get an iterator for features matching the specified request.
QgsFeatureId filterFid() const
Get the feature ID that should be fetched.
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const
Returns a rectangle representing the original request's QgsFeatureRequest::filterRect().
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
Get an iterator for features matching the specified request.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
FilterType filterType() const
Return the filter type which is currently set on this request.
Container of fields for a vector layer.
const QgsFeatureIds & filterFids() const
Get feature IDs that should be fetched.
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...
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
Set 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...
QgsChangedAttributesMap changedAttributeValues() const
Returns a map of features with changed attributes values which are not committed. ...
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
Return number of items.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
QgsCoordinateTransformContext transformContext() const
Returns a copy of the project's coordinate transform context, which stores various information regard...
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
Get 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
Get field's origin index (its meaning is specific to each type of origin)
virtual QgsAbstractFeatureSource * featureSource() const =0
Return 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)
Set 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
Get 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...
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 override
Returns the list of fields of this layer.
QgsFields fields() const
Returns the fields that will be available for features that are retrieved from this source...
QgsFeatureRequest & disableFilter()
Disables filter conditions.
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
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.
QgsCoordinateReferenceSystem crs() const
Returns the layer's spatial reference system.
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
QgsGeometry geometry() const
Returns the geometry associated with this feature.
void prepareField(int fieldIdx)
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QSet< QgsFeatureId > mFetchConsidered
QgsAttributeList subsetOfAttributes() const
Return 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.
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)
Set feature IDs that should be fetched.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
QgsFeatureMap mAddedFeatures
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)
Return 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.
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
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.
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
OrderBy orderBy() const
Return 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
Get 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()...
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)
Set 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.