QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:993
QgsFeatureRequest::FilterFid
@ FilterFid
Filter using feature ID.
Definition: qgsfeaturerequest.h:93
QgsFeature::id
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
qgsvectorlayercache.h
QgsFeatureRequest::filterFids
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
Definition: qgsfeaturerequest.h:345
QgsVectorLayerCache
This class caches features of a given QgsVectorLayer.
Definition: qgsvectorlayercache.h:46
QgsFeatureRequest::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is requi...
Definition: qgsfeaturerequest.cpp:253
QgsFeatureRequest::acceptFeature
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
Definition: qgsfeaturerequest.cpp:271
QgsVectorLayerCache::requestCompleted
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
Definition: qgsvectorlayercache.cpp:204
QgsFeatureRequest::filterFid
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
Definition: qgsfeaturerequest.h:340
QgsFeatureRequest::filterType
FilterType filterType() const
Returns the filter type which is currently set on this request.
Definition: qgsfeaturerequest.h:312
QgsAbstractFeatureIterator::mClosed
bool mClosed
Sets to true, as soon as the iterator is closed.
Definition: qgsfeatureiterator.h:156
QgsFeatureRequest::FilterFids
@ FilterFids
Filter using feature IDs.
Definition: qgsfeaturerequest.h:95
QgsVectorLayerCache::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
Definition: qgsvectorlayercache.cpp:184
QgsFeature::setValid
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:190
QgsFeatureRequest::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordi...
Definition: qgsfeaturerequest.cpp:248
QgsAbstractFeatureIterator::mRequest
QgsFeatureRequest mRequest
A copy of the feature request.
Definition: qgsfeatureiterator.h:153
qgscachedfeatureiterator.h
QgsCachedFeatureIterator::close
bool close() override
Close this iterator.
Definition: qgscachedfeatureiterator.cpp:100
QgsCachedFeatureIterator::fetchFeature
bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
Definition: qgscachedfeatureiterator.cpp:66
QgsFeatureRequest::setFilterRect
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
Definition: qgsfeaturerequest.cpp:92
QgsAbstractFeatureIterator::filterRectToSourceCrs
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const SIP_THROW(QgsCsException)
Returns a rectangle representing the original request's QgsFeatureRequest::filterRect().
Definition: qgsfeatureiterator.cpp:118
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
QgsCachedFeatureWriterIterator::close
bool close() override
Close this iterator.
Definition: qgscachedfeatureiterator.cpp:164
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:387
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:924
QgsVectorLayerCache::layer
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
Definition: qgsvectorlayercache.cpp:179
QgsAbstractFeatureIterator::geometryToDestinationCrs
void geometryToDestinationCrs(QgsFeature &feature, const QgsCoordinateTransform &transform) const
Transforms feature's geometry according to the specified coordinate transform.
Definition: qgsfeatureiterator.cpp:95
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qgsvectorlayer.h
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:374
QgsCachedFeatureWriterIterator::rewind
bool rewind() override
Rewind to the beginning of the iterator.
Definition: qgscachedfeatureiterator.cpp:158
QgsCachedFeatureWriterIterator::fetchFeature
bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
Definition: qgscachedfeatureiterator.cpp:134
qgsexception.h
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsCachedFeatureIterator::rewind
bool rewind() override
Rewind to the beginning of the iterator.
Definition: qgscachedfeatureiterator.cpp:94
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
QgsAbstractFeatureIterator
Internal feature iterator to be implemented within data providers.
Definition: qgsfeatureiterator.h:29
QgsRectangle::isNull
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:447
QgsFeatureIterator::rewind
bool rewind()
Definition: qgsfeatureiterator.h:379
QgsCachedFeatureWriterIterator::QgsCachedFeatureWriterIterator
QgsCachedFeatureWriterIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, which queries the backend and caches retrieved features.
Definition: qgscachedfeatureiterator.cpp:107
QgsCachedFeatureIterator::QgsCachedFeatureIterator
QgsCachedFeatureIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, that delivers all cached features.
Definition: qgscachedfeatureiterator.cpp:21