QGIS API Documentation  3.17.0-Master (df2c9ff931)
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
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:41
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
Sets 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:55
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
friend bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool compileFailed() const
Indicator if there was an error when sending the compiled query to the server.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
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:126
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.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
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.
#define SIP_THROW(name)
Definition: qgis_sip.h:189
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
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.