QGIS API Documentation  2.13.0-Master
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 
27 class CORE_EXPORT QgsAbstractFeatureIterator
28 {
29  public:
32 
34  virtual ~QgsAbstractFeatureIterator();
35 
37  virtual bool nextFeature( QgsFeature& f );
38 
40  virtual bool rewind() = 0;
42  virtual bool close() = 0;
43 
44  protected:
52  virtual bool fetchFeature( QgsFeature& f ) = 0;
53 
64  virtual bool nextFeatureFilterExpression( QgsFeature &f );
65 
77  virtual bool nextFeatureFilterFids( QgsFeature & f );
78 
81 
83  bool mClosed;
84 
92  bool mZombie;
93 
96  int refs;
97  void ref();
98  void deref();
99  friend class QgsFeatureIterator;
100 
103 
105  virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );
106 
107  private:
109  QgsAbstractGeometrySimplifier* mGeometrySimplifier;
111  bool mLocalSimplification;
112 
113  bool mUseCachedFeatures;
114  QList<QgsIndexedFeature> mCachedFeatures;
116 
118  virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
119 
121  virtual bool simplify( QgsFeature& feature );
122 
131  virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
132 
139  void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause>& orderBys );
140 };
141 
142 
143 
147 template<typename T>
149 {
150  public:
151  QgsAbstractFeatureIteratorFromSource( T* source, bool ownSource, const QgsFeatureRequest& request )
152  : QgsAbstractFeatureIterator( request ), mSource( source ), mOwnSource( ownSource )
153  {
154  mSource->iteratorOpened( this );
155  }
156 
158  {
159  if ( mOwnSource )
160  delete mSource;
161  }
162 
163  protected:
165  void iteratorClosed() { mSource->iteratorClosed( this ); }
166 
169 };
170 
171 
172 
177 class CORE_EXPORT QgsFeatureIterator
178 {
179  public:
188 
189  QgsFeatureIterator& operator=( const QgsFeatureIterator& other );
190 
191  bool nextFeature( QgsFeature& f );
192  bool rewind();
193  bool close();
194 
196  bool isClosed() const;
197 
198  friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
199  friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
200 
201  protected:
203 };
204 
206 
208  : mIter( nullptr )
209 {
210 }
211 
213  : mIter( iter )
214 {
215  if ( iter )
216  iter->ref();
217 }
218 
220  : mIter( fi.mIter )
221 {
222  if ( mIter )
223  mIter->ref();
224 }
225 
227 {
228  if ( mIter )
229  mIter->deref();
230 }
231 
233 {
234  return mIter ? mIter->nextFeature( f ) : false;
235 }
236 
238 {
239  if ( mIter )
240  mIter->mFetchedCount = 0;
241 
242  return mIter ? mIter->rewind() : false;
243 }
244 
246 {
247  if ( mIter )
248  mIter->mFetchedCount = 0;
249 
250  return mIter ? mIter->close() : false;
251 }
252 
253 inline bool QgsFeatureIterator::isClosed() const
254 {
255  return mIter ? mIter->mClosed && !mIter->mZombie : true;
256 }
257 
258 inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
259 {
260  return fi1.mIter == fi2.mIter;
261 }
262 
263 inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
264 {
265  return !( fi1 == fi2 );
266 }
267 
268 #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
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.
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.
QgsAbstractFeatureIterator * mIter
~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)
Helper template that cares of two things: 1.