QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
26 #include <QPointer>
27 #include <QSet>
28 #include <memory>
29 
30 typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap SIP_SKIP;
31 
33 class QgsVectorLayer;
38 
40 
41 #ifdef SIP_RUN
42 % ModuleHeaderCode
43 #include "qgsfeatureiterator.h"
44 % End
45 #endif
46 
52 {
53  public:
54 
59  explicit QgsVectorLayerFeatureSource( const QgsVectorLayer *layer );
60 
61  ~QgsVectorLayerFeatureSource() override;
62 
64 
66 
73  QgsFields fields() const;
74 
80 
86  QString id() const;
87 
88  protected:
89 
90  QgsAbstractFeatureSource *mProviderFeatureSource = nullptr;
91 
92  QgsVectorLayerJoinBuffer *mJoinBuffer = nullptr;
93 
94  QgsExpressionFieldBuffer *mExpressionFieldBuffer = nullptr;
95 
97 
98  QString mId;
99 
101 
103 
104  // A deep-copy is only performed, if the original maps change
105  // see here https://github.com/qgis/Quantum-GIS/pull/673
106  // for explanation
110  QList<QgsField> mAddedAttributes;
113 
115 };
116 
120 class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>
121 {
122  public:
123  QgsVectorLayerFeatureIterator( QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
124 
125  ~QgsVectorLayerFeatureIterator() override;
126 
128  bool rewind() override;
129 
131  bool close() override;
132 
133  void setInterruptionChecker( QgsFeedback *interruptionChecker ) override SIP_SKIP;
134 
139  struct CORE_EXPORT FetchJoinInfo
140  {
146  int joinField;
147 
148  void addJoinedAttributesCached( QgsFeature &f, const QVariant &joinValue ) const;
149  void addJoinedAttributesDirect( QgsFeature &f, const QVariant &joinValue ) const;
150  };
151 
152 
153  bool isValid() const override;
154 
155  protected:
157  bool fetchFeature( QgsFeature &feature ) override;
158 
163  bool nextFeatureFilterExpression( QgsFeature &f ) override { return fetchFeature( f ); }
164 
166  bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod ) override;
167 
169  void rewindEditBuffer() SIP_SKIP;
170 
172  void prepareJoin( int fieldIdx ) SIP_SKIP;
173 
175  void prepareExpression( int fieldIdx ) SIP_SKIP;
176 
178  void prepareFields() SIP_SKIP;
179 
181  void prepareField( int fieldIdx ) SIP_SKIP;
182 
184  bool fetchNextAddedFeature( QgsFeature &f ) SIP_SKIP;
186  bool fetchNextChangedGeomFeature( QgsFeature &f ) SIP_SKIP;
188  bool fetchNextChangedAttributeFeature( QgsFeature &f ) SIP_SKIP;
190  void useAddedFeature( const QgsFeature &src, QgsFeature &f ) SIP_SKIP;
192  void useChangedAttributeFeature( QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f ) SIP_SKIP;
194  bool nextFeatureFid( QgsFeature &f ) SIP_SKIP;
196  void addJoinedAttributes( QgsFeature &f ) SIP_SKIP;
197 
208  void addVirtualAttributes( QgsFeature &f ) SIP_SKIP;
209 
217  void addExpressionAttribute( QgsFeature &f, int attrIndex ) SIP_SKIP;
218 
223  void updateChangedAttributes( QgsFeature &f ) SIP_SKIP;
224 
229  void updateFeatureGeometry( QgsFeature &f ) SIP_SKIP;
230 
231  QgsFeatureRequest mProviderRequest;
232  QgsFeatureIterator mProviderIterator;
233  QgsFeatureRequest mChangedFeaturesRequest;
234  QgsFeatureIterator mChangedFeaturesIterator;
235 
236  QgsRectangle mFilterRect;
238 
239  // only related to editing
240  QSet<QgsFeatureId> mFetchConsidered;
241  QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
242  QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt;
243 
244  bool mFetchedFid; // when iterating by FID: indicator whether it has been fetched yet or not
245 
250 
251  QMap<int, QgsExpression *> mExpressionFieldInfo;
252 
253  bool mHasVirtualAttributes;
254 
255  private:
256 #ifdef SIP_RUN
258 #endif
259 
260  void createExpressionContext();
261  std::unique_ptr<QgsExpressionContext> mExpressionContext;
262 
263  QgsFeedback *mInterruptionChecker = nullptr;
264 
265  QList< int > mPreparedFields;
266  QList< int > mFieldsToPrepare;
267 
269  QList< FetchJoinInfo > mOrderedJoinInfoList;
270 
275  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
276 
278  bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
279 
280  void createOrderedJoinList();
281 
285  bool postProcessFeature( QgsFeature &feature );
286 
290  bool checkGeometryValidity( const QgsFeature &feature );
291 };
292 
293 
294 
302 {
303  public:
304 
311 
312  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
313  QgsCoordinateReferenceSystem sourceCrs() const override;
314  QgsFields fields() const override;
315  QgsWkbTypes::Type wkbType() const override;
316  long featureCount() const override;
317  QString sourceName() const override;
319  SpatialIndexPresence hasSpatialIndex() const override;
320 
321  private:
322 
323  // ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
324  mutable QgsVectorLayerFeatureSource mSource;
325  QgsFeatureIds mSelectedFeatureIds;
327  QString mName;
328  QPointer< QgsVectorLayer > mLayer;
329 
330 };
331 
333 
334 #ifndef SIP_RUN
335 class QgsVectorLayerSelectedFeatureIterator : public QgsAbstractFeatureIterator
336 {
337  public:
338 
339  QgsVectorLayerSelectedFeatureIterator( const QgsFeatureIds &selectedFeatureIds,
340  const QgsFeatureRequest &request,
341  QgsVectorLayerFeatureSource &source );
342 
343  bool rewind() override;
344  bool close() override;
345 
346  protected:
347  bool fetchFeature( QgsFeature &f ) override;
348 
349  private:
350  QgsFeatureIds mSelectedFeatureIds;
351  QgsFeatureIterator mIterator;
352 
353 };
354 
355 #endif
356 
358 
359 #endif // QGSVECTORLAYERFEATUREITERATOR_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsVectorLayerFeatureSource::mLayerScope
QgsExpressionContextScope mLayerScope
Definition: qgsvectorlayerfeatureiterator.h:100
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
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:220
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:163
QgsVectorLayerFeatureSource::mFields
QgsFields mFields
Definition: qgsvectorlayerfeatureiterator.h:96
QgsVectorLayerFeatureSource::mAddedFeatures
QgsFeatureMap mAddedFeatures
Definition: qgsvectorlayerfeatureiterator.h:107
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
qgsfeatureiterator.h
QgsFields
Definition: qgsfields.h:44
QgsVectorLayerFeatureSource::mDeletedAttributeIds
QgsAttributeList mDeletedAttributeIds
Definition: qgsvectorlayerfeatureiterator.h:112
QgsFeatureSource
Definition: qgsfeaturesource.h:37
QgsVectorLayerFeatureSource::mChangedAttributeValues
QgsChangedAttributesMap mChangedAttributeValues
Definition: qgsvectorlayerfeatureiterator.h:111
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsExpressionContextScopeGenerator
Definition: qgsexpressioncontextscopegenerator.h:28
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:558
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
Definition: qgsrectangle.h:41
QgsAbstractFeatureIteratorFromSource
Definition: qgsfeatureiterator.h:233
QgsVectorLayerFeatureSource::mChangedGeometries
QgsGeometryMap mChangedGeometries
Definition: qgsvectorlayerfeatureiterator.h:108
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinField
int joinField
Index of field (of the joined layer) must have equal value.
Definition: qgsvectorlayerfeatureiterator.h:146
QgsVectorLayerSelectedFeatureSource
Definition: qgsvectorlayerfeatureiterator.h:301
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinLayer
QgsVectorLayer * joinLayer
Resolved pointer to the joined layer.
Definition: qgsvectorlayerfeatureiterator.h:144
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:70
QgsFeatureSource::sourceName
virtual QString sourceName() const =0
Returns a friendly display name for the source.
QgsFeedback
Definition: qgsfeedback.h:43
QgsVectorLayerFeatureIterator
Definition: qgsvectorlayerfeatureiterator.h:120
QgsVectorLayerFeatureSource::mAddedAttributes
QList< QgsField > mAddedAttributes
Definition: qgsvectorlayerfeatureiterator.h:110
QgsFeatureSource::SpatialIndexPresence
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition: qgsfeaturesource.h:187
QgsVectorLayerJoinBuffer
Definition: qgsvectorlayerjoinbuffer.h:36
qgis_sip.h
QgsAbstractFeatureSource
Definition: qgsfeaturerequest.h:742
QgsFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
qgsfeaturesource.h
QgsVectorLayerJoinInfo
Definition: qgsvectorlayerjoininfo.h:33
QgsExpressionFieldBuffer
Definition: qgsexpressionfieldbuffer.h:35
QgsFeatureMap
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
Definition: qgsvectorlayereditbuffer.h:30
QgsVectorLayerFeatureIterator::FetchJoinInfo::indexOffset
int indexOffset
At what position the joined fields start.
Definition: qgsvectorlayerfeatureiterator.h:143
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsAbstractFeatureIterator::rewind
virtual bool rewind()=0
reset the iterator to the starting position
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsVectorLayerFeatureSource::mCrs
QgsCoordinateReferenceSystem mCrs
Definition: qgsvectorlayerfeatureiterator.h:114
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:111
QgsVectorLayerFeatureIterator::FetchJoinInfo::targetField
int targetField
Index of field (of this layer) that drives the join.
Definition: qgsvectorlayerfeatureiterator.h:145
QgsFeatureSource::featureCount
virtual long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsVectorLayerFeatureIterator::FetchJoinInfo
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
Definition: qgsvectorlayerfeatureiterator.h:139
QgsVectorLayerFeatureSource::mHasEditBuffer
bool mHasEditBuffer
Definition: qgsvectorlayerfeatureiterator.h:102
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayerEditBuffer
Definition: qgsvectorlayereditbuffer.h:36
QgsFeatureSource::wkbType
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsVectorLayerFeatureSource
Definition: qgsvectorlayerfeatureiterator.h:51
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::attributes
QgsAttributeList attributes
Attributes to fetch.
Definition: qgsvectorlayerfeatureiterator.h:142
QgsSimplifyMethod::MethodType
MethodType
Definition: qgssimplifymethod.h:31
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:567
qgsexpressioncontextscopegenerator.h
QgsFeature
Definition: qgsfeature.h:55
QgsVectorLayerFeatureIterator::FetchJoinInfo::joinInfo
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
Definition: qgsvectorlayerfeatureiterator.h:141
QgsExpression
Definition: qgsexpression.h:113
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsVectorLayerFeatureSource::mId
QString mId
Definition: qgsvectorlayerfeatureiterator.h:98
qgscoordinatereferencesystem.h
QgsAbstractFeatureIterator
Definition: qgsfeatureiterator.h:28
QgsVectorLayerFeatureSource::mDeletedFeatureIds
QgsFeatureIds mDeletedFeatureIds
Definition: qgsvectorlayerfeatureiterator.h:109
QgsAbstractFeatureIterator::close
virtual bool close()=0
end of iterating: free the resources / lock
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
Definition: qgsfeatureid.h:25