QGIS API Documentation
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 "qgsfeaturerequest.h"
19 #include "qgslogger.h"
20 #include "qgsindexedfeature.h"
21 
23 
30 class CORE_EXPORT QgsInterruptionChecker
31 {
32  public:
34  virtual bool mustStop() const = 0;
35 };
36 
40 class CORE_EXPORT QgsAbstractFeatureIterator
41 {
42  public:
43 
46  {
50  };
51 
54 
56  virtual ~QgsAbstractFeatureIterator();
57 
59  virtual bool nextFeature( QgsFeature& f );
60 
62  virtual bool rewind() = 0;
64  virtual bool close() = 0;
65 
74  virtual void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );
75 
79  CompileStatus compileStatus() const { return mCompileStatus; }
80 
81  protected:
89  virtual bool fetchFeature( QgsFeature& f ) = 0;
90 
101  virtual bool nextFeatureFilterExpression( QgsFeature &f );
102 
114  virtual bool nextFeatureFilterFids( QgsFeature & f );
115 
118 
120  bool mClosed;
121 
129  bool mZombie;
130 
133  int refs;
134  void ref();
135  void deref();
136  friend class QgsFeatureIterator;
137 
140 
143 
145  virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );
146 
147  private:
149  QgsAbstractGeometrySimplifier* mGeometrySimplifier;
151  bool mLocalSimplification;
152 
153  bool mUseCachedFeatures;
154  QList<QgsIndexedFeature> mCachedFeatures;
156 
158  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
159 
161  virtual bool simplify( QgsFeature& feature );
162 
171  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
172 
179  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
180 };
181 
182 
183 
187 template<typename T>
189 {
190  public:
191  QgsAbstractFeatureIteratorFromSource( T* source, bool ownSource, const QgsFeatureRequest& request )
192  : QgsAbstractFeatureIterator( request ), mSource( source ), mOwnSource( ownSource )
193  {
194  mSource->iteratorOpened( this );
195  }
196 
198  {
199  if ( mOwnSource )
200  delete mSource;
201  }
202 
203  protected:
205  void iteratorClosed() { mSource->iteratorClosed( this ); }
206 
209 };
210 
215 class CORE_EXPORT QgsFeatureIterator
216 {
217  public:
226 
227  QgsFeatureIterator& operator=( const QgsFeatureIterator& other );
228 
229  bool nextFeature( QgsFeature& f );
230  bool rewind();
231  bool close();
232 
234  bool isClosed() const;
235 
243  void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );
244 
248  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
249 
250  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
251  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
252 
253  protected:
255 };
256 
258 
260  : mIter( nullptr )
261 {
262 }
263 
265  : mIter( iter )
266 {
267  if ( iter )
268  iter->ref();
269 }
270 
272  : mIter( fi.mIter )
273 {
274  if ( mIter )
275  mIter->ref();
276 }
277 
279 {
280  if ( mIter )
281  mIter->deref();
282 }
283 
285 {
286  return mIter ? mIter->nextFeature( f ) : false;
287 }
288 
290 {
291  if ( mIter )
292  mIter->mFetchedCount = 0;
293 
294  return mIter ? mIter->rewind() : false;
295 }
296 
298 {
299  if ( mIter )
300  mIter->mFetchedCount = 0;
301 
302  return mIter ? mIter->close() : false;
303 }
304 
305 inline bool QgsFeatureIterator::isClosed() const
306 {
307  return mIter ? mIter->mClosed && !mIter->mZombie : true;
308 }
309 
310 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
311 {
312  return fi1.mIter == fi2.mIter;
313 }
314 
315 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
316 {
317  return !( fi1 == fi2 );
318 }
319 
321 {
322  if ( mIter )
323  mIter->setInterruptionChecker( interruptionChecker );
324 }
325 
326 #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
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
bool mClosed
Set 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:187
friend bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool isClosed() const
find out whether the iterator is still valid or closed already
CompileStatus mCompileStatus
Status of compilation of filter expression.
Interface that can be optionaly attached to an iterator so its nextFeature() implementaton can check ...
void iteratorClosed()
to be called by from subclass in close()
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
Internal feature iterator to be implemented within data providers.
CompileStatus
Status of expression compilation for filter expression requests.
QgsFeatureIterator()
construct invalid iterator
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void deref()
remove reference, delete if refs == 0
friend bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsFeatureRequest mRequest
A copy of the feature request.
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
virtual void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
QgsAbstractFeatureIterator * mIter
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
~QgsFeatureIterator()
destructor deletes the iterator if it has no more references
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
int refs
reference counting (to allow seamless copying of QgsFeatureIterator instances) TODO QGIS3: make this ...
bool nextFeature(QgsFeature &f)
Abstract base class for simplify geometries using a specific algorithm.
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
Helper template that cares of two things: 1.