QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvectorlayerfeatureiterator.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerfeatureiterator.h
3  ---------------------
4  begin : Dezember 2012
5  copyright : (C) 2012 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSVECTORLAYERFEATUREITERATOR_H
16 #define QGSVECTORLAYERFEATUREITERATOR_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include "qgsfeatureiterator.h"
21 #include "qgsfields.h"
23 #include "qgsfeaturesource.h"
25 #include "qgscoordinatetransform.h"
26 
27 #include <QPointer>
28 #include <QSet>
29 #include <memory>
30 
31 typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap SIP_SKIP;
32 
34 class QgsVectorLayer;
39 
41 
42 #ifdef SIP_RUN
43 % ModuleHeaderCode
44 #include "qgsfeatureiterator.h"
45 % End
46 #endif
47 
53 {
54  public:
55 
60  explicit QgsVectorLayerFeatureSource( const QgsVectorLayer *layer );
61 
66 
67  ~QgsVectorLayerFeatureSource() override;
68 
70 
72 
79  QgsFields fields() const;
80 
86 
92  QString id() const;
93 
94  protected:
95 
96  std::unique_ptr< QgsAbstractFeatureSource > mProviderFeatureSource;
97  std::unique_ptr< QgsVectorLayerJoinBuffer > mJoinBuffer;
98  std::unique_ptr< QgsExpressionFieldBuffer > mExpressionFieldBuffer;
99 
101 
102  QString mId;
103 
105 
107 
108  // A deep-copy is only performed, if the original maps change
109  // see here https://github.com/qgis/Quantum-GIS/pull/673
110  // for explanation
114  QList<QgsField> mAddedAttributes;
117 
119 
120  private:
121 #ifdef SIP_RUN
123 #endif
124 };
125 
129 class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>
130 {
131  public:
132  QgsVectorLayerFeatureIterator( QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
133 
134  ~QgsVectorLayerFeatureIterator() override;
135 
137  bool rewind() override;
138 
140  bool close() override;
141 
142  void setInterruptionChecker( QgsFeedback *interruptionChecker ) override SIP_SKIP;
143 
148  struct CORE_EXPORT FetchJoinInfo
149  {
155  QMap<int, int> attributesSourceToDestLayerMap SIP_SKIP;
158 
159 #ifndef SIP_RUN
160 
167  std::shared_ptr< QgsVectorLayerFeatureSource > joinSource;
168 
176 #endif
177 
180 
183 
184  void addJoinedAttributesCached( QgsFeature &f, const QVariant &joinValue ) const;
185  void addJoinedAttributesDirect( QgsFeature &f, const QVariant &joinValue ) const;
186  };
187 
188  bool isValid() const override;
189 
190  protected:
192  bool fetchFeature( QgsFeature &feature ) override;
193 
198  bool nextFeatureFilterExpression( QgsFeature &f ) override { return fetchFeature( f ); }
199 
201  bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod ) override;
202 
204  void rewindEditBuffer() SIP_SKIP;
205 
207  void prepareJoin( int fieldIdx ) SIP_SKIP;
208 
210  void prepareExpression( int fieldIdx ) SIP_SKIP;
211 
213  void prepareFields() SIP_SKIP;
214 
216  void prepareField( int fieldIdx ) SIP_SKIP;
217 
219  bool fetchNextAddedFeature( QgsFeature &f ) SIP_SKIP;
221  bool fetchNextChangedGeomFeature( QgsFeature &f ) SIP_SKIP;
223  bool fetchNextChangedAttributeFeature( QgsFeature &f ) SIP_SKIP;
225  void useAddedFeature( const QgsFeature &src, QgsFeature &f ) SIP_SKIP;
227  void useChangedAttributeFeature( QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f ) SIP_SKIP;
229  bool nextFeatureFid( QgsFeature &f ) SIP_SKIP;
231  void addJoinedAttributes( QgsFeature &f ) SIP_SKIP;
232 
243  void addVirtualAttributes( QgsFeature &f ) SIP_SKIP;
244 
252  void addExpressionAttribute( QgsFeature &f, int attrIndex ) SIP_SKIP;
253 
258  void updateChangedAttributes( QgsFeature &f ) SIP_SKIP;
259 
264  void updateFeatureGeometry( QgsFeature &f ) SIP_SKIP;
265 
266  QgsFeatureRequest mProviderRequest;
267  QgsFeatureIterator mProviderIterator;
268  QgsFeatureRequest mChangedFeaturesRequest;
269  QgsFeatureIterator mChangedFeaturesIterator;
270 
271  // filter bounding box constraint, in SOURCE CRS
272  QgsRectangle mFilterRect;
274 
275  // distance within constraint reference geometry and distance IN DESTINATION CRS
276  QgsGeometry mDistanceWithinGeom;
277  std::shared_ptr< QgsGeometryEngine > mDistanceWithinEngine;
278  double mDistanceWithin = 0;
279 
280  // only related to editing
281  QSet<QgsFeatureId> mFetchConsidered;
282  QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
283  QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt;
284 
285  bool mFetchedFid; // when iterating by FID: indicator whether it has been fetched yet or not
286 
292 
293  QMap<int, QgsExpression *> mExpressionFieldInfo;
294 
295  bool mHasVirtualAttributes;
296 
297  private:
298 #ifdef SIP_RUN
300 #endif
301 
302  void createExpressionContext();
303  std::unique_ptr<QgsExpressionContext> mExpressionContext;
304 
305  QgsFeedback *mInterruptionChecker = nullptr;
306 
307  QList< int > mPreparedFields;
308  QList< int > mFieldsToPrepare;
309 
311  QList< FetchJoinInfo > mOrderedJoinInfoList;
312 
317  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
318 
320  bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
321 
322  void createOrderedJoinList();
323 
327  bool postProcessFeature( QgsFeature &feature );
328 
332  bool checkGeometryValidity( const QgsFeature &feature );
333 
334  bool mDelegatedOrderByToProvider = false;
335 };
336 
337 
338 
346 {
347  public:
348 
355 
360 
361  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
362  QgsCoordinateReferenceSystem sourceCrs() const override;
363  QgsFields fields() const override;
364  QgsWkbTypes::Type wkbType() const override;
365  long long featureCount() const override;
366  QString sourceName() const override;
368  SpatialIndexPresence hasSpatialIndex() const override;
369 
370  private:
371 
372 #ifdef SIP_RUN
374 #endif
375 
376  // ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
377  mutable QgsVectorLayerFeatureSource mSource;
378  QgsFeatureIds mSelectedFeatureIds;
380  QString mName;
381  QPointer< QgsVectorLayer > mLayer;
382 
383 };
384 
386 
387 #ifndef SIP_RUN
388 class QgsVectorLayerSelectedFeatureIterator : public QgsAbstractFeatureIterator
389 {
390  public:
391 
392  QgsVectorLayerSelectedFeatureIterator( const QgsFeatureIds &selectedFeatureIds,
393  const QgsFeatureRequest &request,
394  QgsVectorLayerFeatureSource &source );
395 
396  bool rewind() override;
397  bool close() override;
398 
399  protected:
400  bool fetchFeature( QgsFeature &f ) override;
401 
402  private:
403  QgsFeatureIds mSelectedFeatureIds;
404  QgsFeatureIterator mIterator;
405 
406 };
407 
408 #endif
409 
411 
412 #endif // QGSVECTORLAYERFEATUREITERATOR_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsVectorLayerFeatureSource::mLayerScope
QgsExpressionContextScope mLayerScope
Definition: qgsvectorlayerfeatureiterator.h:104
qgsfields.h
QgsFeatureSource::hasSpatialIndex
virtual SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on the source,...
Definition: qgsfeaturesource.cpp:190
QgsFeatureSource::sourceCrs
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
QgsSimplifyMethod
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Definition: qgssimplifymethod.h:28
QgsAbstractFeatureIterator::setInterruptionChecker
virtual void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
Definition: qgsfeatureiterator.cpp:261
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
QgsVectorLayerFeatureIterator::nextFeatureFilterExpression
bool nextFeatureFilterExpression(QgsFeature &f) override
Overrides default method as we only need to filter features in the edit buffer while for others filte...
Definition: qgsvectorlayerfeatureiterator.h:198
QgsVectorLayerFeatureSource::mFields
QgsFields mFields
Definition: qgsvectorlayerfeatureiterator.h:100
QgsVectorLayerFeatureSource::mAddedFeatures
QgsFeatureMap mAddedFeatures
Definition: qgsvectorlayerfeatureiterator.h:111
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
qgsfeatureiterator.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsVectorLayerFeatureSource::mDeletedAttributeIds
QgsAttributeList mDeletedAttributeIds
Definition: qgsvectorlayerfeatureiterator.h:116
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:37
QgsVectorLayerFeatureSource::mChangedAttributeValues
QgsChangedAttributesMap mChangedAttributeValues
Definition: qgsvectorlayerfeatureiterator.h:115
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsExpressionContextScopeGenerator
Abstract interface for generating an expression context scope.
Definition: qgsexpressioncontextscopegenerator.h:28
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:868
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsAbstractFeatureIteratorFromSource
Helper template that cares of two things: 1. automatic deletion of source if owned by iterator,...
Definition: qgsfeatureiterator.h:259
QgsVectorLayerFeatureSource::mChangedGeometries
QgsGeometryMap mChangedGeometries
Definition: qgsvectorlayerfeatureiterator.h:112
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinField
int joinField
Definition: qgsvectorlayerfeatureiterator.h:182
QgsVectorLayerSelectedFeatureSource
QgsFeatureSource subclass for the selected features from a QgsVectorLayer.
Definition: qgsvectorlayerfeatureiterator.h:345
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:71
QgsFeatureSource::sourceName
virtual QString sourceName() const =0
Returns a friendly display name for the source.
QgsVectorLayerFeatureSource::mProviderFeatureSource
std::unique_ptr< QgsAbstractFeatureSource > mProviderFeatureSource
Definition: qgsvectorlayerfeatureiterator.h:96
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsVectorLayerFeatureIterator
Definition: qgsvectorlayerfeatureiterator.h:129
QgsVectorLayerFeatureSource::mAddedAttributes
QList< QgsField > mAddedAttributes
Definition: qgsvectorlayerfeatureiterator.h:114
QgsFeatureSource::SpatialIndexPresence
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition: qgsfeaturesource.h:187
QgsVectorLayerJoinBuffer
Manages joined fields for a vector layer.
Definition: qgsvectorlayerjoinbuffer.h:37
qgis_sip.h
QgsAbstractFeatureSource
Base class that can be used for any class that is capable of returning features.
Definition: qgsfeaturerequest.h:1009
QgsFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
qgsfeaturesource.h
QgsFeatureSource::featureCount
virtual long long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
qgscoordinatetransform.h
QgsVectorLayerJoinInfo
Defines left outer join from our vector layer to some other vector layer. The join is done based on [...
Definition: qgsvectorlayerjoininfo.h:33
QgsExpressionFieldBuffer
Buffers information about expression fields for a vector layer.
Definition: qgsexpressionfieldbuffer.h:35
QgsFeatureMap
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
Definition: qgsvectorlayereditbuffer.h:31
QgsVectorLayerFeatureIterator::FetchJoinInfo::indexOffset
int indexOffset
At what position the joined fields start.
Definition: qgsvectorlayerfeatureiterator.h:157
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsAbstractFeatureIterator::rewind
virtual bool rewind()=0
reset the iterator to the starting position
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsVectorLayerFeatureSource::mCrs
QgsCoordinateReferenceSystem mCrs
Definition: qgsvectorlayerfeatureiterator.h:118
QgsFeatureSource::fields
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsVectorLayerFeatureIterator::FetchJoinInfo::targetField
int targetField
Index of field (of this layer) that drives the join.
Definition: qgsvectorlayerfeatureiterator.h:179
QgsVectorLayerFeatureIterator::FetchJoinInfo
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
Definition: qgsvectorlayerfeatureiterator.h:148
QgsVectorLayerFeatureSource::mHasEditBuffer
bool mHasEditBuffer
Definition: qgsvectorlayerfeatureiterator.h:106
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayerEditBuffer
Definition: qgsvectorlayereditbuffer.h:37
QgsFeatureSource::wkbType
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorLayerFeatureSource
Partial snapshot of vector layer's state (only the members necessary for access to features)
Definition: qgsvectorlayerfeatureiterator.h:52
QgsAbstractFeatureIterator::fetchFeature
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!...
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinSource
std::shared_ptr< QgsVectorLayerFeatureSource > joinSource
Feature source for join.
Definition: qgsvectorlayerfeatureiterator.h:167
QgsVectorLayerFeatureSource::mExpressionFieldBuffer
std::unique_ptr< QgsExpressionFieldBuffer > mExpressionFieldBuffer
Definition: qgsvectorlayerfeatureiterator.h:98
QgsVectorLayerFeatureIterator::FetchJoinInfo::attributes
QgsAttributeList attributes
Attributes to fetch.
Definition: qgsvectorlayerfeatureiterator.h:153
QgsSimplifyMethod::MethodType
MethodType
Definition: qgssimplifymethod.h:31
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinLayerFields
QgsFields joinLayerFields
Fields from joined layer.
Definition: qgsvectorlayerfeatureiterator.h:175
QgsAbstractFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
QgsGeometryMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:877
qgsexpressioncontextscopegenerator.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinInfo
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
Definition: qgsvectorlayerfeatureiterator.h:151
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsVectorLayerFeatureSource::mId
QString mId
Definition: qgsvectorlayerfeatureiterator.h:102
QgsGeometryEngine
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
Definition: qgsgeometryengine.h:68
qgscoordinatereferencesystem.h
QgsAbstractFeatureIterator
Internal feature iterator to be implemented within data providers.
Definition: qgsfeatureiterator.h:28
QgsVectorLayerFeatureSource::mDeletedFeatureIds
QgsFeatureIds mDeletedFeatureIds
Definition: qgsvectorlayerfeatureiterator.h:113
QgsAbstractFeatureIterator::close
virtual bool close()=0
end of iterating: free the resources / lock
QgsVectorLayerFeatureSource::mJoinBuffer
std::unique_ptr< QgsVectorLayerJoinBuffer > mJoinBuffer
Definition: qgsvectorlayerfeatureiterator.h:97
QgsExpressionContextScopeGenerator::createExpressionContextScope
virtual QgsExpressionContextScope * createExpressionContextScope() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28