QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
92  bool compileFailed() const;
93 
94  protected:
95 
103  virtual bool fetchFeature( QgsFeature &f ) = 0;
104 
115  virtual bool nextFeatureFilterExpression( QgsFeature &f );
116 
128  virtual bool nextFeatureFilterFids( QgsFeature &f );
129 
138  void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
139 
140 
150  QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
151 
154 
156  bool mClosed = false;
157 
165  bool mZombie = false;
166 
167  // TODO QGIS 4: make this private
168 
172  int refs = 0;
174  void ref();
176  void deref();
177  friend class QgsFeatureIterator;
178 
180  long mFetchedCount = 0;
181 
183  CompileStatus mCompileStatus = NoCompilation;
184 
185  bool mCompileFailed = false;
186 
188  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
189 
198  bool mValid = true;
199 
200  private:
201  bool mUseCachedFeatures = false;
202  QList<QgsIndexedFeature> mCachedFeatures;
203  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
204 
206  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
207 
216  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
217 
224  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
225 };
226 
227 
233 template<typename T>
235 {
236  public:
237  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
238  : QgsAbstractFeatureIterator( request )
239  , mSource( source )
240  , mOwnSource( ownSource )
241  {
242  mSource->iteratorOpened( this );
243  }
244 
246  {
247  if ( mOwnSource )
248  delete mSource;
249  }
250 
251  protected:
253  void iteratorClosed() { mSource->iteratorClosed( this ); }
254 
255  T *mSource = nullptr;
257 };
258 
259 
264 class CORE_EXPORT QgsFeatureIterator
265 {
266  public:
267 
268 #ifdef SIP_RUN
269  QgsFeatureIterator *__iter__();
270  % MethodCode
271  sipRes = sipCpp;
272  % End
273 
274  SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
275  % MethodCode
276  std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
277  bool result = false;
278  Py_BEGIN_ALLOW_THREADS
279  result = ( sipCpp->nextFeature( *f ) );
280  Py_END_ALLOW_THREADS
281  if ( result )
282  sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
283  else
284  {
285  PyErr_SetString( PyExc_StopIteration, "" );
286  }
287  % End
288 #endif
289 
291  QgsFeatureIterator() = default;
298 
299  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
300 
301  bool nextFeature( QgsFeature &f );
302  bool rewind();
303  bool close();
304 
314  bool isValid() const;
315 
317  bool isClosed() const;
318 
327  void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
328 
333  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
334 
341  bool compileFailed() const { return mIter->compileFailed(); }
342 
343  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
344  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
345 
346  protected:
347  QgsAbstractFeatureIterator *mIter = nullptr;
348 
349 
350 };
351 
352 #ifndef SIP_RUN
353 
355  : mIter( iter )
356 {
357  if ( iter )
358  iter->ref();
359 }
360 
362  : mIter( fi.mIter )
363 {
364  if ( mIter )
365  mIter->ref();
366 }
367 
369 {
370  if ( mIter )
371  mIter->deref();
372 }
373 
375 {
376  return mIter ? mIter->nextFeature( f ) : false;
377 }
378 
380 {
381  if ( mIter )
382  mIter->mFetchedCount = 0;
383 
384  return mIter ? mIter->rewind() : false;
385 }
386 
388 {
389  if ( mIter )
390  mIter->mFetchedCount = 0;
391 
392  return mIter ? mIter->close() : false;
393 }
394 
395 inline bool QgsFeatureIterator::isClosed() const
396 {
397  return mIter ? mIter->mClosed && !mIter->mZombie : true;
398 }
399 
400 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
401 {
402  return fi1.mIter == fi2.mIter;
403 }
404 
405 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
406 {
407  return !( fi1 == fi2 );
408 }
409 
410 inline void QgsFeatureIterator::setInterruptionChecker( QgsFeedback *interruptionChecker )
411 {
412  if ( mIter )
413  mIter->setInterruptionChecker( interruptionChecker );
414 }
415 
416 #endif
417 
418 #endif // QGSFEATUREITERATOR_H
QgsAbstractFeatureIterator::Compiled
@ Compiled
Expression was fully compiled and delegated to data provider source.
Definition: qgsfeatureiterator.h:37
qgsfeaturerequest.h
QgsSimplifyMethod
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Definition: qgssimplifymethod.h:29
QgsAbstractFeatureIterator::setInterruptionChecker
virtual void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
Definition: qgsfeatureiterator.cpp:220
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:400
QgsAbstractFeatureIterator::isValid
virtual bool isValid() const
Returns if this iterator is valid.
Definition: qgsfeatureiterator.h:81
QgsAbstractFeatureIteratorFromSource::iteratorClosed
void iteratorClosed()
to be called by from subclass in close()
Definition: qgsfeatureiterator.h:253
QgsAbstractFeatureIterator::mClosed
bool mClosed
Sets to true, as soon as the iterator is closed.
Definition: qgsfeatureiterator.h:156
QgsAbstractFeatureIterator::mFetchedCount
long mFetchedCount
Number of features already fetched by iterator.
Definition: qgsfeatureiterator.h:180
QgsAbstractFeatureIteratorFromSource::mSource
T * mSource
Definition: qgsfeatureiterator.h:255
QgsAbstractFeatureIterator::mRequest
QgsFeatureRequest mRequest
A copy of the feature request.
Definition: qgsfeatureiterator.h:153
QgsAbstractFeatureIteratorFromSource::mOwnSource
bool mOwnSource
Definition: qgsfeatureiterator.h:256
QgsFeatureIterator::isClosed
bool isClosed() const
find out whether the iterator is still valid or closed already
Definition: qgsfeatureiterator.h:395
QgsAbstractFeatureIterator::nextFeature
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
Definition: qgsfeatureiterator.cpp:27
QgsFeatureIterator::compileFailed
bool compileFailed() const
Indicator if there was an error when sending the compiled query to the server.
Definition: qgsfeatureiterator.h:341
QgsFeatureIterator::compileStatus
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Definition: qgsfeatureiterator.h:333
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:405
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsAbstractFeatureIteratorFromSource::QgsAbstractFeatureIteratorFromSource
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
Definition: qgsfeatureiterator.h:237
QgsAbstractFeatureIterator::~QgsAbstractFeatureIterator
virtual ~QgsAbstractFeatureIterator()=default
destructor makes sure that the iterator is closed properly
SIP_TYPEHINT
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
QgsAbstractFeatureIterator::ref
void ref()
Add reference.
Definition: qgsfeatureiterator.cpp:126
QgsAbstractFeatureIteratorFromSource
Helper template that cares of two things: 1.
Definition: qgsfeatureiterator.h:235
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsAbstractFeatureIterator::mZombie
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
Definition: qgsfeatureiterator.h:165
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:189
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:387
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsAbstractFeatureIteratorFromSource::~QgsAbstractFeatureIteratorFromSource
~QgsAbstractFeatureIteratorFromSource() override
Definition: qgsfeatureiterator.h:245
QgsAbstractFeatureIterator::PartiallyCompiled
@ PartiallyCompiled
Expression was partially compiled, but extra checks need to be applied to features.
Definition: qgsfeatureiterator.h:36
QgsFeatureIterator::~QgsFeatureIterator
~QgsFeatureIterator()
Destructor deletes the iterator if it has no more references.
Definition: qgsfeatureiterator.h:368
QgsAbstractFeatureIterator::rewind
virtual bool rewind()=0
reset the iterator to the starting position
QgsAbstractFeatureIterator::CompileStatus
CompileStatus
Status of expression compilation for filter expression requests.
Definition: qgsfeatureiterator.h:34
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:374
qgsindexedfeature.h
QgsAbstractFeatureIterator::fetchFeature
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!...
QgsAbstractFeatureIterator::deref
void deref()
Remove reference, delete if refs == 0.
Definition: qgsfeatureiterator.cpp:143
QgsSimplifyMethod::MethodType
MethodType
Definition: qgssimplifymethod.h:32
QgsFeatureIterator::QgsFeatureIterator
QgsFeatureIterator()=default
Construct invalid iterator.
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFeatureIterator::setInterruptionChecker
void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
Definition: qgsfeatureiterator.h:410
QgsAbstractFeatureIterator::NoCompilation
@ NoCompilation
Expression could not be compiled or not attempt was made to compile expression.
Definition: qgsfeatureiterator.h:35
QgsFeatureIterator::mIter
QgsAbstractFeatureIterator * mIter
Definition: qgsfeatureiterator.h:347
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
QgsAbstractFeatureIterator::compileStatus
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Definition: qgsfeatureiterator.h:69
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265
QgsAbstractFeatureIterator
Internal feature iterator to be implemented within data providers.
Definition: qgsfeatureiterator.h:29
QgsAbstractFeatureIterator::close
virtual bool close()=0
end of iterating: free the resources / lock
QgsFeatureIterator::rewind
bool rewind()
Definition: qgsfeatureiterator.h:379