QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgscachedfeatureiterator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscachedfeatureiterator.cpp
3 --------------------------------------
4 Date : 12.2.2013
5 Copyright : (C) 2013 Matthias Kuhn
6 Email : matthias at opengis dot ch
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
17#include "qgsvectorlayercache.h"
18#include "qgsexception.h"
19#include "qgsvectorlayer.h"
20#include "qgsgeometryengine.h"
21
23 : QgsAbstractFeatureIterator( featureRequest )
24 , mVectorLayerCache( vlCache )
25{
26 mTransform = mRequest.calculateTransform( mVectorLayerCache->sourceCrs() );
27 try
28 {
29 mFilterRect = filterRectToSourceCrs( mTransform );
30 }
31 catch ( QgsCsException & )
32 {
33 // can't reproject mFilterRect
34 close();
35 return;
36 }
37
38 // prepare spatial filter geometries for optimal speed
39 switch ( mRequest.spatialFilterType() )
40 {
43 break;
44
47 {
48 mDistanceWithinGeom = mRequest.referenceGeometry();
49 mDistanceWithinEngine.reset( QgsGeometry::createGeometryEngine( mDistanceWithinGeom.constGet() ) );
50 mDistanceWithinEngine->prepareGeometry();
51 mDistanceWithin = mRequest.distanceWithin();
52 }
53 break;
54 }
55
56 if ( !mFilterRect.isNull() )
57 {
58 // update request to be the unprojected filter rect
59 mRequest.setFilterRect( mFilterRect );
60 }
61
62 switch ( featureRequest.filterType() )
63 {
65 {
66 const QgsFeatureIds filterFids = featureRequest.filterFids();
67 mFeatureIds = QList< QgsFeatureId >( filterFids.begin(), filterFids.end() );
68 break;
69 }
70
72 mFeatureIds = QList< QgsFeatureId >() << featureRequest.filterFid();
73 break;
74
77 mFeatureIds = QList( mVectorLayerCache->mCacheOrderedKeys.begin(), mVectorLayerCache->mCacheOrderedKeys.end() );
78 break;
79 }
80
81 mFeatureIdIterator = mFeatureIds.constBegin();
82
83 if ( mFeatureIdIterator == mFeatureIds.constEnd() )
84 close();
85}
86
88
90{
91 f.setValid( false );
92
93 if ( mClosed || !mVectorLayerCache )
94 return false;
95
96 while ( mFeatureIdIterator != mFeatureIds.constEnd() )
97 {
98 if ( !mVectorLayerCache->mCache.contains( *mFeatureIdIterator ) )
99 {
100 ++mFeatureIdIterator;
101 continue;
102 }
103
104 f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
105 ++mFeatureIdIterator;
106 if ( mRequest.acceptFeature( f ) )
107 {
108 f.setValid( true );
109 geometryToDestinationCrs( f, mTransform );
110
111 bool result = true;
112 if ( mDistanceWithinEngine && mDistanceWithinEngine->distance( f.geometry().constGet() ) > mDistanceWithin )
113 {
114 f.setValid( false );
115 result = false;
116 }
117
118 if ( result )
119 return true;
120 }
121 }
122 close();
123 return false;
124}
125
127{
128 mFeatureIdIterator = mFeatureIds.constBegin();
129 return true;
130}
131
133{
134 mClosed = true;
135 mFeatureIds.clear();
136 return true;
137}
138
140 : QgsAbstractFeatureIterator( featureRequest )
141 , mVectorLayerCache( vlCache )
142{
143 mTransform = mRequest.calculateTransform( mVectorLayerCache->sourceCrs() );
144 try
145 {
146 mFilterRect = filterRectToSourceCrs( mTransform );
147 }
148 catch ( QgsCsException & )
149 {
150 // can't reproject mFilterRect
151 close();
152 return;
153 }
154 if ( !mFilterRect.isNull() )
155 {
156 // update request to be the unprojected filter rect
157 mRequest.setFilterRect( mFilterRect );
158 }
159
160 mFeatIt = vlCache->layer()->getFeatures( mRequest );
161}
162
164{
165 if ( mClosed || !mVectorLayerCache )
166 {
167 f.setValid( false );
168 return false;
169 }
170 if ( mFeatIt.nextFeature( f ) )
171 {
172 // As long as features can be fetched from the provider: Write them to cache
173 mVectorLayerCache->cacheFeature( f, ! mRequest.flags().testFlag( Qgis::FeatureRequestFlag::SubsetOfAttributes ) );
174 mFids.insert( f.id() );
175 geometryToDestinationCrs( f, mTransform );
176 return true;
177 }
178 else
179 {
180 // Once no more features can be fetched: Inform the cache, that
181 // the request has been completed
182 mVectorLayerCache->requestCompleted( mRequest, mFids );
183 return false;
184 }
185}
186
188{
189 mFids.clear();
190 return mFeatIt.rewind();
191}
192
194{
195 mClosed = true;
196 return mFeatIt.close();
197}
@ Fid
Filter using feature ID.
@ Fids
Filter using feature IDs.
@ Expression
Filter using expression.
@ NoFilter
No filter is applied.
@ SubsetOfAttributes
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
@ DistanceWithin
Filter by distance to reference geometry.
@ BoundingBox
Filter using a bounding box.
@ NoFilter
No spatial filtering of features.
Internal feature iterator to be implemented within data providers.
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().
QgsFeatureRequest mRequest
A copy of the feature request.
bool mClosed
Sets to true, as soon as the iterator is closed.
bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
QgsCachedFeatureIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, that delivers all cached features.
bool close() override
Close this iterator.
bool rewind() override
Rewind to the beginning of the iterator.
~QgsCachedFeatureIterator() override
bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
bool close() override
Close this iterator.
QgsCachedFeatureWriterIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, which queries the backend and caches retrieved features.
bool rewind() override
Rewind to the beginning of the iterator.
Custom exception class for Coordinate Reference System related exceptions.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool close()
Call to end the iteration.
bool rewind()
Resets the iterator to the starting position.
This class wraps a request for features to a vector layer (or directly its vector data provider).
Qgis::FeatureRequestFilterType filterType() const
Returns the attribute/ID filter type which is currently set on this request.
QgsGeometry referenceGeometry() const
Returns the reference geometry used for spatial filtering of features.
Qgis::FeatureRequestFlags flags() const
Returns the flags which affect how features are fetched.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
QgsCoordinateTransform calculateTransform(const QgsCoordinateReferenceSystem &sourceCrs) const
Calculates the coordinate transform to use to transform geometries when they are originally in source...
Qgis::SpatialFilterType spatialFilterType() const
Returns the spatial filter type which is currently set on this request.
double distanceWithin() const
Returns the maximum distance from the referenceGeometry() of fetched features, if spatialFilterType()...
const QgsFeatureIds & filterFids() const
Returns the feature IDs that should be fetched.
QgsFeatureId filterFid() const
Returns the feature ID that should be fetched.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
void setValid(bool validity)
Sets the validity of the feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
This class caches features of a given QgsVectorLayer.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QSet< QgsFeatureId > QgsFeatureIds