QGIS API Documentation  3.17.0-Master (8af46bc54f)
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 
235 
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  QMap<const QgsVectorLayerJoinInfo *, QgsVectorLayerFeatureIterator::FetchJoinInfo> mFetchJoinInfo;
251 
252  QMap<int, QgsExpression *> mExpressionFieldInfo;
253 
255 
256  private:
257 #ifdef SIP_RUN
259 #endif
260 
261  void createExpressionContext();
262  std::unique_ptr<QgsExpressionContext> mExpressionContext;
263 
264  QgsFeedback *mInterruptionChecker = nullptr;
265 
266  QList< int > mPreparedFields;
267  QList< int > mFieldsToPrepare;
268 
270  QList< FetchJoinInfo > mOrderedJoinInfoList;
271 
276  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
277 
279  bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
280 
281  void createOrderedJoinList();
282 
286  bool postProcessFeature( QgsFeature &feature );
287 
291  bool checkGeometryValidity( const QgsFeature &feature );
292 
293  bool mDelegatedOrderByToProvider = false;
294 };
295 
296 
297 
305 {
306  public:
307 
314 
315  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
316  QgsCoordinateReferenceSystem sourceCrs() const override;
317  QgsFields fields() const override;
318  QgsWkbTypes::Type wkbType() const override;
319  long featureCount() const override;
320  QString sourceName() const override;
322  SpatialIndexPresence hasSpatialIndex() const override;
323 
324  private:
325 
326  // ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
327  mutable QgsVectorLayerFeatureSource mSource;
328  QgsFeatureIds mSelectedFeatureIds;
330  QString mName;
331  QPointer< QgsVectorLayer > mLayer;
332 
333 };
334 
336 
337 #ifndef SIP_RUN
338 class QgsVectorLayerSelectedFeatureIterator : public QgsAbstractFeatureIterator
339 {
340  public:
341 
342  QgsVectorLayerSelectedFeatureIterator( const QgsFeatureIds &selectedFeatureIds,
343  const QgsFeatureRequest &request,
344  QgsVectorLayerFeatureSource &source );
345 
346  bool rewind() override;
347  bool close() override;
348 
349  protected:
350  bool fetchFeature( QgsFeature &f ) override;
351 
352  private:
353  QgsFeatureIds mSelectedFeatureIds;
354  QgsFeatureIterator mIterator;
355 
356 };
357 
358 #endif
359 
361 
362 #endif // QGSVECTORLAYERFEATUREITERATOR_H
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:578
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:41
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
QMap< int, QgsExpression * > mExpressionFieldInfo
QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt
QgsGeometryMap::ConstIterator mFetchChangedGeomIt
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.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features ...
Definition: qgsfeatureid.h:28
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
Container of fields for a vector layer.
Definition: qgsfields.h:44
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:123
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
#define SIP_SKIP
Definition: qgis_sip.h:126
Internal feature iterator to be implemented within data providers.
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)...
virtual SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on the source, if it can be determined.
Single scope for storing variables and functions for use within a QgsExpressionContext.
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.
virtual QgsExpressionContextScope * createExpressionContextScope() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Buffers information about expression fields for a vector layer.
QgsFeatureSource subclass for the selected features from a QgsVectorLayer.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:569
Abstract interface for generating an expression context scope.
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...
virtual void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
SpatialIndexPresence
Enumeration of spatial index presence states.
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsChangedAttributesMap mChangedAttributeValues
virtual QString sourceName() const =0
Returns a friendly display name for the source.
Represents a vector layer which manages a vector based data sets.
bool nextFeatureFilterExpression(QgsFeature &f) override
Overrides default method as we only need to filter features in the edit buffer while for others filte...
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...