QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 = mVectorLayerCache->mCache.keys().toSet();
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 }
QgsFeatureId id
Definition: qgsfeature.h:64
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
void geometryToDestinationCrs(QgsFeature &feature, const QgsCoordinateTransform &transform) const
Transforms feature&#39;s geometry according to the specified coordinate transform.
Filter using feature ID.
Filter using feature IDs.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const SIP_THROW(QgsCsException)
Returns a rectangle representing the original request&#39;s QgsFeatureRequest::filterRect().
bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature&#39;s geometries, or an invalid QgsCoordi...
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.
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.
bool mClosed
Sets to true, as soon as the iterator is closed.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
Internal feature iterator to be implemented within data providers.
bool close() override
Close this iterator.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
bool close() override
Close this iterator.
bool rewind() override
Rewind to the beginning of the iterator.
This class caches features of a given QgsVectorLayer.
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:188
QgsFeatureRequest mRequest
A copy of the feature request.
bool rewind() override
Rewind to the beginning of the iterator.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:436
Class for doing transforms between two map coordinate systems.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsCachedFeatureIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, that delivers all cached features.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
bool nextFeature(QgsFeature &f)
QgsCoordinateTransformContext transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is requi...
bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
QgsCachedFeatureWriterIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, which queries the backend and caches retrieved features...
bool isValid() const
Returns whether this CRS is correctly initialized and usable.