QGIS API Documentation  2.15.0-Master (972fc9f)
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 
22 
29 class CORE_EXPORT QgsInterruptionChecker
30 {
31  public:
33  virtual bool mustStop() const = 0;
34 };
35 
39 class CORE_EXPORT QgsAbstractFeatureIterator
40 {
41  public:
42 
45  {
49  };
50 
53 
55  virtual ~QgsAbstractFeatureIterator();
56 
58  virtual bool nextFeature( QgsFeature& f );
59 
61  virtual bool rewind() = 0;
63  virtual bool close() = 0;
64 
73  virtual void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );
74 
78  CompileStatus compileStatus() const { return mCompileStatus; }
79 
80  protected:
88  virtual bool fetchFeature( QgsFeature& f ) = 0;
89 
100  virtual bool nextFeatureFilterExpression( QgsFeature &f );
101 
113  virtual bool nextFeatureFilterFids( QgsFeature & f );
114 
117 
119  bool mClosed;
120 
128  bool mZombie;
129 
132  int refs;
133  void ref();
134  void deref();
135  friend class QgsFeatureIterator;
136 
139 
142 
144  virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );
145 
146  private:
147  bool mUseCachedFeatures;
148  QList<QgsIndexedFeature> mCachedFeatures;
150 
152  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
153 
162  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
163 
170  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
171 };
172 
173 
174 
178 template<typename T>
180 {
181  public:
182  QgsAbstractFeatureIteratorFromSource( T* source, bool ownSource, const QgsFeatureRequest& request )
183  : QgsAbstractFeatureIterator( request )
184  , mSource( source )
185  , mOwnSource( ownSource )
186  {
187  mSource->iteratorOpened( this );
188  }
189 
191  {
192  if ( mOwnSource )
193  delete mSource;
194  }
195 
196  protected:
198  void iteratorClosed() { mSource->iteratorClosed( this ); }
199 
202 };
203 
208 class CORE_EXPORT QgsFeatureIterator
209 {
210  public:
219 
220  QgsFeatureIterator& operator=( const QgsFeatureIterator& other );
221 
222  bool nextFeature( QgsFeature& f );
223  bool rewind();
224  bool close();
225 
227  bool isClosed() const;
228 
236  void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );
237 
241  QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
242 
243  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
244  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
245 
246  protected:
248 };
249 
251 
253  : mIter( nullptr )
254 {
255 }
256 
258  : mIter( iter )
259 {
260  if ( iter )
261  iter->ref();
262 }
263 
265  : mIter( fi.mIter )
266 {
267  if ( mIter )
268  mIter->ref();
269 }
270 
272 {
273  if ( mIter )
274  mIter->deref();
275 }
276 
278 {
279  return mIter ? mIter->nextFeature( f ) : false;
280 }
281 
283 {
284  if ( mIter )
285  mIter->mFetchedCount = 0;
286 
287  return mIter ? mIter->rewind() : false;
288 }
289 
291 {
292  if ( mIter )
293  mIter->mFetchedCount = 0;
294 
295  return mIter ? mIter->close() : false;
296 }
297 
298 inline bool QgsFeatureIterator::isClosed() const
299 {
300  return mIter ? mIter->mClosed && !mIter->mZombie : true;
301 }
302 
303 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
304 {
305  return fi1.mIter == fi2.mIter;
306 }
307 
308 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
309 {
310  return !( fi1 == fi2 );
311 }
312 
314 {
315  if ( mIter )
316  mIter->setInterruptionChecker( interruptionChecker );
317 }
318 
319 #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)
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.