QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
47 virtual bool nextFeature( QgsFeature &f );
48
50 virtual bool rewind() = 0;
52 virtual bool close() = 0;
53
63 virtual void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
64
69 CompileStatus compileStatus() const { return mCompileStatus; }
70
81 virtual bool isValid() const
82 {
83 return mValid;
84 }
85
92 bool compileFailed() const;
93
99 enum class RequestToSourceCrsResult : int
100 {
101 Success,
102 DistanceWithinMustBeCheckedManually,
103 };
104
105 protected:
106
114 virtual bool fetchFeature( QgsFeature &f ) = 0;
115
126 virtual bool nextFeatureFilterExpression( QgsFeature &f );
127
139 virtual bool nextFeatureFilterFids( QgsFeature &f );
140
149 void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
150
151
161 QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
162
175 RequestToSourceCrsResult updateRequestToSourceCrs( QgsFeatureRequest &request, const QgsCoordinateTransform &transform ) const SIP_THROW( QgsCsException );
176
179
181 bool mClosed = false;
182
190 bool mZombie = false;
191
192 // TODO QGIS 4: make this private
193
197 int refs = 0;
199 void ref();
201 void deref();
202 friend class QgsFeatureIterator;
203
205 long long mFetchedCount = 0;
206
208 CompileStatus mCompileStatus = NoCompilation;
209
210 bool mCompileFailed = false;
211
213 virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
214
223 bool mValid = true;
224
225 private:
226 bool mUseCachedFeatures = false;
227 QList<QgsIndexedFeature> mCachedFeatures;
228 QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
229
231 virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
232
241 virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
242
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
326 bool nextFeature( QgsFeature &f );
327 bool rewind();
328 bool close();
329
339 bool isValid() const;
340
342 bool isClosed() const;
343
352 void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
353
358 QgsAbstractFeatureIterator::CompileStatus compileStatus() const { return mIter->compileStatus(); }
359
366 bool compileFailed() const { return mIter->compileFailed(); }
367
368 friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
369 friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
370
371 protected:
373
374
375};
376
377#ifndef SIP_RUN
378
380 : mIter( iter )
381{
382 if ( iter )
383 iter->ref();
384}
385
387 : mIter( fi.mIter )
388{
389 if ( mIter )
390 mIter->ref();
391}
392
394{
395 if ( mIter )
396 mIter->deref();
397}
398
400{
401 return mIter ? mIter->nextFeature( f ) : false;
402}
403
405{
406 if ( mIter )
407 mIter->mFetchedCount = 0;
408
409 return mIter ? mIter->rewind() : false;
410}
411
413{
414 if ( mIter )
415 mIter->mFetchedCount = 0;
416
417 return mIter ? mIter->close() : false;
418}
419
421{
422 return mIter ? mIter->mClosed && !mIter->mZombie : true;
423}
424
425inline bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
426{
427 return fi1.mIter == fi2.mIter;
428}
429
430inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
431{
432 return !( fi1 == fi2 );
433}
434
436{
437 if ( mIter )
438 mIter->setInterruptionChecker( interruptionChecker );
439}
440
441#endif
442
443#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
reset 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
end of iterating: free the resources / lock
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, return 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:66
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
~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.
QgsFeatureIterator()=default
Construct invalid iterator.
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:45
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:227
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_THROW(name,...)
Definition: qgis_sip.h:198
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)