QGIS API Documentation  2.99.0-Master (d55fa22)
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 #ifndef SIP_RUN
23 
30 class CORE_EXPORT QgsInterruptionChecker
31 {
32  public:
34  virtual bool mustStop() const = 0;
35 };
36 
37 #endif
38 
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 
68 #ifndef SIP_RUN
69 
78  virtual void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker );
79 #endif
80 
84  CompileStatus compileStatus() const { return mCompileStatus; }
85 
86  protected:
87 
95  virtual bool fetchFeature( QgsFeature &f ) = 0;
96 
107  virtual bool nextFeatureFilterExpression( QgsFeature &f );
108 
120  virtual bool nextFeatureFilterFids( QgsFeature &f );
121 
124 
126  bool mClosed;
127 
135  bool mZombie;
136 
139  int refs;
141  void ref();
143  void deref();
144  friend class QgsFeatureIterator;
145 
148 
151 
153  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
154 
155  private:
156  bool mUseCachedFeatures;
157  QList<QgsIndexedFeature> mCachedFeatures;
158  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
159 
161  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
162 
171  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
172 
179  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
180 };
181 
182 #ifndef SIP_RUN
183 
188 template<typename T>
190 {
191  public:
192  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
193  : QgsAbstractFeatureIterator( request )
194  , mSource( source )
195  , mOwnSource( ownSource )
196  {
197  mSource->iteratorOpened( this );
198  }
199 
201  {
202  if ( mOwnSource )
203  delete mSource;
204  }
205 
206  protected:
208  void iteratorClosed() { mSource->iteratorClosed( this ); }
209 
210  T *mSource = nullptr;
212 };
213 
214 #endif
215 
220 class CORE_EXPORT QgsFeatureIterator
221 {
222  public:
223 
224 #ifdef SIP_RUN
225  QgsFeatureIterator *__iter__();
226  % MethodCode
227  sipRes = sipCpp;
228  % End
229 
230  SIP_PYOBJECT __next__();
231  % MethodCode
232  QgsFeature *f = new QgsFeature;
233  if ( sipCpp->nextFeature( *f ) )
234  sipRes = sipConvertFromType( f, sipType_QgsFeature, Py_None );
235  else
236  {
237  delete f;
238  PyErr_SetString( PyExc_StopIteration, "" );
239  }
240  % End
241 #endif
242 
245 #ifndef SIP_RUN
246  QgsFeatureIterator( QgsAbstractFeatureIterator *iter );
248 #endif
249  QgsFeatureIterator( const QgsFeatureIterator &fi );
252  ~QgsFeatureIterator();
253 
254  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
255 
256  bool nextFeature( QgsFeature &f );
257  bool rewind();
258  bool close();
259 
261  bool isClosed() const;
262 
263 #ifndef SIP_RUN
264 
272  void setInterruptionChecker( QgsInterruptionChecker *interruptionChecker );
273 
274 #endif
275 
279  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
280 
281 #ifndef SIP_RUN
282 
283  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
284  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
285 
286  protected:
287  QgsAbstractFeatureIterator *mIter = nullptr;
288 
289 #endif
290 
291 };
292 
293 #ifndef SIP_RUN
294 
296  : mIter( nullptr )
297 {
298 }
299 
301  : mIter( iter )
302 {
303  if ( iter )
304  iter->ref();
305 }
306 
308  : mIter( fi.mIter )
309 {
310  if ( mIter )
311  mIter->ref();
312 }
313 
315 {
316  if ( mIter )
317  mIter->deref();
318 }
319 
321 {
322  return mIter ? mIter->nextFeature( f ) : false;
323 }
324 
326 {
327  if ( mIter )
328  mIter->mFetchedCount = 0;
329 
330  return mIter ? mIter->rewind() : false;
331 }
332 
334 {
335  if ( mIter )
336  mIter->mFetchedCount = 0;
337 
338  return mIter ? mIter->close() : false;
339 }
340 
341 inline bool QgsFeatureIterator::isClosed() const
342 {
343  return mIter ? mIter->mClosed && !mIter->mZombie : true;
344 }
345 
346 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
347 {
348  return fi1.mIter == fi2.mIter;
349 }
350 
351 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
352 {
353  return !( fi1 == fi2 );
354 }
355 
357 {
358  if ( mIter )
359  mIter->setInterruptionChecker( interruptionChecker );
360 }
361 
362 #endif
363 
364 #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
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
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
~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.