QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
99  enum class RequestToSourceCrsResult : int
100  {
101  Success,
102  DistanceWithinMustBeCheckedManually,
103  };
104 
105  protected:
106 
114  virtual bool fetchFeature( QgsFeature &f ) = 0;
115 
126  virtual bool nextFeatureFilterExpression( QgsFeature &f );
127 
139  virtual bool nextFeatureFilterFids( QgsFeature &f );
140 
149  void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
150 
151 
161  QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
162 
175  RequestToSourceCrsResult updateRequestToSourceCrs( QgsFeatureRequest &request, const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
176 
179 
181  bool mClosed = false;
182 
190  bool mZombie = false;
191 
192  // TODO QGIS 4: make this private
193 
197  int refs = 0;
199  void ref();
201  void deref();
202  friend class QgsFeatureIterator;
203 
205  long long mFetchedCount = 0;
206 
208  CompileStatus mCompileStatus = NoCompilation;
209 
210  bool mCompileFailed = false;
211 
213  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
214 
223  bool mValid = true;
224 
225  private:
226  bool mUseCachedFeatures = false;
227  QList<QgsIndexedFeature> mCachedFeatures;
228  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
229 
231  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
232 
241  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
242 
249  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
250 };
251 
252 
258 template<typename T>
260 {
261  public:
262  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
263  : QgsAbstractFeatureIterator( request )
264  , mSource( source )
265  , mOwnSource( ownSource )
266  {
267  mSource->iteratorOpened( this );
268  }
269 
271  {
272  if ( mOwnSource )
273  delete mSource;
274  }
275 
276  protected:
278  void iteratorClosed() { mSource->iteratorClosed( this ); }
279 
280  T *mSource = nullptr;
282 };
283 
284 
289 class CORE_EXPORT QgsFeatureIterator
290 {
291  public:
292 
293 #ifdef SIP_RUN
294  QgsFeatureIterator *__iter__();
295  % MethodCode
296  sipRes = sipCpp;
297  % End
298 
299  SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
300  % MethodCode
301  std::unique_ptr< QgsFeature > f = std::make_unique< QgsFeature >();
302  bool result = false;
303  Py_BEGIN_ALLOW_THREADS
304  result = ( sipCpp->nextFeature( *f ) );
305  Py_END_ALLOW_THREADS
306  if ( result )
307  sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
308  else
309  {
310  PyErr_SetString( PyExc_StopIteration, "" );
311  }
312  % End
313 #endif
314 
316  QgsFeatureIterator() = default;
323 
324  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
325 
326  bool nextFeature( QgsFeature &f );
327  bool rewind();
328  bool close();
329 
339  bool isValid() const;
340 
342  bool isClosed() const;
343 
352  void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
353 
358  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
359 
366  bool compileFailed() const { return mIter->compileFailed(); }
367 
368  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
369  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
370 
371  protected:
372  QgsAbstractFeatureIterator *mIter = nullptr;
373 
374 
375 };
376 
377 #ifndef SIP_RUN
378 
380  : mIter( iter )
381 {
382  if ( iter )
383  iter->ref();
384 }
385 
387  : mIter( fi.mIter )
388 {
389  if ( mIter )
390  mIter->ref();
391 }
392 
394 {
395  if ( mIter )
396  mIter->deref();
397 }
398 
400 {
401  return mIter ? mIter->nextFeature( f ) : false;
402 }
403 
405 {
406  if ( mIter )
407  mIter->mFetchedCount = 0;
408 
409  return mIter ? mIter->rewind() : false;
410 }
411 
413 {
414  if ( mIter )
415  mIter->mFetchedCount = 0;
416 
417  return mIter ? mIter->close() : false;
418 }
419 
420 inline bool QgsFeatureIterator::isClosed() const
421 {
422  return mIter ? mIter->mClosed && !mIter->mZombie : true;
423 }
424 
425 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
426 {
427  return fi1.mIter == fi2.mIter;
428 }
429 
430 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
431 {
432  return !( fi1 == fi2 );
433 }
434 
435 inline void QgsFeatureIterator::setInterruptionChecker( QgsFeedback *interruptionChecker )
436 {
437  if ( mIter )
438  mIter->setInterruptionChecker( interruptionChecker );
439 }
440 
441 #endif
442 
443 #endif // QGSFEATUREITERATOR_H
QgsAbstractFeatureIterator::Compiled
@ Compiled
Expression was fully compiled and delegated to data provider source.
Definition: qgsfeatureiterator.h:37
QgsAbstractFeatureIterator::RequestToSourceCrsResult
RequestToSourceCrsResult
Possible results from the updateRequestToSourceCrs() method.
Definition: qgsfeatureiterator.h:99
qgsfeaturerequest.h
QgsSimplifyMethod
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Definition: qgssimplifymethod.h:28
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:261
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
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:278
QgsAbstractFeatureIterator::mClosed
bool mClosed
Sets to true, as soon as the iterator is closed.
Definition: qgsfeatureiterator.h:181
QgsAbstractFeatureIterator::mFetchedCount
long long mFetchedCount
Number of features already fetched by iterator.
Definition: qgsfeatureiterator.h:205
QgsAbstractFeatureIteratorFromSource::mSource
T * mSource
Definition: qgsfeatureiterator.h:280
QgsAbstractFeatureIterator::mRequest
QgsFeatureRequest mRequest
A copy of the feature request.
Definition: qgsfeatureiterator.h:178
QgsAbstractFeatureIteratorFromSource::mOwnSource
bool mOwnSource
Definition: qgsfeatureiterator.h:281
QgsFeatureIterator::isClosed
bool isClosed() const
find out whether the iterator is still valid or closed already
Definition: qgsfeatureiterator.h:420
QgsAbstractFeatureIterator::nextFeature
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
Definition: qgsfeatureiterator.cpp:30
QgsFeatureIterator::compileFailed
bool compileFailed() const
Indicator if there was an error when sending the compiled query to the server.
Definition: qgsfeatureiterator.h:366
QgsFeatureIterator::compileStatus
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Definition: qgsfeatureiterator.h:358
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:430
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsAbstractFeatureIteratorFromSource::QgsAbstractFeatureIteratorFromSource
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
Definition: qgsfeatureiterator.h:262
SIP_TYPEHINT
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:227
QgsAbstractFeatureIterator::ref
void ref()
Add reference.
Definition: qgsfeatureiterator.cpp:167
QgsAbstractFeatureIteratorFromSource
Helper template that cares of two things: 1. automatic deletion of source if owned by iterator,...
Definition: qgsfeatureiterator.h:259
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsAbstractFeatureIterator::mZombie
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
Definition: qgsfeatureiterator.h:190
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:412
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:270
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:393
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:33
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
qgsindexedfeature.h
QgsAbstractFeatureIterator::deref
void deref()
Remove reference, delete if refs == 0.
Definition: qgsfeatureiterator.cpp:184
QgsSimplifyMethod::MethodType
MethodType
Definition: qgssimplifymethod.h:31
QgsFeatureIterator::QgsFeatureIterator
QgsFeatureIterator()=default
Construct invalid iterator.
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
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:435
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:372
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
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:289
QgsAbstractFeatureIterator
Internal feature iterator to be implemented within data providers.
Definition: qgsfeatureiterator.h:28
QgsAbstractFeatureIterator::close
virtual bool close()=0
end of iterating: free the resources / lock
QgsFeatureIterator::rewind
bool rewind()
Definition: qgsfeatureiterator.h:404