QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
22  : QgsAbstractFeatureIterator( featureRequest )
23  , mVectorLayerCache( vlCache )
24 {
25  if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mVectorLayerCache->sourceCrs() )
26  {
27  mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs(), mRequest.transformContext() );
28  }
29  try
30  {
31  mFilterRect = filterRectToSourceCrs( mTransform );
32  }
33  catch ( QgsCsException & )
34  {
35  // can't reproject mFilterRect
36  close();
37  return;
38  }
39  if ( !mFilterRect.isNull() )
40  {
41  // update request to be the unprojected filter rect
42  mRequest.setFilterRect( mFilterRect );
43  }
44 
45  switch ( featureRequest.filterType() )
46  {
48  mFeatureIds = featureRequest.filterFids();
49  break;
50 
52  mFeatureIds = QgsFeatureIds() << featureRequest.filterFid();
53  break;
54 
55  default:
56  mFeatureIds = qgis::listToSet( mVectorLayerCache->mCache.keys() );
57  break;
58  }
59 
60  mFeatureIdIterator = mFeatureIds.constBegin();
61 
62  if ( mFeatureIdIterator == mFeatureIds.constEnd() )
63  close();
64 }
65 
67 {
68  f.setValid( false );
69 
70  if ( mClosed )
71  return false;
72 
73  while ( mFeatureIdIterator != mFeatureIds.constEnd() )
74  {
75  if ( !mVectorLayerCache->mCache.contains( *mFeatureIdIterator ) )
76  {
77  ++mFeatureIdIterator;
78  continue;
79  }
80 
81  f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
82  ++mFeatureIdIterator;
83  if ( mRequest.acceptFeature( f ) )
84  {
85  f.setValid( true );
86  geometryToDestinationCrs( f, mTransform );
87  return true;
88  }
89  }
90  close();
91  return false;
92 }
93 
95 {
96  mFeatureIdIterator = mFeatureIds.constBegin();
97  return true;
98 }
99 
101 {
102  mClosed = true;
103  mFeatureIds.clear();
104  return true;
105 }
106 
108  : QgsAbstractFeatureIterator( featureRequest )
109  , mVectorLayerCache( vlCache )
110 {
111  if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mVectorLayerCache->sourceCrs() )
112  {
113  mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs(), mRequest.transformContext() );
114  }
115  try
116  {
117  mFilterRect = filterRectToSourceCrs( mTransform );
118  }
119  catch ( QgsCsException & )
120  {
121  // can't reproject mFilterRect
122  close();
123  return;
124  }
125  if ( !mFilterRect.isNull() )
126  {
127  // update request to be the unprojected filter rect
128  mRequest.setFilterRect( mFilterRect );
129  }
130 
131  mFeatIt = vlCache->layer()->getFeatures( mRequest );
132 }
133 
135 {
136  if ( mClosed )
137  {
138  f.setValid( false );
139  return false;
140  }
141  if ( mFeatIt.nextFeature( f ) )
142  {
143  // As long as features can be fetched from the provider: Write them to cache
144  mVectorLayerCache->cacheFeature( f );
145  mFids.insert( f.id() );
146  geometryToDestinationCrs( f, mTransform );
147  return true;
148  }
149  else
150  {
151  // Once no more features can be fetched: Inform the cache, that
152  // the request has been completed
153  mVectorLayerCache->requestCompleted( mRequest, mFids );
154  return false;
155  }
156 }
157 
159 {
160  mFids.clear();
161  return mFeatIt.rewind();
162 }
163 
165 {
166  mClosed = true;
167  return mFeatIt.close();
168 }
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 SIP_THROW(QgsCsException)
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.
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.
Definition: qgsexception.h:66
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
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...
FilterType filterType() const
Returns the filter type which is currently set on this request.
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
@ FilterFid
Filter using feature ID.
@ FilterFids
Filter using feature IDs.
QgsFeatureId filterFid() const
Gets 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 id, geometry and a list of field/values...
Definition: qgsfeature.h:56
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:195
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:447
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
Definition: qgsfeatureid.h:37