QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgscoordinatetransform.h"
26 
27 #include <QPointer>
28 #include <QSet>
29 #include <memory>
30 
31 typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap SIP_SKIP;
32 
34 class QgsVectorLayer;
39 
41 
42 #ifdef SIP_RUN
43 % ModuleHeaderCode
44 #include "qgsfeatureiterator.h"
45 % End
46 #endif
47 
53 {
54  public:
55 
60  explicit QgsVectorLayerFeatureSource( const QgsVectorLayer *layer );
61 
66 
68 
70 
72 
79  QgsFields fields() const;
80 
86 
92  QString id() const;
93 
94  protected:
95 
96  std::unique_ptr< QgsAbstractFeatureSource > mProviderFeatureSource;
97  std::unique_ptr< QgsVectorLayerJoinBuffer > mJoinBuffer;
98  std::unique_ptr< QgsExpressionFieldBuffer > mExpressionFieldBuffer;
99 
101 
102  QString mId;
103 
105 
107 
108  // A deep-copy is only performed, if the original maps change
109  // see here https://github.com/qgis/Quantum-GIS/pull/673
110  // for explanation
114  QList<QgsField> mAddedAttributes;
117 
119 
120  private:
121 #ifdef SIP_RUN
123 #endif
124 };
125 
129 class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>
130 {
131  public:
132  QgsVectorLayerFeatureIterator( QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
133 
134  ~QgsVectorLayerFeatureIterator() override;
135 
137  bool rewind() override;
138 
140  bool close() override;
141 
142  void setInterruptionChecker( QgsFeedback *interruptionChecker ) override SIP_SKIP;
143 
148  struct CORE_EXPORT FetchJoinInfo
149  {
155  QMap<int, int> attributesSourceToDestLayerMap SIP_SKIP;
158 
159 #ifndef SIP_RUN
160 
167  std::shared_ptr< QgsVectorLayerFeatureSource > joinSource;
168 
176 #endif
177 
180 
183 
184  void addJoinedAttributesCached( QgsFeature &f, const QVariant &joinValue ) const;
185  void addJoinedAttributesDirect( QgsFeature &f, const QVariant &joinValue ) const;
186  };
187 
188  bool isValid() const override;
189 
190  protected:
192  bool fetchFeature( QgsFeature &feature ) override;
193 
198  bool nextFeatureFilterExpression( QgsFeature &f ) override { return fetchFeature( f ); }
199 
201  bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod ) override;
202 
204  void rewindEditBuffer() SIP_SKIP;
205 
207  void prepareJoin( int fieldIdx ) SIP_SKIP;
208 
210  void prepareExpression( int fieldIdx ) SIP_SKIP;
211 
213  void prepareFields() SIP_SKIP;
214 
216  void prepareField( int fieldIdx ) SIP_SKIP;
217 
219  bool fetchNextAddedFeature( QgsFeature &f ) SIP_SKIP;
221  bool fetchNextChangedGeomFeature( QgsFeature &f ) SIP_SKIP;
223  bool fetchNextChangedAttributeFeature( QgsFeature &f ) SIP_SKIP;
225  void useAddedFeature( const QgsFeature &src, QgsFeature &f ) SIP_SKIP;
227  void useChangedAttributeFeature( QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f ) SIP_SKIP;
229  bool nextFeatureFid( QgsFeature &f ) SIP_SKIP;
231  void addJoinedAttributes( QgsFeature &f ) SIP_SKIP;
232 
243  void addVirtualAttributes( QgsFeature &f ) SIP_SKIP;
244 
252  void addExpressionAttribute( QgsFeature &f, int attrIndex ) SIP_SKIP;
253 
258  void updateChangedAttributes( QgsFeature &f ) SIP_SKIP;
259 
264  void updateFeatureGeometry( QgsFeature &f ) SIP_SKIP;
265 
266  QgsFeatureRequest mProviderRequest;
267  QgsFeatureIterator mProviderIterator;
268  QgsFeatureRequest mChangedFeaturesRequest;
269  QgsFeatureIterator mChangedFeaturesIterator;
270 
271  // filter bounding box constraint, in SOURCE CRS
272  QgsRectangle mFilterRect;
274 
275  // distance within constraint reference geometry and distance IN DESTINATION CRS
276  QgsGeometry mDistanceWithinGeom;
277  std::shared_ptr< QgsGeometryEngine > mDistanceWithinEngine;
278  double mDistanceWithin = 0;
279 
280  // only related to editing
281  QSet<QgsFeatureId> mFetchConsidered;
282  QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
283  QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt;
284 
285  bool mFetchedFid; // when iterating by FID: indicator whether it has been fetched yet or not
286 
292 
293  QMap<int, QgsExpression *> mExpressionFieldInfo;
294 
295  bool mHasVirtualAttributes;
296 
297  private:
298 #ifdef SIP_RUN
300 #endif
301 
302  void createExpressionContext();
303  std::unique_ptr<QgsExpressionContext> mExpressionContext;
304 
305  QgsFeedback *mInterruptionChecker = nullptr;
306 
307  QList< int > mPreparedFields;
308  QList< int > mFieldsToPrepare;
309 
311  QList< FetchJoinInfo > mOrderedJoinInfoList;
312 
317  bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
318 
320  bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
321 
322  void createOrderedJoinList();
323 
327  bool postProcessFeature( QgsFeature &feature );
328 
332  bool checkGeometryValidity( const QgsFeature &feature );
333 
334  bool mDelegatedOrderByToProvider = false;
335 };
336 
337 
338 
346 {
347  public:
348 
355 
360 
361  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
362  QgsCoordinateReferenceSystem sourceCrs() const override;
363  QgsFields fields() const override;
364  QgsWkbTypes::Type wkbType() const override;
365  long long featureCount() const override;
366  QString sourceName() const override;
367  QgsExpressionContextScope *createExpressionContextScope() const override;
368  SpatialIndexPresence hasSpatialIndex() const override;
369 
370  private:
371 
372 #ifdef SIP_RUN
374 #endif
375 
376  // ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
377  mutable QgsVectorLayerFeatureSource mSource;
378  QgsFeatureIds mSelectedFeatureIds;
380  QString mName;
381  QPointer< QgsVectorLayer > mLayer;
382 
383 };
384 
386 
387 #ifndef SIP_RUN
388 class QgsVectorLayerSelectedFeatureIterator : public QgsAbstractFeatureIterator
389 {
390  public:
391 
392  QgsVectorLayerSelectedFeatureIterator( const QgsFeatureIds &selectedFeatureIds,
393  const QgsFeatureRequest &request,
394  QgsVectorLayerFeatureSource &source );
395 
396  bool rewind() override;
397  bool close() override;
398 
399  protected:
400  bool fetchFeature( QgsFeature &f ) override;
401 
402  private:
403  QgsFeatureIds mSelectedFeatureIds;
404  QgsFeatureIterator mIterator;
405 
406 };
407 
408 #endif
409 
411 
412 #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 unique ID, geometry and a list of field...
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 engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
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:877
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:868
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...
std::shared_ptr< QgsVectorLayerFeatureSource > joinSource
Feature source for join.
int targetField
Index of field (of this layer) that drives the join.
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
int indexOffset
At what position the joined fields start.