QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
21  : QgsAbstractFeatureIterator( featureRequest )
22  , mVectorLayerCache( vlCache )
23 {
24  if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mVectorLayerCache->sourceCrs() )
25  {
26  mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs(), mRequest.transformContext() );
27  }
28  try
29  {
30  mFilterRect = filterRectToSourceCrs( mTransform );
31  }
32  catch ( QgsCsException & )
33  {
34  // can't reproject mFilterRect
35  close();
36  return;
37  }
38  if ( !mFilterRect.isNull() )
39  {
40  // update request to be the unprojected filter rect
41  mRequest.setFilterRect( mFilterRect );
42  }
43 
44  switch ( featureRequest.filterType() )
45  {
47  mFeatureIds = featureRequest.filterFids();
48  break;
49 
51  mFeatureIds = QgsFeatureIds() << featureRequest.filterFid();
52  break;
53 
54  default:
55  mFeatureIds = mVectorLayerCache->mCache.keys().toSet();
56  break;
57  }
58 
59  mFeatureIdIterator = mFeatureIds.constBegin();
60 
61  if ( mFeatureIdIterator == mFeatureIds.constEnd() )
62  close();
63 }
64 
66 {
67  f.setValid( false );
68 
69  if ( mClosed )
70  return false;
71 
72  while ( mFeatureIdIterator != mFeatureIds.constEnd() )
73  {
74  if ( !mVectorLayerCache->mCache.contains( *mFeatureIdIterator ) )
75  {
76  ++mFeatureIdIterator;
77  continue;
78  }
79 
80  f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
81  ++mFeatureIdIterator;
82  if ( mRequest.acceptFeature( f ) )
83  {
84  f.setValid( true );
85  geometryToDestinationCrs( f, mTransform );
86  return true;
87  }
88  }
89  close();
90  return false;
91 }
92 
94 {
95  mFeatureIdIterator = mFeatureIds.constBegin();
96  return true;
97 }
98 
100 {
101  mClosed = true;
102  mFeatureIds.clear();
103  return true;
104 }
105 
107  : QgsAbstractFeatureIterator( featureRequest )
108  , mVectorLayerCache( vlCache )
109 {
110  if ( mRequest.destinationCrs().isValid() && mRequest.destinationCrs() != mVectorLayerCache->sourceCrs() )
111  {
112  mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs(), mRequest.transformContext() );
113  }
114  try
115  {
116  mFilterRect = filterRectToSourceCrs( mTransform );
117  }
118  catch ( QgsCsException & )
119  {
120  // can't reproject mFilterRect
121  close();
122  return;
123  }
124  if ( !mFilterRect.isNull() )
125  {
126  // update request to be the unprojected filter rect
127  mRequest.setFilterRect( mFilterRect );
128  }
129 
130  mFeatIt = vlCache->layer()->getFeatures( mRequest );
131 }
132 
134 {
135  if ( mClosed )
136  {
137  f.setValid( false );
138  return false;
139  }
140  if ( mFeatIt.nextFeature( f ) )
141  {
142  // As long as features can be fetched from the provider: Write them to cache
143  mVectorLayerCache->cacheFeature( f );
144  mFids.insert( f.id() );
145  geometryToDestinationCrs( f, mTransform );
146  return true;
147  }
148  else
149  {
150  // Once no more features can be fetched: Inform the cache, that
151  // the request has been completed
152  mVectorLayerCache->requestCompleted( mRequest, mFids );
153  return false;
154  }
155 }
156 
158 {
159  mFids.clear();
160  return mFeatIt.rewind();
161 }
162 
164 {
165  mClosed = true;
166  return mFeatIt.close();
167 }
QgsFeatureId id
Definition: qgsfeature.h:71
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.
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const SIP_THROW(QgsCsException)
Returns a rectangle representing the original request&#39;s QgsFeatureRequest::filterRect().
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
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:62
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.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
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:181
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:429
Class for doing transforms between two map coordinate systems.
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.