QGIS API Documentation  2.99.0-Master (d55fa22)
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 
24 #include <QSet>
25 #include <memory>
26 
27 typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap;
28 
30 class QgsVectorLayer;
35 
37 
42 {
43  public:
44 
48  explicit QgsVectorLayerFeatureSource( const QgsVectorLayer *layer );
49 
51 
52  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) override;
53 
55 
62  QgsFields fields() const;
63 
64  protected:
65 
66  QgsAbstractFeatureSource *mProviderFeatureSource = nullptr;
67 
68  QgsVectorLayerJoinBuffer *mJoinBuffer = nullptr;
69 
70  QgsExpressionFieldBuffer *mExpressionFieldBuffer = nullptr;
71 
73 
75 
76  // A deep-copy is only performed, if the original maps change
77  // see here https://github.com/qgis/Quantum-GIS/pull/673
78  // for explanation
82  QList<QgsField> mAddedAttributes;
85 
87 };
88 
91 class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>
92 {
93  public:
94  QgsVectorLayerFeatureIterator( QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
95 
97 
99  virtual bool rewind() override;
100 
102  virtual bool close() override;
103 
104  virtual void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) override;
105 
106  protected:
108  virtual bool fetchFeature( QgsFeature &feature ) override;
109 
112  virtual bool nextFeatureFilterExpression( QgsFeature &f ) override { return fetchFeature( f ); }
113 
115  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod ) override;
116 
118  void rewindEditBuffer() SIP_SKIP;
119 
121  void prepareJoin( int fieldIdx ) SIP_SKIP;
122 
124  void prepareExpression( int fieldIdx ) SIP_SKIP;
125 
127  void prepareFields() SIP_SKIP;
128 
130  void prepareField( int fieldIdx ) SIP_SKIP;
131 
133  bool fetchNextAddedFeature( QgsFeature &f ) SIP_SKIP;
135  bool fetchNextChangedGeomFeature( QgsFeature &f ) SIP_SKIP;
137  bool fetchNextChangedAttributeFeature( QgsFeature &f ) SIP_SKIP;
139  void useAddedFeature( const QgsFeature &src, QgsFeature &f ) SIP_SKIP;
141  void useChangedAttributeFeature( QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f ) SIP_SKIP;
143  bool nextFeatureFid( QgsFeature &f ) SIP_SKIP;
145  void addJoinedAttributes( QgsFeature &f ) SIP_SKIP;
146 
156  void addVirtualAttributes( QgsFeature &f ) SIP_SKIP;
157 
164  void addExpressionAttribute( QgsFeature &f, int attrIndex ) SIP_SKIP;
165 
169  void updateChangedAttributes( QgsFeature &f ) SIP_SKIP;
170 
174  void updateFeatureGeometry( QgsFeature &f ) SIP_SKIP;
175 
180  {
186  int joinField;
187 
188  void addJoinedAttributesCached( QgsFeature &f, const QVariant &joinValue ) const;
189  void addJoinedAttributesDirect( QgsFeature &f, const QVariant &joinValue ) const;
190  };
191 
196 
197  // only related to editing
198  QSet<QgsFeatureId> mFetchConsidered;
199  QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
200  QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt;
201 
202  bool mFetchedFid; // when iterating by FID: indicator whether it has been fetched yet or not
203 
206  QMap<const QgsVectorLayerJoinInfo *, FetchJoinInfo> mFetchJoinInfo;
207 
208  QMap<int, QgsExpression *> mExpressionFieldInfo;
209 
211 
212  private:
213  std::unique_ptr<QgsExpressionContext> mExpressionContext;
214 
215  QgsInterruptionChecker *mInterruptionChecker = nullptr;
216 
217  QList< int > mPreparedFields;
218  QList< int > mFieldsToPrepare;
219 
221  QList< FetchJoinInfo > mOrderedJoinInfoList;
222 
227  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
228 
230  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
231 
232  void createOrderedJoinList();
233 
237  bool testFeature( const QgsFeature &feature );
238 
242  bool checkGeometryValidity( const QgsFeature &feature );
243 };
244 
245 #endif // QGSVECTORLAYERFEATUREITERATOR_H
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:512
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
virtual bool close()=0
end of iterating: free the resources / lock
QMap< int, QgsExpression * > mExpressionFieldInfo
QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt
QgsGeometryMap::ConstIterator mFetchChangedGeomIt
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!! ...
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
virtual bool rewind()=0
reset the iterator to the starting position
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Get an iterator for features matching the specified request.
Container of fields for a vector layer.
Definition: qgsfields.h:41
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:96
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QgsVectorLayer * joinLayer
Resolved pointer to the joined layer.
Manages joined fields for a vector layer.
int joinField
Index of field (of the joined layer) must have equal value.
Interface that can be optionally attached to an iterator so its nextFeature() implementaton can check...
#define SIP_SKIP
Definition: qgis_sip.h:87
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Defines left outer join from our vector layer to some other vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Base class that can be used for any class that is capable of returning features.
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
int indexOffset
At what position the joined fields start.
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
int targetField
Index of field (of this layer) that drives the join.
Buffers information about expression fields for a vector layer.
virtual void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:503
This class represents a coordinate reference system (CRS).
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
qint64 QgsFeatureId
Definition: qgsfeature.h:37
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
QgsCoordinateReferenceSystem mCrs
QList< int > QgsAttributeList
Definition: qgsfield.h:28
QMap< const QgsVectorLayerJoinInfo *, FetchJoinInfo > mFetchJoinInfo
Information about joins used in the current select() statement.
virtual bool nextFeatureFilterExpression(QgsFeature &f) override
Overrides default method as we only need to filter features in the edit buffer while for others filte...
QgsChangedAttributesMap mChangedAttributeValues
Represents a vector layer which manages a vector based data sets.
virtual bool prepareSimplification(const QgsSimplifyMethod &simplifyMethod)
Setup the simplification of geometries to fetch using the specified simplify method.
Helper template that cares of two things: 1.