QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mVectorLayerCache->sourceCrs() )
27 {
28 mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs(), mRequest.transformContext() );
29 }
30 try
31 {
32 mFilterRect = filterRectToSourceCrs( mTransform );
33 }
34 catch ( QgsCsException & )
35 {
36 // can't reproject mFilterRect
37 close();
38 return;
39 }
40
41 // prepare spatial filter geometries for optimal speed
42 switch ( mRequest.spatialFilterType() )
43 {
46 break;
47
50 {
51 mDistanceWithinGeom = mRequest.referenceGeometry();
52 mDistanceWithinEngine.reset( QgsGeometry::createGeometryEngine( mDistanceWithinGeom.constGet() ) );
53 mDistanceWithinEngine->prepareGeometry();
54 mDistanceWithin = mRequest.distanceWithin();
55 }
56 break;
57 }
58
59 if ( !mFilterRect.isNull() )
60 {
61 // update request to be the unprojected filter rect
62 mRequest.setFilterRect( mFilterRect );
63 }
64
65 switch ( featureRequest.filterType() )
66 {
68 {
69 const QgsFeatureIds filterFids = featureRequest.filterFids();
70 mFeatureIds = QList< QgsFeatureId >( filterFids.begin(), filterFids.end() );
71 break;
72 }
73
75 mFeatureIds = QList< QgsFeatureId >() << featureRequest.filterFid();
76 break;
77
80 mFeatureIds = QList( mVectorLayerCache->mCacheOrderedKeys.begin(), mVectorLayerCache->mCacheOrderedKeys.end() );
81 break;
82 }
83
84 mFeatureIdIterator = mFeatureIds.constBegin();
85
86 if ( mFeatureIdIterator == mFeatureIds.constEnd() )
87 close();
88}
89
91
93{
94 f.setValid( false );
95
96 if ( mClosed )
97 return false;
98
99 while ( mFeatureIdIterator != mFeatureIds.constEnd() )
100 {
101 if ( !mVectorLayerCache->mCache.contains( *mFeatureIdIterator ) )
102 {
103 ++mFeatureIdIterator;
104 continue;
105 }
106
107 f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
108 ++mFeatureIdIterator;
109 if ( mRequest.acceptFeature( f ) )
110 {
111 f.setValid( true );
112 geometryToDestinationCrs( f, mTransform );
113
114 bool result = true;
115 if ( mDistanceWithinEngine && mDistanceWithinEngine->distance( f.geometry().constGet() ) > mDistanceWithin )
116 {
117 f.setValid( false );
118 result = false;
119 }
120
121 if ( result )
122 return true;
123 }
124 }
125 close();
126 return false;
127}
128
130{
131 mFeatureIdIterator = mFeatureIds.constBegin();
132 return true;
133}
134
136{
137 mClosed = true;
138 mFeatureIds.clear();
139 return true;
140}
141
143 : QgsAbstractFeatureIterator( featureRequest )
144 , mVectorLayerCache( vlCache )
145{
146 if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mVectorLayerCache->sourceCrs() )
147 {
148 mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs(), mRequest.transformContext() );
149 }
150 try
151 {
152 mFilterRect = filterRectToSourceCrs( mTransform );
153 }
154 catch ( QgsCsException & )
155 {
156 // can't reproject mFilterRect
157 close();
158 return;
159 }
160 if ( !mFilterRect.isNull() )
161 {
162 // update request to be the unprojected filter rect
163 mRequest.setFilterRect( mFilterRect );
164 }
165
166 mFeatIt = vlCache->layer()->getFeatures( mRequest );
167}
168
170{
171 if ( mClosed )
172 {
173 f.setValid( false );
174 return false;
175 }
176 if ( mFeatIt.nextFeature( f ) )
177 {
178 // As long as features can be fetched from the provider: Write them to cache
179 mVectorLayerCache->cacheFeature( f, ! mRequest.flags().testFlag( Qgis::FeatureRequestFlag::SubsetOfAttributes ) );
180 mFids.insert( f.id() );
181 geometryToDestinationCrs( f, mTransform );
182 return true;
183 }
184 else
185 {
186 // Once no more features can be fetched: Inform the cache, that
187 // the request has been completed
188 mVectorLayerCache->requestCompleted( mRequest, mFids );
189 return false;
190 }
191}
192
194{
195 mFids.clear();
196 return mFeatIt.rewind();
197}
198
200{
201 mClosed = true;
202 return mFeatIt.close();
203}
@ 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.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Class for doing transforms between two map coordinate systems.
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.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordi...
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
QgsCoordinateTransformContext transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is requi...
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:56
QgsFeatureId id
Definition qgsfeature.h:64
QgsGeometry geometry
Definition qgsfeature.h:67
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)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
bool isNull() const
Test if the rectangle is null (holding no spatial information).
This class caches features of a given QgsVectorLayer.
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QSet< QgsFeatureId > QgsFeatureIds