QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
65 
67 
69 
71 
78  QgsFields fields() const;
79 
85 
91  QString id() const;
92 
93  protected:
94 
95  std::unique_ptr< QgsAbstractFeatureSource > mProviderFeatureSource;
96  std::unique_ptr< QgsVectorLayerJoinBuffer > mJoinBuffer;
97  std::unique_ptr< QgsExpressionFieldBuffer > mExpressionFieldBuffer;
98 
100 
101  QString mId;
102 
104 
106 
107  // A deep-copy is only performed, if the original maps change
108  // see here https://github.com/qgis/Quantum-GIS/pull/673
109  // for explanation
113  QList<QgsField> mAddedAttributes;
116 
118 
119  private:
120 #ifdef SIP_RUN
122 #endif
123 };
124 
128 class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>
129 {
130  public:
131  QgsVectorLayerFeatureIterator( QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
132 
133  ~QgsVectorLayerFeatureIterator() override;
134 
136  bool rewind() override;
137 
139  bool close() override;
140 
141  void setInterruptionChecker( QgsFeedback *interruptionChecker ) override SIP_SKIP;
142 
147  struct CORE_EXPORT FetchJoinInfo
148  {
154  int joinField;
155 
156  void addJoinedAttributesCached( QgsFeature &f, const QVariant &joinValue ) const;
157  void addJoinedAttributesDirect( QgsFeature &f, const QVariant &joinValue ) const;
158  };
159 
160 
161  bool isValid() const override;
162 
163  protected:
165  bool fetchFeature( QgsFeature &feature ) override;
166 
171  bool nextFeatureFilterExpression( QgsFeature &f ) override { return fetchFeature( f ); }
172 
174  bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod ) override;
175 
177  void rewindEditBuffer() SIP_SKIP;
178 
180  void prepareJoin( int fieldIdx ) SIP_SKIP;
181 
183  void prepareExpression( int fieldIdx ) SIP_SKIP;
184 
186  void prepareFields() SIP_SKIP;
187 
189  void prepareField( int fieldIdx ) SIP_SKIP;
190 
192  bool fetchNextAddedFeature( QgsFeature &f ) SIP_SKIP;
194  bool fetchNextChangedGeomFeature( QgsFeature &f ) SIP_SKIP;
196  bool fetchNextChangedAttributeFeature( QgsFeature &f ) SIP_SKIP;
198  void useAddedFeature( const QgsFeature &src, QgsFeature &f ) SIP_SKIP;
200  void useChangedAttributeFeature( QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f ) SIP_SKIP;
202  bool nextFeatureFid( QgsFeature &f ) SIP_SKIP;
204  void addJoinedAttributes( QgsFeature &f ) SIP_SKIP;
205 
216  void addVirtualAttributes( QgsFeature &f ) SIP_SKIP;
217 
225  void addExpressionAttribute( QgsFeature &f, int attrIndex ) SIP_SKIP;
226 
231  void updateChangedAttributes( QgsFeature &f ) SIP_SKIP;
232 
237  void updateFeatureGeometry( QgsFeature &f ) SIP_SKIP;
238 
239  QgsFeatureRequest mProviderRequest;
240  QgsFeatureIterator mProviderIterator;
241  QgsFeatureRequest mChangedFeaturesRequest;
242  QgsFeatureIterator mChangedFeaturesIterator;
243 
244  QgsRectangle mFilterRect;
246 
247  // only related to editing
248  QSet<QgsFeatureId> mFetchConsidered;
249  QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
250  QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt;
251 
252  bool mFetchedFid; // when iterating by FID: indicator whether it has been fetched yet or not
253 
259 
260  QMap<int, QgsExpression *> mExpressionFieldInfo;
261 
262  bool mHasVirtualAttributes;
263 
264  private:
265 #ifdef SIP_RUN
267 #endif
268 
269  void createExpressionContext();
270  std::unique_ptr<QgsExpressionContext> mExpressionContext;
271 
272  QgsFeedback *mInterruptionChecker = nullptr;
273 
274  QList< int > mPreparedFields;
275  QList< int > mFieldsToPrepare;
276 
278  QList< FetchJoinInfo > mOrderedJoinInfoList;
279 
284  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
285 
287  bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
288 
289  void createOrderedJoinList();
290 
294  bool postProcessFeature( QgsFeature &feature );
295 
299  bool checkGeometryValidity( const QgsFeature &feature );
300 
301  bool mDelegatedOrderByToProvider = false;
302 };
303 
304 
305 
313 {
314  public:
315 
322 
327 
328  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
329  QgsCoordinateReferenceSystem sourceCrs() const override;
330  QgsFields fields() const override;
331  QgsWkbTypes::Type wkbType() const override;
332  long featureCount() const override;
333  QString sourceName() const override;
334  QgsExpressionContextScope *createExpressionContextScope() const override;
335  SpatialIndexPresence hasSpatialIndex() const override;
336 
337  private:
338 
339 #ifdef SIP_RUN
341 #endif
342 
343  // ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
344  mutable QgsVectorLayerFeatureSource mSource;
345  QgsFeatureIds mSelectedFeatureIds;
347  QString mName;
348  QPointer< QgsVectorLayer > mLayer;
349 
350 };
351 
353 
354 #ifndef SIP_RUN
355 class QgsVectorLayerSelectedFeatureIterator : public QgsAbstractFeatureIterator
356 {
357  public:
358 
359  QgsVectorLayerSelectedFeatureIterator( const QgsFeatureIds &selectedFeatureIds,
360  const QgsFeatureRequest &request,
361  QgsVectorLayerFeatureSource &source );
362 
363  bool rewind() override;
364  bool close() override;
365 
366  protected:
367  bool fetchFeature( QgsFeature &f ) override;
368 
369  private:
370  QgsFeatureIds mSelectedFeatureIds;
371  QgsFeatureIterator mIterator;
372 
373 };
374 
375 #endif
376 
378 
379 #endif // QGSVECTORLAYERFEATUREITERATOR_H
Helper template that cares of two things: 1.
Internal feature iterator to be implemented within data providers.
virtual bool rewind()=0
reset the iterator to the starting position
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!...
virtual bool close()=0
end of iterating: free the resources / lock
Base class that can be used for any class that is capable of returning features.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Abstract interface for generating an expression context scope.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Buffers information about expression fields for a vector layer.
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
An interface for objects which provide features via a getFeatures method.
SpatialIndexPresence
Enumeration of spatial index presence states.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
bool nextFeatureFilterExpression(QgsFeature &f) override
Overrides default method as we only need to filter features in the edit buffer while for others filte...
Partial snapshot of vector layer's state (only the members necessary for access to features)
QgsChangedAttributesMap mChangedAttributeValues
std::unique_ptr< QgsAbstractFeatureSource > mProviderFeatureSource
QgsVectorLayerFeatureSource & operator==(const QgsVectorLayerFeatureSource &other)=delete
QgsVectorLayerFeatureSource cannot be copied.
~QgsVectorLayerFeatureSource() override
QgsVectorLayerFeatureSource(const QgsVectorLayerFeatureSource &other)=delete
QgsVectorLayerFeatureSource cannot be copied.
std::unique_ptr< QgsVectorLayerJoinBuffer > mJoinBuffer
std::unique_ptr< QgsExpressionFieldBuffer > mExpressionFieldBuffer
Manages joined fields for a vector layer.
Defines left outer join from our vector layer to some other vector layer.
QgsFeatureSource subclass for the selected features from a QgsVectorLayer.
QgsVectorLayerSelectedFeatureSource(const QgsVectorLayerSelectedFeatureSource &other)=delete
QgsVectorLayerSelectedFeatureSource cannot be copied.
QgsVectorLayerSelectedFeatureSource & operator==(const QgsVectorLayerSelectedFeatureSource &other)=delete
QgsVectorLayerSelectedFeatureSource cannot be copied.
Represents a vector layer which manages a vector based data sets.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
#define SIP_SKIP
Definition: qgis_sip.h:126
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:609
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:600
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
const QgsCoordinateReferenceSystem & crs
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
int targetField
Index of field (of this layer) that drives the join.
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
QgsVectorLayer * joinLayer
Resolved pointer to the joined layer.
int joinField
Index of field (of the joined layer) must have equal value.
int indexOffset
At what position the joined fields start.