QGIS API Documentation  2.99.0-Master (25b0421)
qgsfeatureiterator.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeatureiterator.h
3  ---------------------
4  begin : Juli 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 QGSFEATUREITERATOR_H
16 #define QGSFEATUREITERATOR_H
17 
18 #include "qgis_core.h"
19 #include "qgsfeaturerequest.h"
20 #include "qgsindexedfeature.h"
21 
22 
23 
32 {
33  public:
35  virtual bool mustStop() const = 0;
36 };
37 
42 class CORE_EXPORT QgsAbstractFeatureIterator
43 {
44  public:
45 
48  {
52  };
53 
56 
58  virtual ~QgsAbstractFeatureIterator() = default;
59 
61  virtual bool nextFeature( QgsFeature &f );
62 
64  virtual bool rewind() = 0;
66  virtual bool close() = 0;
67 
77  virtual void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) SIP_SKIP;
78 
83  CompileStatus compileStatus() const { return mCompileStatus; }
84 
85  protected:
86 
94  virtual bool fetchFeature( QgsFeature &f ) = 0;
95 
106  virtual bool nextFeatureFilterExpression( QgsFeature &f );
107 
119  virtual bool nextFeatureFilterFids( QgsFeature &f );
120 
129  void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
130 
131 
141  QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const;
142 
145 
147  bool mClosed;
148 
156  bool mZombie;
157 
162  int refs;
164  void ref();
166  void deref();
167  friend class QgsFeatureIterator;
168 
171 
174 
176  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
177 
178  private:
179  bool mUseCachedFeatures;
180  QList<QgsIndexedFeature> mCachedFeatures;
181  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
182 
184  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
185 
194  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
195 
202  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
203 };
204 
205 
211 template<typename T>
213 {
214  public:
215  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
216  : QgsAbstractFeatureIterator( request )
217  , mSource( source )
218  , mOwnSource( ownSource )
219  {
220  mSource->iteratorOpened( this );
221  }
222 
224  {
225  if ( mOwnSource )
226  delete mSource;
227  }
228 
229  protected:
231  void iteratorClosed() { mSource->iteratorClosed( this ); }
232 
233  T *mSource = nullptr;
235 };
236 
237 
242 class CORE_EXPORT QgsFeatureIterator
243 {
244  public:
245 
246 #ifdef SIP_RUN
247  QgsFeatureIterator *__iter__();
248  % MethodCode
249  sipRes = sipCpp;
250  % End
251 
252  SIP_PYOBJECT __next__();
253  % MethodCode
254  QgsFeature *f = new QgsFeature;
255  if ( sipCpp->nextFeature( *f ) )
256  sipRes = sipConvertFromType( f, sipType_QgsFeature, Py_None );
257  else
258  {
259  delete f;
260  PyErr_SetString( PyExc_StopIteration, "" );
261  }
262  % End
263 #endif
264 
267 #ifndef SIP_RUN
268  QgsFeatureIterator( QgsAbstractFeatureIterator *iter );
270 #endif
271  QgsFeatureIterator( const QgsFeatureIterator &fi );
274  ~QgsFeatureIterator();
275 
276  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
277 
278  bool nextFeature( QgsFeature &f );
279  bool rewind();
280  bool close();
281 
283  bool isClosed() const;
284 
293  void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) SIP_SKIP;
294 
299  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
300 
301  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
302  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
303 
304  protected:
305  QgsAbstractFeatureIterator *mIter = nullptr;
306 
307 
308 };
309 
310 #ifndef SIP_RUN
311 
313 {
314 }
315 
317  : mIter( iter )
318 {
319  if ( iter )
320  iter->ref();
321 }
322 
324  : mIter( fi.mIter )
325 {
326  if ( mIter )
327  mIter->ref();
328 }
329 
331 {
332  if ( mIter )
333  mIter->deref();
334 }
335 
337 {
338  return mIter ? mIter->nextFeature( f ) : false;
339 }
340 
342 {
343  if ( mIter )
344  mIter->mFetchedCount = 0;
345 
346  return mIter ? mIter->rewind() : false;
347 }
348 
350 {
351  if ( mIter )
352  mIter->mFetchedCount = 0;
353 
354  return mIter ? mIter->close() : false;
355 }
356 
357 inline bool QgsFeatureIterator::isClosed() const
358 {
359  return mIter ? mIter->mClosed && !mIter->mZombie : true;
360 }
361 
362 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
363 {
364  return fi1.mIter == fi2.mIter;
365 }
366 
367 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
368 {
369  return !( fi1 == fi2 );
370 }
371 
373 {
374  if ( mIter )
375  mIter->setInterruptionChecker( interruptionChecker );
376 }
377 
378 #endif
379 
380 #endif // QGSFEATUREITERATOR_H
Wrapper for iterator of features from vector data provider or vector layer.
virtual bool close()=0
end of iterating: free the resources / lock
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Expression could not be compiled or not attempt was made to compile expression.
long mFetchedCount
Number of features already fetched by iterator.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual bool rewind()=0
reset the iterator to the starting position
Expression was fully compiled and delegated to data provider source.
bool mClosed
Set to true, as soon as the iterator is closed.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
friend bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
CompileStatus mCompileStatus
Status of compilation of filter expression.
Interface that can be optionally attached to an iterator so its nextFeature() implementaton can check...
void iteratorClosed()
to be called by from subclass in close()
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
#define SIP_SKIP
Definition: qgis_sip.h:119
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Internal feature iterator to be implemented within data providers.
CompileStatus
Status of expression compilation for filter expression requests.
QgsFeatureIterator()
construct invalid iterator
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void deref()
Remove reference, delete if refs == 0.
friend bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsFeatureRequest mRequest
A copy of the feature request.
virtual void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
Expression was partially compiled, but extra checks need to be applied to features.
QgsAbstractFeatureIterator * mIter
Class for doing transforms between two map coordinate systems.
~QgsFeatureIterator()
destructor deletes the iterator if it has no more references
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
int refs
reference counting (to allow seamless copying of QgsFeatureIterator instances) TODO QGIS3: make this ...
bool nextFeature(QgsFeature &f)
bool isClosed() const
find out whether the iterator is still valid or closed already
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
Helper template that cares of two things: 1.