QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgsfield.h"
25 #include "qgsfeaturerequest.h"
26 #include "qgsfeatureiterator.h"
27 
28 #include <QCache>
29 
30 class QgsVectorLayer;
31 class QgsFeature;
34 
45 class CORE_EXPORT QgsVectorLayerCache : public QObject
46 {
47  Q_OBJECT
48 
49  private:
50 
56  class QgsCachedFeature
57  {
58  public:
59 
66  QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache )
67  : mCache( vlCache )
68  {
69  mFeature = new QgsFeature( feat );
70  }
71 
72  ~QgsCachedFeature()
73  {
74  // That's the reason we need this wrapper:
75  // Inform the cache that this feature has been removed
76  mCache->featureRemoved( mFeature->id() );
77  delete mFeature;
78  }
79 
80  inline const QgsFeature *feature() { return mFeature; }
81 
82  private:
83  QgsFeature *mFeature = nullptr;
84  QgsVectorLayerCache *mCache = nullptr;
85 
86  friend class QgsVectorLayerCache;
87  Q_DISABLE_COPY( QgsCachedFeature )
88  };
89 
90  public:
91  QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
92  ~QgsVectorLayerCache() override;
93 
100  void setCacheSize( int cacheSize );
101 
109  int cacheSize();
110 
117  void setCacheGeometry( bool cacheGeometry );
118 
124  bool cacheGeometry() const { return mCacheGeometry; }
125 
131  void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
132 
139  void setCacheAddedAttributes( bool cacheAddedAttributes );
140 
154  void setFullCache( bool fullCache );
155 
163  bool hasFullCache() const { return mFullCache; }
164 
174 
185 
189  inline QgsFeatureIterator getFeatures( const QString &expression )
190  {
191  return getFeatures( QgsFeatureRequest( expression ) );
192  }
193 
199  {
200  QgsFeature feature;
201  getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
202  return feature;
203  }
204 
209  {
210  return getFeatures( QgsFeatureRequest( fids ) );
211  }
212 
216  inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
217  {
218  return getFeatures( QgsFeatureRequest( rectangle ) );
219  }
220 
227  bool isFidCached( QgsFeatureId fid ) const;
228 
234  QgsFeatureIds cachedFeatureIds() const { return qgis::listToSet( mCache.keys() ); }
235 
243  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
244 
250  bool removeCachedFeature( QgsFeatureId fid );
251 
256 
261 
265  QgsFields fields() const;
266 
270  QgsWkbTypes::Type wkbType() const;
271 
272 #ifdef SIP_RUN
273 
278  int __len__() const;
279  % MethodCode
280  sipRes = sipCpp->featureCount();
281  % End
282 
284  int __bool__() const;
285  % MethodCode
286  sipRes = true;
287  % End
288 #endif
289 
294  long featureCount() const;
295 
296  protected:
297 
306  void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
307 
315  void featureRemoved( QgsFeatureId fid );
316 
327  bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
328 
329 
330  signals:
331 
341  void progress( int i, bool &cancel ) SIP_SKIP;
342 
346  void finished();
347 
353  void cachedLayerDeleted();
354 
359  void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
360 
368  void featureAdded( QgsFeatureId fid );
369 
375  void invalidated();
376 
377  private slots:
378  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
379  void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
380  void featureDeleted( QgsFeatureId fid );
381  void onFeatureAdded( QgsFeatureId fid );
382  void attributeAdded( int field );
383  void attributeDeleted( int field );
384  void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
385  void layerDeleted();
386  void invalidate();
387 
388  private:
389 
390  void connectJoinedLayers() const;
391 
392  inline void cacheFeature( QgsFeature &feat )
393  {
394  QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this );
395  mCache.insert( feat.id(), cachedFeature );
396  }
397 
398  QgsVectorLayer *mLayer = nullptr;
399  QCache< QgsFeatureId, QgsCachedFeature > mCache;
400 
401  bool mCacheGeometry = true;
402  bool mFullCache = false;
403  QList<QgsAbstractCacheIndex *> mCacheIndices;
404 
405  QgsAttributeList mCachedAttributes;
406 
409  friend class QgsCachedFeature;
410 
419  bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
420 
421  friend class TestVectorLayerCache;
422 };
423 #endif // QgsVectorLayerCache_H
QgsCachedFeatureWriterIterator
Uses another iterator as backend and writes features to the cache.
Definition: qgscachedfeatureiterator.h:92
QgsVectorLayerCache::setCacheSubsetOfAttributes
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
Definition: qgsvectorlayercache.cpp:82
qgsfeaturerequest.h
QgsVectorLayerCache
The cached features can be indexed by QgsAbstractCacheIndex.
Definition: qgsvectorlayercache.h:45
QgsVectorLayerCache::requestCompleted
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
Definition: qgsvectorlayercache.cpp:204
QgsVectorLayerCache::wkbType
QgsWkbTypes::Type wkbType() const
Returns the geometry type for features in the cache.
Definition: qgsvectorlayercache.cpp:189
QgsVectorLayerCache::setCacheAddedAttributes
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
Definition: qgsvectorlayercache.cpp:133
QgsVectorLayerCache::QgsVectorLayerCache
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
Definition: qgsvectorlayercache.cpp:25
QgsVectorLayerCache::~QgsVectorLayerCache
~QgsVectorLayerCache() override
Definition: qgsvectorlayercache.cpp:47
qgsfeatureiterator.h
QgsFields
Definition: qgsfields.h:44
QgsVectorLayerCache::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
Definition: qgsvectorlayercache.cpp:184
QgsVectorLayerCache::removeCachedFeature
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
Definition: qgsvectorlayercache.cpp:174
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
Definition: qgsvectorlayercache.cpp:378
QgsVectorLayerCache::invalidated
void invalidated()
The cache has been invalidated and cleared.
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsVectorLayerCache::checkInformationCovered
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
Definition: qgsvectorlayercache.cpp:429
QgsRectangle
Definition: qgsrectangle.h:41
QgsVectorLayerCache::getFeature
QgsFeature getFeature(QgsFeatureId fid)
Query the layer for the feature with the given id.
Definition: qgsvectorlayercache.h:198
QgsVectorLayerCache::attributeValueChanged
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Emitted when an attribute is changed.
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsVectorLayerCache::finished
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsVectorLayerCache::fields
QgsFields fields() const
Returns the fields associated with features in the cache.
Definition: qgsvectorlayercache.cpp:194
QgsVectorLayerCache::progress
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
QgsVectorLayerCache::addCacheIndex
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
Definition: qgsvectorlayercache.cpp:128
QgsVectorLayerCache::featureAdded
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer and this cache.
QgsVectorLayerCache::featureRemoved
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
Definition: qgsvectorlayercache.cpp:221
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsVectorLayerCache::layer
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
Definition: qgsvectorlayercache.cpp:179
QgsVectorLayerCache::featureAtId
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
Definition: qgsvectorlayercache.cpp:145
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsVectorLayerCache::cachedLayerDeleted
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
QgsAbstractCacheIndex
Abstract base class for cache indices.
Definition: qgscacheindex.h:31
QgsVectorLayerCache::isFidCached
bool isFidCached(QgsFeatureId fid) const
Check if a certain feature id is cached.
Definition: qgsvectorlayercache.cpp:424
QgsVectorLayerCache::setFullCache
void setFullCache(bool fullCache)
This enables or disables full caching.
Definition: qgsvectorlayercache.cpp:87
QgsVectorLayerCache::hasFullCache
bool hasFullCache() const
Returns true if the cache is complete, ie it contains all features.
Definition: qgsvectorlayercache.h:163
QgsVectorLayerCache::cacheSize
int cacheSize()
Returns the maximum number of features this cache will hold.
Definition: qgsvectorlayercache.cpp:58
QgsCachedFeatureIterator
Delivers features from the cache.
Definition: qgscachedfeatureiterator.h:31
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsVectorLayerCache::QgsCachedFeature
friend class QgsCachedFeature
Definition: qgsvectorlayercache.h:409
qgsfield.h
QgsVectorLayerCache::featureCount
long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown.
Definition: qgsvectorlayercache.cpp:199
QgsVectorLayerCache::setCacheGeometry
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
Definition: qgsvectorlayercache.cpp:63
QgsVectorLayerCache::setCacheSize
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
Definition: qgsvectorlayercache.cpp:53
QgsFeature
Definition: qgsfeature.h:55
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QString &expression)
Query the layer for features matching a given expression.
Definition: qgsvectorlayercache.h:189
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QgsRectangle &rectangle)
Query the layer for the features which intersect the specified rectangle.
Definition: qgsvectorlayercache.h:216
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureIds &fids)
Query the layer for the features with the given ids.
Definition: qgsvectorlayercache.h:208
QgsVectorLayerCache::cacheGeometry
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
Definition: qgsvectorlayercache.h:124
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
QgsVectorLayerCache::cachedFeatureIds
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are cached.
Definition: qgsvectorlayercache.h:234