QGIS API Documentation 4.5.0-Master (c3bae02b63e)
Loading...
Searching...
No Matches
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:
38
41
43 virtual ~QgsAbstractFeatureIterator() = default;
44
48 virtual bool nextFeature( QgsFeature &f );
49
53 virtual bool rewind() = 0;
54
58 virtual bool close() = 0;
59
68 virtual void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
69
74
84 virtual bool isValid() const { return mValid; }
85
92 bool compileFailed() const;
93
99 enum class RequestToSourceCrsResult : int
100 {
101 Success,
102 DistanceWithinMustBeCheckedManually,
103 };
104
105 protected:
113 virtual bool fetchFeature( QgsFeature &f ) = 0;
114
125 virtual bool nextFeatureFilterExpression( QgsFeature &f );
126
138 virtual bool nextFeatureFilterFids( QgsFeature &f );
139
147 void geometryToDestinationCrs( QgsFeature &feature, const QgsCoordinateTransform &transform ) const;
148
149
159
173
176
178 bool mClosed = false;
179
187 bool mZombie = false;
188
189 // TODO QGIS 5: make this private
190
194 int refs = 0;
196 void ref();
198 void deref();
199 friend class QgsFeatureIterator;
200
202 long long mFetchedCount = 0;
203
206
207 bool mCompileFailed = false;
208
210 virtual bool prepareSimplification( const QgsSimplifyMethod &simplifyMethod );
211
220 bool mValid = true;
221
222 private:
223 bool mUseCachedFeatures = false;
224 QList<QgsIndexedFeature> mCachedFeatures;
225 QList<QgsIndexedFeature>::ConstIterator mFeatureIterator;
226
228 virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const;
229
237 virtual bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
238
244 void setupOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys );
245};
246
247
254{
255 public:
256 QgsAbstractFeatureIteratorFromSource( T *source, bool ownSource, const QgsFeatureRequest &request )
257 : QgsAbstractFeatureIterator( request )
258 , mSource( source )
259 , mOwnSource( ownSource )
260 {
261 mSource->iteratorOpened( this );
262 }
263
266
268 {
269 if ( mOwnSource )
270 delete mSource;
271 }
272
273 protected:
275 void iteratorClosed() { mSource->iteratorClosed( this ); }
276
277 T *mSource = nullptr;
279
280 private:
281#ifdef SIP_RUN
283#endif
284};
285
286
291class CORE_EXPORT QgsFeatureIterator
292{
293 public:
294#ifdef SIP_RUN
295 // clang-format off
296 QgsFeatureIterator *__iter__();
297 % MethodCode
298 sipRes = sipCpp;
299 % End
300
301 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
302 % MethodCode
303 auto f = std::make_unique< QgsFeature >();
304 bool result = false;
305 Py_BEGIN_ALLOW_THREADS
306 result = ( sipCpp->nextFeature( *f ) );
307 Py_END_ALLOW_THREADS
308 if ( result )
309 sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
310 else
311 {
312 PyErr_SetString( PyExc_StopIteration, "" );
313 }
314 % End
315// clang-format on
316#endif
317
320 = default;
327
329
333 bool nextFeature( QgsFeature &f );
334
338 bool rewind();
339
343 bool close();
344
353 bool isValid() const;
354
356 bool isClosed() const;
357
365 void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
366
371
378 bool compileFailed() const { return mIter->compileFailed(); }
379
380 friend bool operator==( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
381 friend bool operator!=( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
382
383 protected:
385};
386
387#ifndef SIP_RUN
388
390 : mIter( iter )
391{
392 if ( iter )
393 iter->ref();
394}
395
397 : mIter( fi.mIter )
398{
399 if ( mIter )
400 mIter->ref();
401}
402
404{
405 if ( mIter )
406 mIter->deref();
407}
408
410{
411 return mIter ? mIter->nextFeature( f ) : false;
412}
413
415{
416 if ( mIter )
417 mIter->mFetchedCount = 0;
418
419 return mIter ? mIter->rewind() : false;
420}
421
423{
424 if ( mIter )
425 mIter->mFetchedCount = 0;
426
427 return mIter ? mIter->close() : false;
428}
429
431{
432 return mIter ? mIter->mClosed && !mIter->mZombie : true;
433}
434
435inline bool operator==( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
436{
437 return fi1.mIter == fi2.mIter;
438}
439
440inline bool operator!=( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
441{
442 return !( fi1 == fi2 );
443}
444
446{
447 if ( mIter )
448 mIter->setInterruptionChecker( interruptionChecker );
449}
450
451#endif
452
453#endif // QGSFEATUREITERATOR_H
void iteratorClosed()
to be called by from subclass in close()
QgsAbstractFeatureIteratorFromSource(const QgsAbstractFeatureIteratorFromSource &)=delete
QgsAbstractFeatureIteratorFromSource & operator=(QgsAbstractFeatureIteratorFromSource &other)=delete
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 bool nextFeatureFilterFids(QgsFeature &f)
By default, the iterator will fetch all features and check if the id is in the request.
void geometryToDestinationCrs(QgsFeature &feature, const QgsCoordinateTransform &transform) const
Transforms feature's geometry according to the specified coordinate transform.
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const
Returns a rectangle representing the original request's QgsFeatureRequest::filterRect().
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.
virtual bool prepareSimplification(const QgsSimplifyMethod &simplifyMethod)
Setup the simplification of geometries to fetch using the specified simplify method.
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.
QgsAbstractFeatureIterator(const QgsFeatureRequest &request)
base class constructor - stores the iteration parameters
RequestToSourceCrsResult updateRequestToSourceCrs(QgsFeatureRequest &request, const QgsCoordinateTransform &transform) const
Update a QgsFeatureRequest so that spatial filters are transformed to the source's coordinate referen...
CompileStatus compileStatus() const
Returns the status of expression compilation for filter expression requests.
virtual bool isValid() const
Returns if this iterator is valid.
int refs
reference counting (to allow seamless copying of QgsFeatureIterator instances)
virtual bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool mValid
An invalid state of a feature iterator indicates that there was a problem with even getting it up and...
bool mClosed
Sets to true, as soon as the iterator is closed.
CompileStatus mCompileStatus
Status of compilation of filter expression.
virtual bool nextFeatureFilterExpression(QgsFeature &f)
By default, the iterator will fetch all features and check if the feature matches the expression.
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
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.
QgsFeatureIterator & operator=(const QgsFeatureIterator &other)
bool isValid() const
Will return if this iterator is valid.
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.
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:60
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.
Contains information about how to simplify geometries fetched from a QgsFeatureIterator.
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:242
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_THROW(name,...)
Definition qgis_sip.h:215
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)