QGIS API Documentation  2.99.0-Master (69af2f5)
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"
24 
25 #include <QSet>
26 #include <memory>
27 
28 typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap SIP_SKIP;
29 
31 class QgsVectorLayer;
36 
38 
39 #ifdef SIP_RUN
40 % ModuleHeaderCode
41 #include "qgsfeatureiterator.h"
42 % End
43 #endif
44 
49 {
50  public:
51 
55  explicit QgsVectorLayerFeatureSource( const QgsVectorLayer *layer );
56 
58 
59  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) override;
60 
62 
69  QgsFields fields() const;
70 
75  QgsCoordinateReferenceSystem crs() const;
76 
77  protected:
78 
79  QgsAbstractFeatureSource *mProviderFeatureSource = nullptr;
80 
81  QgsVectorLayerJoinBuffer *mJoinBuffer = nullptr;
82 
83  QgsExpressionFieldBuffer *mExpressionFieldBuffer = nullptr;
84 
86 
88 
89  // A deep-copy is only performed, if the original maps change
90  // see here https://github.com/qgis/Quantum-GIS/pull/673
91  // for explanation
95  QList<QgsField> mAddedAttributes;
98 
100 };
101 
104 class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>
105 {
106  public:
107  QgsVectorLayerFeatureIterator( QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
108 
110 
112  virtual bool rewind() override;
113 
115  virtual bool close() override;
116 
117  virtual void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) override SIP_SKIP;
118 
122  struct CORE_EXPORT FetchJoinInfo
123  {
129  int joinField;
130 
131  void addJoinedAttributesCached( QgsFeature &f, const QVariant &joinValue ) const;
132  void addJoinedAttributesDirect( QgsFeature &f, const QVariant &joinValue ) const;
133  };
134 
135 
136  protected:
138  virtual bool fetchFeature( QgsFeature &feature ) override;
139 
142  virtual bool nextFeatureFilterExpression( QgsFeature &f ) override { return fetchFeature( f ); }
143 
145  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod ) override;
146 
148  void rewindEditBuffer() SIP_SKIP;
149 
151  void prepareJoin( int fieldIdx ) SIP_SKIP;
152 
154  void prepareExpression( int fieldIdx ) SIP_SKIP;
155 
157  void prepareFields() SIP_SKIP;
158 
160  void prepareField( int fieldIdx ) SIP_SKIP;
161 
163  bool fetchNextAddedFeature( QgsFeature &f ) SIP_SKIP;
165  bool fetchNextChangedGeomFeature( QgsFeature &f ) SIP_SKIP;
167  bool fetchNextChangedAttributeFeature( QgsFeature &f ) SIP_SKIP;
169  void useAddedFeature( const QgsFeature &src, QgsFeature &f ) SIP_SKIP;
171  void useChangedAttributeFeature( QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f ) SIP_SKIP;
173  bool nextFeatureFid( QgsFeature &f ) SIP_SKIP;
175  void addJoinedAttributes( QgsFeature &f ) SIP_SKIP;
176 
186  void addVirtualAttributes( QgsFeature &f ) SIP_SKIP;
187 
194  void addExpressionAttribute( QgsFeature &f, int attrIndex ) SIP_SKIP;
195 
199  void updateChangedAttributes( QgsFeature &f ) SIP_SKIP;
200 
204  void updateFeatureGeometry( QgsFeature &f ) SIP_SKIP;
205 
210 
213 
214  // only related to editing
215  QSet<QgsFeatureId> mFetchConsidered;
216  QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
217  QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt;
218 
219  bool mFetchedFid; // when iterating by FID: indicator whether it has been fetched yet or not
220 
223  QMap<const QgsVectorLayerJoinInfo *, QgsVectorLayerFeatureIterator::FetchJoinInfo> mFetchJoinInfo;
224 
225  QMap<int, QgsExpression *> mExpressionFieldInfo;
226 
228 
229  private:
230 #ifdef SIP_RUN
232 #endif
233 
234  std::unique_ptr<QgsExpressionContext> mExpressionContext;
235 
236  QgsInterruptionChecker *mInterruptionChecker = nullptr;
237 
238  QList< int > mPreparedFields;
239  QList< int > mFieldsToPrepare;
240 
242  QList< FetchJoinInfo > mOrderedJoinInfoList;
243 
248  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
249 
251  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
252 
253  void createOrderedJoinList();
254 
258  bool postProcessFeature( QgsFeature &feature );
259 
263  bool checkGeometryValidity( const QgsFeature &feature );
264 };
265 
266 
267 
275 {
276  public:
277 
284 
285  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
286  virtual QgsCoordinateReferenceSystem sourceCrs() const override;
287  virtual QgsFields fields() const override;
288  virtual QgsWkbTypes::Type wkbType() const override;
289  virtual long featureCount() const override;
290 
291  private:
292 
293  // ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
294  mutable QgsVectorLayerFeatureSource mSource;
295  QgsFeatureIds mSelectedFeatureIds;
297 
298 };
299 
300 #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
A rectangle specified with double values.
Definition: qgsrectangle.h:38
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
QMap< int, QgsExpression * > mExpressionFieldInfo
QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt
QgsGeometryMap::ConstIterator mFetchChangedGeomIt
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
virtual bool rewind()=0
reset the iterator to the starting position
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
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...
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:66
#define SIP_SKIP
Definition: qgis_sip.h:107
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.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
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...
QgsFeatureSource subclass for the selected features from a QgsVectorLayer.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:503
An interface for objects which provide features via a getFeatures method.
QMap< const QgsVectorLayerJoinInfo *, QgsVectorLayerFeatureIterator::FetchJoinInfo > mFetchJoinInfo
Information about joins used in the current select() statement.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
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:27
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 QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
Helper template that cares of two things: 1.
virtual long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown...