QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
171  int refs = 0;
173  void ref();
175  void deref();
176  friend class QgsFeatureIterator;
177 
179  long mFetchedCount = 0;
180 
182  CompileStatus mCompileStatus = NoCompilation;
183 
184  bool mCompileFailed = false;
185 
187  virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
188 
197  bool mValid = true;
198 
199  private:
200  bool mUseCachedFeatures = false;
201  QList<QgsIndexedFeature> mCachedFeatures;
202  QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
203 
205  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
206 
215  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
216 
223  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
224 };
225 
226 
232 template<typename T>
234 {
235  public:
236  QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
237  : QgsAbstractFeatureIterator( request )
238  , mSource( source )
239  , mOwnSource( ownSource )
240  {
241  mSource->iteratorOpened( this );
242  }
243 
245  {
246  if ( mOwnSource )
247  delete mSource;
248  }
249 
250  protected:
252  void iteratorClosed() { mSource->iteratorClosed( this ); }
253 
254  T *mSource = nullptr;
256 };
257 
258 
263 class CORE_EXPORT QgsFeatureIterator
264 {
265  public:
266 
267 #ifdef SIP_RUN
268  QgsFeatureIterator *__iter__();
269  % MethodCode
270  sipRes = sipCpp;
271  % End
272 
273  SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
274  % MethodCode
275  std::unique_ptr< QgsFeature > f = qgis::make_unique< QgsFeature >();
276  bool result = false;
277  Py_BEGIN_ALLOW_THREADS
278  result = ( sipCpp->nextFeature( *f ) );
279  Py_END_ALLOW_THREADS
280  if ( result )
281  sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
282  else
283  {
284  PyErr_SetString( PyExc_StopIteration, "" );
285  }
286  % End
287 #endif
288 
290  QgsFeatureIterator() = default;
297 
298  QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
299 
300  bool nextFeature( QgsFeature &f );
301  bool rewind();
302  bool close();
303 
313  bool isValid() const;
314 
316  bool isClosed() const;
317 
326  void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
327 
332  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
333 
340  bool compileFailed() const { return mIter->compileFailed(); }
341 
342  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
343  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
344 
345  protected:
346  QgsAbstractFeatureIterator *mIter = nullptr;
347 
348 
349 };
350 
351 #ifndef SIP_RUN
352 
354  : mIter( iter )
355 {
356  if ( iter )
357  iter->ref();
358 }
359 
361  : mIter( fi.mIter )
362 {
363  if ( mIter )
364  mIter->ref();
365 }
366 
368 {
369  if ( mIter )
370  mIter->deref();
371 }
372 
374 {
375  return mIter ? mIter->nextFeature( f ) : false;
376 }
377 
379 {
380  if ( mIter )
381  mIter->mFetchedCount = 0;
382 
383  return mIter ? mIter->rewind() : false;
384 }
385 
387 {
388  if ( mIter )
389  mIter->mFetchedCount = 0;
390 
391  return mIter ? mIter->close() : false;
392 }
393 
394 inline bool QgsFeatureIterator::isClosed() const
395 {
396  return mIter ? mIter->mClosed && !mIter->mZombie : true;
397 }
398 
399 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
400 {
401  return fi1.mIter == fi2.mIter;
402 }
403 
404 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
405 {
406  return !( fi1 == fi2 );
407 }
408 
409 inline void QgsFeatureIterator::setInterruptionChecker( QgsFeedback *interruptionChecker )
410 {
411  if ( mIter )
412  mIter->setInterruptionChecker( interruptionChecker );
413 }
414 
415 #endif
416 
417 #endif // QGSFEATUREITERATOR_H
QgsAbstractFeatureIterator::Compiled
@ Compiled
Expression was fully compiled and delegated to data provider source.
Definition: qgsfeatureiterator.h:37
qgsfeaturerequest.h
QgsSimplifyMethod
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:220
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:399
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:252
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:179
QgsAbstractFeatureIteratorFromSource::mSource
T * mSource
Definition: qgsfeatureiterator.h:254
QgsAbstractFeatureIterator::mRequest
QgsFeatureRequest mRequest
A copy of the feature request.
Definition: qgsfeatureiterator.h:153
QgsAbstractFeatureIteratorFromSource::mOwnSource
bool mOwnSource
Definition: qgsfeatureiterator.h:255
QgsFeatureIterator::isClosed
bool isClosed() const
find out whether the iterator is still valid or closed already
Definition: qgsfeatureiterator.h:394
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:340
QgsFeatureIterator::compileStatus
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Definition: qgsfeatureiterator.h:332
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:404
QgsRectangle
Definition: qgsrectangle.h:41
QgsAbstractFeatureIteratorFromSource::QgsAbstractFeatureIteratorFromSource
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
Definition: qgsfeatureiterator.h:236
SIP_TYPEHINT
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:213
QgsAbstractFeatureIterator::ref
void ref()
Add reference.
Definition: qgsfeatureiterator.cpp:126
QgsAbstractFeatureIteratorFromSource
Definition: qgsfeatureiterator.h:233
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsAbstractFeatureIterator::mZombie
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
Definition: qgsfeatureiterator.h:165
QgsCsException
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:386
QgsFeedback
Definition: qgsfeedback.h:43
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsAbstractFeatureIteratorFromSource::~QgsAbstractFeatureIteratorFromSource
~QgsAbstractFeatureIteratorFromSource() override
Definition: qgsfeatureiterator.h:244
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:367
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:373
qgsindexedfeature.h
QgsAbstractFeatureIterator::deref
void deref()
Remove reference, delete if refs == 0.
Definition: qgsfeatureiterator.cpp:143
QgsSimplifyMethod::MethodType
MethodType
Definition: qgssimplifymethod.h:31
QgsFeatureIterator::QgsFeatureIterator
QgsFeatureIterator()=default
Construct invalid iterator.
QgsFeature
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:409
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:346
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
QgsAbstractFeatureIterator::compileStatus
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
Definition: qgsfeatureiterator.h:69
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsAbstractFeatureIterator
Definition: qgsfeatureiterator.h:28
QgsAbstractFeatureIterator::close
virtual bool close()=0
end of iterating: free the resources / lock
QgsFeatureIterator::rewind
bool rewind()
Definition: qgsfeatureiterator.h:378