QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
22class QgsFeedback;
23
29{
30 public:
31
34 {
38 };
39
42
44 virtual ~QgsAbstractFeatureIterator() = default;
45
49 virtual bool nextFeature( QgsFeature &f );
50
54 virtual bool rewind() = 0;
55
59 virtual bool close() = 0;
60
69 virtual void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
70
74 CompileStatus compileStatus() const { return mCompileStatus; }
75
85 virtual bool isValid() const
86 {
87 return mValid;
88 }
89
96 bool compileFailed() const;
97
104 {
105 Success,
106 DistanceWithinMustBeCheckedManually,
107 };
108
109 protected:
110
118 virtual bool fetchFeature( QgsFeature &f ) = 0;
119
130 virtual bool nextFeatureFilterExpression( QgsFeature &f );
131
143 virtual bool nextFeatureFilterFids( QgsFeature &f );
144
152 void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
153
154
163 QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
164
177 RequestToSourceCrsResult updateRequestToSourceCrs( QgsFeatureRequest &request, const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
178
181
183 bool mClosed = false;
184
192 bool mZombie = false;
193
194 // TODO QGIS 4: make this private
195
199 int refs = 0;
201 void ref();
203 void deref();
204 friend class QgsFeatureIterator;
205
207 long long mFetchedCount = 0;
208
210 CompileStatus mCompileStatus = NoCompilation;
211
212 bool mCompileFailed = false;
213
215 virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
216
225 bool mValid = true;
226
227 private:
228 bool mUseCachedFeatures = false;
229 QList<QgsIndexedFeature> mCachedFeatures;
230 QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
231
233 virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
234
242 virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
243
249 void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
250};
251
252
258template<typename T>
260{
261 public:
262 QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
263 : QgsAbstractFeatureIterator( request )
264 , mSource( source )
265 , mOwnSource( ownSource )
266 {
267 mSource->iteratorOpened( this );
268 }
269
271 {
272 if ( mOwnSource )
273 delete mSource;
274 }
275
276 protected:
278 void iteratorClosed() { mSource->iteratorClosed( this ); }
279
280 T *mSource = nullptr;
282};
283
284
289class CORE_EXPORT QgsFeatureIterator
290{
291 public:
292
293#ifdef SIP_RUN
294 QgsFeatureIterator *__iter__();
295 % MethodCode
296 sipRes = sipCpp;
297 % End
298
299 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
300 % MethodCode
301 std::unique_ptr< QgsFeature > f = std::make_unique< QgsFeature >();
302 bool result = false;
303 Py_BEGIN_ALLOW_THREADS
304 result = ( sipCpp->nextFeature( *f ) );
305 Py_END_ALLOW_THREADS
306 if ( result )
307 sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
308 else
309 {
310 PyErr_SetString( PyExc_StopIteration, "" );
311 }
312 % End
313#endif
314
323
324 QgsFeatureIterator &operator=( const QgsFeatureIterator &other );
325
329 bool nextFeature( QgsFeature &f );
330
334 bool rewind();
335
339 bool close();
340
349 bool isValid() const;
350
352 bool isClosed() const;
353
361 void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
362
366 QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
367
374 bool compileFailed() const { return mIter->compileFailed(); }
375
376 friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
377 friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
378
379 protected:
381
382
383};
384
385#ifndef SIP_RUN
386
388 : mIter( iter )
389{
390 if ( iter )
391 iter->ref();
392}
393
395 : mIter( fi.mIter )
396{
397 if ( mIter )
398 mIter->ref();
399}
400
402{
403 if ( mIter )
404 mIter->deref();
405}
406
408{
409 return mIter ? mIter->nextFeature( f ) : false;
410}
411
413{
414 if ( mIter )
415 mIter->mFetchedCount = 0;
416
417 return mIter ? mIter->rewind() : false;
418}
419
421{
422 if ( mIter )
423 mIter->mFetchedCount = 0;
424
425 return mIter ? mIter->close() : false;
426}
427
429{
430 return mIter ? mIter->mClosed && !mIter->mZombie : true;
431}
432
433inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
434{
435 return fi1.mIter == fi2.mIter;
436}
437
438inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
439{
440 return !( fi1 == fi2 );
441}
442
444{
445 if ( mIter )
446 mIter->setInterruptionChecker( interruptionChecker );
447}
448
449#endif
450
451#endif // QGSFEATUREITERATOR_H
Helper template that cares of two things: 1.
void iteratorClosed()
to be called by from subclass in close()
QgsAbstractFeatureIteratorFromSource(T *source, bool ownSource, const QgsFeatureRequest &request)
Internal feature iterator to be implemented within data providers.
virtual bool rewind()=0
Resets the iterator to the starting position.
RequestToSourceCrsResult
Possible results from the updateRequestToSourceCrs() method.
bool mZombie
A feature iterator may be closed already but still be serving features from the cache.
virtual void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!...
virtual bool close()=0
Call to end the iteration.
long long mFetchedCount
Number of features already fetched by iterator.
CompileStatus
Status of expression compilation for filter expression requests.
@ PartiallyCompiled
Expression was partially compiled, but extra checks need to be applied to features.
@ Compiled
Expression was fully compiled and delegated to data provider source.
@ NoCompilation
Expression could not be compiled or not attempt was made to compile expression.
void deref()
Remove reference, delete if refs == 0.
virtual ~QgsAbstractFeatureIterator()=default
destructor makes sure that the iterator is closed properly
QgsFeatureRequest mRequest
A copy of the feature request.
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
virtual bool isValid() const
Returns if this iterator is valid.
virtual bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool mClosed
Sets to true, as soon as the iterator is closed.
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:67
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
~QgsFeatureIterator()
Destructor deletes the iterator if it has no more references.
QgsAbstractFeatureIterator * mIter
bool isClosed() const
find out whether the iterator is still valid or closed already
void setInterruptionChecker(QgsFeedback *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped.
bool compileFailed() const
Indicator if there was an error when sending the compiled query to the server.
bool close()
Call to end the iteration.
QgsFeatureIterator()=default
Construct invalid iterator.
bool rewind()
Resets the iterator to the starting position.
QgsAbstractFeatureIterator::CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:232
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_THROW(name,...)
Definition: qgis_sip.h:203
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)