QGIS API Documentation  3.0.2-Girona (307d082)
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 class QgsFeedback;
23 
28 class CORE_EXPORT QgsAbstractFeatureIterator
29 {
30  public:
31 
34  {
38  };
39 
42 
44  virtual ~QgsAbstractFeatureIterator() = default;
45 
47  virtual bool nextFeature( QgsFeature &f );
48 
50  virtual bool rewind() = 0;
52  virtual bool close() = 0;
53 
63  virtual void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
64 
69  CompileStatus compileStatus() const { return mCompileStatus; }
70 
81  virtual bool isValid() const
82  {
83  return mValid;
84  }
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 
130  void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
131 
132 
142  QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const;
143 
146 
148  bool mClosed = false;
149 
157  bool mZombie = false;
158 
163  int refs = 0;
165  void ref();
167  void deref();
168  friend class QgsFeatureIterator;
169 
171  long mFetchedCount = 0;
172 
174  CompileStatus mCompileStatus = NoCompilation;
175 
177  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
178 
187  bool mValid = true;
188 
189  private:
190  bool mUseCachedFeatures = false;
191  QList<QgsIndexedFeature> mCachedFeatures;
192  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
193 
195  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
196 
205  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
206 
213  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
214 };
215 
216 
222 template<typename T>
224 {
225  public:
226  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
227  : QgsAbstractFeatureIterator( request )
228  , mSource( source )
229  , mOwnSource( ownSource )
230  {
231  mSource->iteratorOpened( this );
232  }
233 
235  {
236  if ( mOwnSource )
237  delete mSource;
238  }
239 
240  protected:
242  void iteratorClosed() { mSource->iteratorClosed( this ); }
243 
244  T *mSource = nullptr;
246 };
247 
248 
253 class CORE_EXPORT QgsFeatureIterator
254 {
255  public:
256 
257 #ifdef SIP_RUN
258  QgsFeatureIterator *__iter__();
259  % MethodCode
260  sipRes = sipCpp;
261  % End
262 
263  SIP_PYOBJECT __next__();
264  % MethodCode
265  std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
266  bool result = false;
267  Py_BEGIN_ALLOW_THREADS
268  result = ( sipCpp->nextFeature( *f ) );
269  Py_END_ALLOW_THREADS
270  if ( result )
271  sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
272  else
273  {
274  PyErr_SetString( PyExc_StopIteration, "" );
275  }
276  % End
277 #endif
278 
280  QgsFeatureIterator() = default;
281 #ifndef SIP_RUN
284 #endif
289 
290  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
291 
292  bool nextFeature( QgsFeature &f );
293  bool rewind();
294  bool close();
295 
305  virtual bool isValid() const;
306 
308  bool isClosed() const;
309 
318  void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
319 
324  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
325 
326  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
327  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
328 
329  protected:
330  QgsAbstractFeatureIterator *mIter = nullptr;
331 
332 
333 };
334 
335 #ifndef SIP_RUN
336 
338  : mIter( iter )
339 {
340  if ( iter )
341  iter->ref();
342 }
343 
345  : mIter( fi.mIter )
346 {
347  if ( mIter )
348  mIter->ref();
349 }
350 
352 {
353  if ( mIter )
354  mIter->deref();
355 }
356 
358 {
359  return mIter ? mIter->nextFeature( f ) : false;
360 }
361 
363 {
364  if ( mIter )
365  mIter->mFetchedCount = 0;
366 
367  return mIter ? mIter->rewind() : false;
368 }
369 
371 {
372  if ( mIter )
373  mIter->mFetchedCount = 0;
374 
375  return mIter ? mIter->close() : false;
376 }
377 
378 inline bool QgsFeatureIterator::isClosed() const
379 {
380  return mIter ? mIter->mClosed && !mIter->mZombie : true;
381 }
382 
383 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
384 {
385  return fi1.mIter == fi2.mIter;
386 }
387 
388 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
389 {
390  return !( fi1 == fi2 );
391 }
392 
393 inline void QgsFeatureIterator::setInterruptionChecker( QgsFeedback *interruptionChecker )
394 {
395  if ( mIter )
396  mIter->setInterruptionChecker( interruptionChecker );
397 }
398 
399 #endif
400 
401 #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)
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
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.
void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
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.
Expression was partially compiled, but extra checks need to be applied to features.
QgsAbstractFeatureIterator * mIter
Class for doing transforms between two map coordinate systems.
virtual void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
~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)
Helper template that cares of two things: 1.