QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  , mFeatureIds( featureIds )
22  , mVectorLayerCache( vlCache )
23 {
24  mFeatureIdIterator = featureIds.constBegin();
25 
26  if ( mFeatureIdIterator == featureIds.constEnd() )
27  close();
28 }
29 
31  : QgsAbstractFeatureIterator( featureRequest )
32  , mVectorLayerCache( vlCache )
33 {
34  switch ( featureRequest.filterType() )
35  {
37  mFeatureIds = featureRequest.filterFids();
38  break;
39 
41  mFeatureIds = QgsFeatureIds() << featureRequest.filterFid();
42  break;
43 
44  default:
45  mFeatureIds = mVectorLayerCache->mCache.keys().toSet();
46  break;
47  }
48 
49  mFeatureIdIterator = mFeatureIds.constBegin();
50 
51  if ( mFeatureIdIterator == mFeatureIds.constEnd() )
52  close();
53 }
54 
56 {
57  if ( mClosed )
58  return false;
59 
60  while ( mFeatureIdIterator != mFeatureIds.constEnd() )
61  {
62  f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
63  ++mFeatureIdIterator;
64  if ( mRequest.acceptFeature( f ) )
65  return true;
66  }
67  close();
68  return false;
69 }
70 
72 {
73  mFeatureIdIterator = mFeatureIds.constBegin();
74  return true;
75 }
76 
78 {
79  mClosed = true;
80  mFeatureIds.clear();
81  return true;
82 }
83 
85  : QgsAbstractFeatureIterator( featureRequest )
86  , mVectorLayerCache( vlCache )
87 {
88  mFeatIt = vlCache->layer()->getFeatures( featureRequest );
89 }
90 
92 {
93  if ( mFeatIt.nextFeature( f ) )
94  {
95  // As long as features can be fetched from the provider: Write them to cache
96  mVectorLayerCache->cacheFeature( f );
97  mFids.insert( f.id() );
98  return true;
99  }
100  else
101  {
102  // Once no more features can be fetched: Inform the cache, that
103  // the request has been completed
104  mVectorLayerCache->requestCompleted( mRequest, mFids );
105  return false;
106  }
107 }
108 
110 {
111  mFids.clear();
112  return mFeatIt.rewind();
113 }
114 
116 {
117  mClosed = true;
118  return mFeatIt.close();
119 }
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:439
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
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.
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
Internal feature iterator to be implemented within data providers.
virtual bool close() override
Close this iterator.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
const_iterator constEnd() const
virtual bool close() override
Close this iterator.
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
virtual bool rewind() override
Rewind to the beginning of the iterator.
This class caches features of a given QgsVectorLayer.
const_iterator constBegin() const
QgsFeatureRequest mRequest
A copy of the feature request.
QList< Key > keys() const
virtual bool rewind() override
Rewind to the beginning of the iterator.
Q_DECL_DEPRECATED QgsCachedFeatureIterator(QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest, const QgsFeatureIds &featureIds)
This constructor creates a feature iterator, that delivers only cached information, based on the QgsFeatureIds.
bool nextFeature(QgsFeature &f)
void clear()
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...