QGIS API Documentation  3.0.2-Girona (307d082)
qgsvectorlayercache.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayercache.h
3  Cache features of a vector layer
4  -------------------
5  begin : January 2013
6  copyright : (C) Matthias Kuhn
7  email : matthias at opengis dot ch
8 
9  ***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #ifndef QgsVectorLayerCache_H
20 #define QgsVectorLayerCache_H
21 
22 #include "qgis_core.h"
23 #include "qgis_sip.h"
24 #include "qgis.h"
25 #include <QCache>
26 
27 #include "qgsvectorlayer.h"
28 
31 
42 class CORE_EXPORT QgsVectorLayerCache : public QObject
43 {
44  Q_OBJECT
45 
46  private:
47 
53  class QgsCachedFeature
54  {
55  public:
56 
63  QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache )
64  : mCache( vlCache )
65  {
66  mFeature = new QgsFeature( feat );
67  }
68 
69  ~QgsCachedFeature()
70  {
71  // That's the reason we need this wrapper:
72  // Inform the cache that this feature has been removed
73  mCache->featureRemoved( mFeature->id() );
74  delete mFeature;
75  }
76 
77  inline const QgsFeature *feature() { return mFeature; }
78 
79  private:
80  QgsFeature *mFeature = nullptr;
81  QgsVectorLayerCache *mCache = nullptr;
82 
83  friend class QgsVectorLayerCache;
84  Q_DISABLE_COPY( QgsCachedFeature )
85  };
86 
87  public:
88  QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
89  ~QgsVectorLayerCache() override;
90 
97  void setCacheSize( int cacheSize );
98 
106  int cacheSize();
107 
114  void setCacheGeometry( bool cacheGeometry );
115 
121  bool cacheGeometry() const { return mCacheGeometry; }
122 
128  void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
129 
136  void setCacheAddedAttributes( bool cacheAddedAttributes );
137 
151  void setFullCache( bool fullCache );
152 
160  bool hasFullCache() const { return mFullCache; }
161 
171 
182 
186  inline QgsFeatureIterator getFeatures( const QString &expression )
187  {
188  return getFeatures( QgsFeatureRequest( expression ) );
189  }
190 
196  {
197  QgsFeature feature;
198  getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
199  return feature;
200  }
201 
206  {
207  return getFeatures( QgsFeatureRequest( fids ) );
208  }
209 
213  inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
214  {
215  return getFeatures( QgsFeatureRequest( rectangle ) );
216  }
217 
224  bool isFidCached( const QgsFeatureId fid ) const;
225 
231  QgsFeatureIds cachedFeatureIds() const { return mCache.keys().toSet(); }
232 
240  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
241 
247  bool removeCachedFeature( QgsFeatureId fid );
248 
253 
258 
262  QgsFields fields() const;
263 
267  QgsWkbTypes::Type wkbType() const;
268 
269 #ifdef SIP_RUN
270 
275  int __len__() const;
276  % MethodCode
277  sipRes = sipCpp->featureCount();
278  % End
279 #endif
280 
285  long featureCount() const;
286 
287  protected:
288 
297  void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
298 
306  void featureRemoved( QgsFeatureId fid );
307 
318  bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
319 
320 
321  signals:
322 
332  void progress( int i, bool &cancel ) SIP_SKIP;
333 
337  void finished();
338 
344  void cachedLayerDeleted();
345 
350  void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
351 
359  void featureAdded( QgsFeatureId fid );
360 
366  void invalidated();
367 
368  private slots:
369  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
370  void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
371  void featureDeleted( QgsFeatureId fid );
372  void onFeatureAdded( QgsFeatureId fid );
373  void attributeAdded( int field );
374  void attributeDeleted( int field );
375  void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
376  void layerDeleted();
377  void invalidate();
378 
379  private:
380 
381  void connectJoinedLayers() const;
382 
383  inline void cacheFeature( QgsFeature &feat )
384  {
385  QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this );
386  mCache.insert( feat.id(), cachedFeature );
387  }
388 
389  QgsVectorLayer *mLayer = nullptr;
390  QCache< QgsFeatureId, QgsCachedFeature > mCache;
391 
392  bool mCacheGeometry = true;
393  bool mFullCache = false;
394  QList<QgsAbstractCacheIndex *> mCacheIndices;
395 
396  QgsAttributeList mCachedAttributes;
397 
400  friend class QgsCachedFeature;
401 
410  bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
411 
412  friend class TestVectorLayerCache;
413 };
414 #endif // QgsVectorLayerCache_H
QgsFeatureIterator getFeatures(const QString &expression)
Query the layer for features matching a given expression.
QgsFeatureId id
Definition: qgsfeature.h:71
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QgsFeatureIterator getFeatures(const QgsFeatureIds &fids)
Query the layer for the features with the given ids.
void invalidated()
The cache has been invalidated and cleared.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
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 isFidCached(const QgsFeatureId fid) const
Check if a certain feature id is cached.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Is emitted when an attribute is changed.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
void featureAdded(QgsFeatureId fid)
Is emitted, when a new feature has been added to the layer and this cache.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
Uses another iterator as backend and writes features to the cache.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
#define SIP_SKIP
Definition: qgis_sip.h:119
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are cached.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFields fields() const
Returns the fields associated with features in the cache.
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
This class caches features of a given QgsVectorLayer.
long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown...
Abstract base class for cache indices.
Definition: qgscacheindex.h:31
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
void setFullCache(bool fullCache)
This enables or disables full caching.
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
This class represents a coordinate reference system (CRS).
QgsWkbTypes::Type wkbType() const
Returns the geometry type for features in the cache.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
QgsFeatureIterator getFeatures(const QgsRectangle &rectangle)
Query the layer for the features which intersect the specified rectangle.
Delivers features from the cache.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
int cacheSize()
Returns the maximum number of features this cache will hold.
QgsFeature getFeature(QgsFeatureId fid)
Query the layer for the feature with the given id.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
bool nextFeature(QgsFeature &f)
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
bool hasFullCache() const
Returns true if the cache is complete, ie it contains all features.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...