QGIS API Documentation  2.15.0-Master (af20121)
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 )
193  , mSource( source )
194  , mOwnSource( ownSource )
195  {
196  mSource->iteratorOpened( this );
197  }
198 
200  {
201  if ( mOwnSource )
202  delete mSource;
203  }
204 
205  protected:
207  void iteratorClosed() { mSource->iteratorClosed( this ); }
208 
211 };
212 
217 class CORE_EXPORT QgsFeatureIterator
218 {
219  public:
228 
229  QgsFeatureIterator& operator=( const QgsFeatureIterator& other );
230 
231  bool nextFeature( QgsFeature& f );
232  bool rewind();
233  bool close();
234 
236  bool isClosed() const;
237 
245  void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );
246 
250  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
251 
252  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
253  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
254 
255  protected:
257 };
258 
260 
262  : mIter( nullptr )
263 {
264 }
265 
267  : mIter( iter )
268 {
269  if ( iter )
270  iter->ref();
271 }
272 
274  : mIter( fi.mIter )
275 {
276  if ( mIter )
277  mIter->ref();
278 }
279 
281 {
282  if ( mIter )
283  mIter->deref();
284 }
285 
287 {
288  return mIter ? mIter->nextFeature( f ) : false;
289 }
290 
292 {
293  if ( mIter )
294  mIter->mFetchedCount = 0;
295 
296  return mIter ? mIter->rewind() : false;
297 }
298 
300 {
301  if ( mIter )
302  mIter->mFetchedCount = 0;
303 
304  return mIter ? mIter->close() : false;
305 }
306 
307 inline bool QgsFeatureIterator::isClosed() const
308 {
309  return mIter ? mIter->mClosed && !mIter->mZombie : true;
310 }
311 
312 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
313 {
314  return fi1.mIter == fi2.mIter;
315 }
316 
317 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
318 {
319  return !( fi1 == fi2 );
320 }
321 
323 {
324  if ( mIter )
325  mIter->setInterruptionChecker( interruptionChecker );
326 }
327 
328 #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.