QGIS API Documentation  2.99.0-Master (d8402ac)
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 
95  virtual bool isValid() const
96  {
97  return mValid;
98  }
99 
100  protected:
101 
109  virtual bool fetchFeature( QgsFeature &f ) = 0;
110 
121  virtual bool nextFeatureFilterExpression( QgsFeature &f );
122 
134  virtual bool nextFeatureFilterFids( QgsFeature &f );
135 
144  void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
145 
146 
156  QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const;
157 
160 
162  bool mClosed = false;
163 
171  bool mZombie = false;
172 
177  int refs = 0;
179  void ref();
181  void deref();
182  friend class QgsFeatureIterator;
183 
185  long mFetchedCount = 0;
186 
188  CompileStatus mCompileStatus = NoCompilation;
189 
191  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
192 
201  bool mValid = true;
202 
203  private:
204  bool mUseCachedFeatures = false;
205  QList<QgsIndexedFeature> mCachedFeatures;
206  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
207 
209  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
210 
219  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
220 
227  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
228 };
229 
230 
236 template<typename T>
238 {
239  public:
240  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
241  : QgsAbstractFeatureIterator( request )
242  , mSource( source )
243  , mOwnSource( ownSource )
244  {
245  mSource->iteratorOpened( this );
246  }
247 
249  {
250  if ( mOwnSource )
251  delete mSource;
252  }
253 
254  protected:
256  void iteratorClosed() { mSource->iteratorClosed( this ); }
257 
258  T *mSource = nullptr;
260 };
261 
262 
267 class CORE_EXPORT QgsFeatureIterator
268 {
269  public:
270 
271 #ifdef SIP_RUN
272  QgsFeatureIterator *__iter__();
273  % MethodCode
274  sipRes = sipCpp;
275  % End
276 
277  SIP_PYOBJECT __next__();
278  % MethodCode
279  QgsFeature *f = new QgsFeature;
280  if ( sipCpp->nextFeature( *f ) )
281  sipRes = sipConvertFromType( f, sipType_QgsFeature, Py_None );
282  else
283  {
284  delete f;
285  PyErr_SetString( PyExc_StopIteration, "" );
286  }
287  % End
288 #endif
289 
291  QgsFeatureIterator() = default;
292 #ifndef SIP_RUN
295 #endif
300 
301  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
302 
303  bool nextFeature( QgsFeature &f );
304  bool rewind();
305  bool close();
306 
316  virtual bool isValid() const;
317 
319  bool isClosed() const;
320 
329  void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) SIP_SKIP;
330 
335  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
336 
337  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
338  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
339 
340  protected:
341  QgsAbstractFeatureIterator *mIter = nullptr;
342 
343 
344 };
345 
346 #ifndef SIP_RUN
347 
349  : mIter( iter )
350 {
351  if ( iter )
352  iter->ref();
353 }
354 
356  : mIter( fi.mIter )
357 {
358  if ( mIter )
359  mIter->ref();
360 }
361 
363 {
364  if ( mIter )
365  mIter->deref();
366 }
367 
369 {
370  return mIter ? mIter->nextFeature( f ) : false;
371 }
372 
374 {
375  if ( mIter )
376  mIter->mFetchedCount = 0;
377 
378  return mIter ? mIter->rewind() : false;
379 }
380 
382 {
383  if ( mIter )
384  mIter->mFetchedCount = 0;
385 
386  return mIter ? mIter->close() : false;
387 }
388 
389 inline bool QgsFeatureIterator::isClosed() const
390 {
391  return mIter ? mIter->mClosed && !mIter->mZombie : true;
392 }
393 
394 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
395 {
396  return fi1.mIter == fi2.mIter;
397 }
398 
399 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
400 {
401  return !( fi1 == fi2 );
402 }
403 
405 {
406  if ( mIter )
407  mIter->setInterruptionChecker( interruptionChecker );
408 }
409 
410 #endif
411 
412 #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)
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.
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)...
QgsFeatureIterator()=default
construct invalid iterator
void deref()
Remove reference, delete if refs == 0.
friend bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual bool isValid() const
Returns if this iterator is valid.
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.
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.