QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
265 {
266 if ( mOwnSource )
267 delete mSource;
268 }
269
270 protected:
272 void iteratorClosed() { mSource->iteratorClosed( this ); }
273
274 T *mSource = nullptr;
276};
277
278
283class CORE_EXPORT QgsFeatureIterator
284{
285 public:
286#ifdef SIP_RUN
287 // clang-format off
288 QgsFeatureIterator *__iter__();
289 % MethodCode
290 sipRes = sipCpp;
291 % End
292
293 SIP_PYOBJECT __next__() SIP_TYPEHINT( QgsFeature );
294 % MethodCode
295 auto f = std::make_unique< QgsFeature >();
296 bool result = false;
297 Py_BEGIN_ALLOW_THREADS
298 result = ( sipCpp->nextFeature( *f ) );
299 Py_END_ALLOW_THREADS
300 if ( result )
301 sipRes = sipConvertFromType( f.release(), sipType_QgsFeature, Py_None );
302 else
303 {
304 PyErr_SetString( PyExc_StopIteration, "" );
305 }
306 % End
307// clang-format on
308#endif
309
312 = default;
319
321
325 bool nextFeature( QgsFeature &f );
326
330 bool rewind();
331
335 bool close();
336
345 bool isValid() const;
346
348 bool isClosed() const;
349
357 void setInterruptionChecker( QgsFeedback *interruptionChecker ) SIP_SKIP;
358
363
370 bool compileFailed() const { return mIter->compileFailed(); }
371
372 friend bool operator==( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
373 friend bool operator!=( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 ) SIP_SKIP;
374
375 protected:
377};
378
379#ifndef SIP_RUN
380
382 : mIter( iter )
383{
384 if ( iter )
385 iter->ref();
386}
387
389 : mIter( fi.mIter )
390{
391 if ( mIter )
392 mIter->ref();
393}
394
396{
397 if ( mIter )
398 mIter->deref();
399}
400
402{
403 return mIter ? mIter->nextFeature( f ) : false;
404}
405
407{
408 if ( mIter )
409 mIter->mFetchedCount = 0;
410
411 return mIter ? mIter->rewind() : false;
412}
413
415{
416 if ( mIter )
417 mIter->mFetchedCount = 0;
418
419 return mIter ? mIter->close() : false;
420}
421
423{
424 return mIter ? mIter->mClosed && !mIter->mZombie : true;
425}
426
427inline bool operator==( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
428{
429 return fi1.mIter == fi2.mIter;
430}
431
432inline bool operator!=( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 )
433{
434 return !( fi1 == fi2 );
435}
436
438{
439 if ( mIter )
440 mIter->setInterruptionChecker( interruptionChecker );
441}
442
443#endif
444
445#endif // QGSFEATUREITERATOR_H
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 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:239
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_THROW(name,...)
Definition qgis_sip.h:210
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)