QGIS API Documentation  2.99.0-Master (f867b65)
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 
31 {
32  public:
34  virtual bool mustStop() const = 0;
35 };
36 
40 class CORE_EXPORT QgsAbstractFeatureIterator
41 {
42  public:
43 
46  {
50  };
51 
54 
56  virtual ~QgsAbstractFeatureIterator() = default;
57 
59  virtual bool nextFeature( QgsFeature &f );
60 
62  virtual bool rewind() = 0;
64  virtual bool close() = 0;
65 
74  virtual void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) SIP_SKIP;
75 
79  CompileStatus compileStatus() const { return mCompileStatus; }
80 
81  protected:
82 
90  virtual bool fetchFeature( QgsFeature &f ) = 0;
91 
102  virtual bool nextFeatureFilterExpression( QgsFeature &f );
103 
115  virtual bool nextFeatureFilterFids( QgsFeature &f );
116 
125  void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
126 
127 
137  QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const;
138 
141 
143  bool mClosed;
144 
152  bool mZombie;
153 
156  int refs;
158  void ref();
160  void deref();
161  friend class QgsFeatureIterator;
162 
165 
168 
170  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
171 
172  private:
173  bool mUseCachedFeatures;
174  QList<QgsIndexedFeature> mCachedFeatures;
175  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
176 
178  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
179 
188  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
189 
196  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
197 };
198 
199 
204 template<typename T>
206 {
207  public:
208  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
209  : QgsAbstractFeatureIterator( request )
210  , mSource( source )
211  , mOwnSource( ownSource )
212  {
213  mSource->iteratorOpened( this );
214  }
215 
217  {
218  if ( mOwnSource )
219  delete mSource;
220  }
221 
222  protected:
224  void iteratorClosed() { mSource->iteratorClosed( this ); }
225 
226  T *mSource = nullptr;
228 };
229 
230 
235 class CORE_EXPORT QgsFeatureIterator
236 {
237  public:
238 
239 #ifdef SIP_RUN
240  QgsFeatureIterator *__iter__();
241  % MethodCode
242  sipRes = sipCpp;
243  % End
244 
245  SIP_PYOBJECT __next__();
246  % MethodCode
247  QgsFeature *f = new QgsFeature;
248  if ( sipCpp->nextFeature( *f ) )
249  sipRes = sipConvertFromType( f, sipType_QgsFeature, Py_None );
250  else
251  {
252  delete f;
253  PyErr_SetString( PyExc_StopIteration, "" );
254  }
255  % End
256 #endif
257 
260 #ifndef SIP_RUN
261  QgsFeatureIterator( QgsAbstractFeatureIterator *iter );
263 #endif
264  QgsFeatureIterator( const QgsFeatureIterator &fi );
267  ~QgsFeatureIterator();
268 
269  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
270 
271  bool nextFeature( QgsFeature &f );
272  bool rewind();
273  bool close();
274 
276  bool isClosed() const;
277 
285  void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker ) SIP_SKIP;
286 
290  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
291 
292  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
293  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
294 
295  protected:
296  QgsAbstractFeatureIterator *mIter = nullptr;
297 
298 
299 };
300 
301 #ifndef SIP_RUN
302 
304  : mIter( nullptr )
305 {
306 }
307 
309  : mIter( iter )
310 {
311  if ( iter )
312  iter->ref();
313 }
314 
316  : mIter( fi.mIter )
317 {
318  if ( mIter )
319  mIter->ref();
320 }
321 
323 {
324  if ( mIter )
325  mIter->deref();
326 }
327 
329 {
330  return mIter ? mIter->nextFeature( f ) : false;
331 }
332 
334 {
335  if ( mIter )
336  mIter->mFetchedCount = 0;
337 
338  return mIter ? mIter->rewind() : false;
339 }
340 
342 {
343  if ( mIter )
344  mIter->mFetchedCount = 0;
345 
346  return mIter ? mIter->close() : false;
347 }
348 
349 inline bool QgsFeatureIterator::isClosed() const
350 {
351  return mIter ? mIter->mClosed && !mIter->mZombie : true;
352 }
353 
354 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
355 {
356  return fi1.mIter == fi2.mIter;
357 }
358 
359 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
360 {
361  return !( fi1 == fi2 );
362 }
363 
365 {
366  if ( mIter )
367  mIter->setInterruptionChecker( interruptionChecker );
368 }
369 
370 #endif
371 
372 #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:38
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:61
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.