QGIS API Documentation  3.2.0-Bonn (bc43194)
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__();
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  virtual 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
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:40
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:62
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
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 cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
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:119
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:177
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.