QGIS API Documentation  2.99.0-Master (6a61179)
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 
20  : QgsAbstractFeatureIterator( featureRequest )
21  , mVectorLayerCache( vlCache )
22 {
23  switch ( featureRequest.filterType() )
24  {
26  mFeatureIds = featureRequest.filterFids();
27  break;
28 
30  mFeatureIds = QgsFeatureIds() << featureRequest.filterFid();
31  break;
32 
33  default:
34  mFeatureIds = mVectorLayerCache->mCache.keys().toSet();
35  break;
36  }
37 
38  mFeatureIdIterator = mFeatureIds.constBegin();
39 
40  if ( mFeatureIdIterator == mFeatureIds.constEnd() )
41  close();
42 }
43 
45 {
46  if ( mClosed )
47  return false;
48 
49  while ( mFeatureIdIterator != mFeatureIds.constEnd() )
50  {
51  f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
52  ++mFeatureIdIterator;
53  if ( mRequest.acceptFeature( f ) )
54  return true;
55  }
56  close();
57  return false;
58 }
59 
61 {
62  mFeatureIdIterator = mFeatureIds.constBegin();
63  return true;
64 }
65 
67 {
68  mClosed = true;
69  mFeatureIds.clear();
70  return true;
71 }
72 
74  : QgsAbstractFeatureIterator( featureRequest )
75  , mVectorLayerCache( vlCache )
76 {
77  mFeatIt = vlCache->layer()->getFeatures( featureRequest );
78 }
79 
81 {
82  if ( mFeatIt.nextFeature( f ) )
83  {
84  // As long as features can be fetched from the provider: Write them to cache
85  mVectorLayerCache->cacheFeature( f );
86  mFids.insert( f.id() );
87  return true;
88  }
89  else
90  {
91  // Once no more features can be fetched: Inform the cache, that
92  // the request has been completed
93  mVectorLayerCache->requestCompleted( mRequest, mFids );
94  return false;
95  }
96 }
97 
99 {
100  mFids.clear();
101  return mFeatIt.rewind();
102 }
103 
105 {
106  mClosed = true;
107  return mFeatIt.close();
108 }
QgsFeatureId id
Definition: qgsfeature.h:139
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
Filter using feature ID.
Filter using feature IDs.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
virtual bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
QgsFeatureId filterFid() const
Get the feature ID that should be fetched.
FilterType filterType() const
Return the filter type which is currently set on this request.
const QgsFeatureIds & filterFids() const
Get 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
Set 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:135
Internal feature iterator to be implemented within data providers.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const
Query the layer for features specified in request.
virtual bool close() override
Close this iterator.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
virtual bool close() override
Close this iterator.
virtual bool rewind() override
Rewind to the beginning of the iterator.
This class caches features of a given QgsVectorLayer.
QgsFeatureRequest mRequest
A copy of the feature request.
virtual bool rewind() override
Rewind to the beginning of the iterator.
QgsCachedFeatureIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest)
This constructor creates a feature iterator, that delivers all cached features.
bool nextFeature(QgsFeature &f)
virtual 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...