QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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
Helper template that cares of two things: 1.
void iteratorClosed()
to be called by from subclass in close()
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
Internal feature iterator to be implemented within data providers.
virtual bool rewind()=0
reset the iterator to the starting position
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
virtual void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!...
virtual bool close()=0
end of iterating: free the resources / lock
CompileStatus
Status of expression compilation for filter expression requests.
@ PartiallyCompiled
Expression was partially compiled, but extra checks need to be applied to features.
@ Compiled
Expression was fully compiled and delegated to data provider source.
@ NoCompilation
Expression could not be compiled or not attempt was made to compile expression.
void deref()
Remove reference, delete if refs == 0.
virtual ~QgsAbstractFeatureIterator()=default
destructor makes sure that the iterator is closed properly
QgsFeatureRequest mRequest
A copy of the feature request.
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
long mFetchedCount
Number of features already fetched by iterator.
virtual bool isValid() const
Returns if this iterator is valid.
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
bool mClosed
Sets to true, as soon as the iterator is closed.
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
~QgsFeatureIterator()
Destructor deletes the iterator if it has no more references.
QgsAbstractFeatureIterator * mIter
bool isClosed() const
find out whether the iterator is still valid or closed already
void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
bool compileFailed() const
Indicator if there was an error when sending the compiled query to the server.
QgsFeatureIterator()=default
Construct invalid iterator.
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)