QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 dot kuhn at gmx 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 }
QgsFeatureId id() const
Get the feature id for this feature.
Definition: qgsfeature.cpp:100
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:317
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
const QgsFeatureIds & filterFids() const
virtual bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.
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:113
QgsCachedFeatureIterator(QgsVectorLayerCache *vlCache, QgsFeatureRequest featureRequest, QgsFeatureIds featureIds)
This constructor creates a feature iterator, that delivers only cached information, based on the QgsFeatureIds.
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)...
FilterType filterType() const
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.
const QgsFeatureId & filterFid() const
QgsFeatureRequest mRequest
A copy of the feature request.
virtual bool rewind() override
Rewind to the beginning of the iterator.
void requestCompleted(QgsFeatureRequest featureRequest, QgsFeatureIds fids)
Gets called, whenever the full list of feature ids for a certain request is known.
bool nextFeature(QgsFeature &f)
QgsCachedFeatureWriterIterator(QgsVectorLayerCache *vlCache, QgsFeatureRequest featureRequest)
This constructor creates a feature iterator, which queries the backend and caches retrieved features...
virtual bool fetchFeature(QgsFeature &f) override
Implementation for fetching a feature.