QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgsfield.h"
26 #include "qgsfeaturerequest.h"
27 #include "qgsfeatureiterator.h"
28 
29 #include <QCache>
30 
31 class QgsVectorLayer;
32 class QgsFeature;
35 
46 class CORE_EXPORT QgsVectorLayerCache : public QObject
47 {
48  Q_OBJECT
49 
50  private:
51 
57  class QgsCachedFeature
58  {
59  public:
60 
67  QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache )
68  : mCache( vlCache )
69  {
70  mFeature = new QgsFeature( feat );
71  }
72 
73  ~QgsCachedFeature()
74  {
75  // That's the reason we need this wrapper:
76  // Inform the cache that this feature has been removed
77  mCache->featureRemoved( mFeature->id() );
78  delete mFeature;
79  }
80 
81  inline const QgsFeature *feature() { return mFeature; }
82 
83  private:
84  QgsFeature *mFeature = nullptr;
85  QgsVectorLayerCache *mCache = nullptr;
86 
87  friend class QgsVectorLayerCache;
88  Q_DISABLE_COPY( QgsCachedFeature )
89  };
90 
91  public:
92  QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
93  ~QgsVectorLayerCache() override;
94 
101  void setCacheSize( int cacheSize );
102 
110  int cacheSize();
111 
118  void setCacheGeometry( bool cacheGeometry );
119 
125  bool cacheGeometry() const { return mCacheGeometry; }
126 
132  void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
133 
140  void setCacheAddedAttributes( bool cacheAddedAttributes );
141 
155  void setFullCache( bool fullCache );
156 
164  bool hasFullCache() const { return mFullCache; }
165 
175 
186 
190  inline QgsFeatureIterator getFeatures( const QString &expression )
191  {
192  return getFeatures( QgsFeatureRequest( expression ) );
193  }
194 
200  {
201  QgsFeature feature;
202  getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
203  return feature;
204  }
205 
210  {
211  return getFeatures( QgsFeatureRequest( fids ) );
212  }
213 
217  inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
218  {
219  return getFeatures( QgsFeatureRequest( rectangle ) );
220  }
221 
228  bool isFidCached( QgsFeatureId fid ) const;
229 
235  QgsFeatureIds cachedFeatureIds() const { return mCache.keys().toSet(); }
236 
244  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
245 
251  bool removeCachedFeature( QgsFeatureId fid );
252 
257 
262 
266  QgsFields fields() const;
267 
271  QgsWkbTypes::Type wkbType() const;
272 
273 #ifdef SIP_RUN
274 
279  int __len__() const;
280  % MethodCode
281  sipRes = sipCpp->featureCount();
282  % End
283 
285  int __bool__() const;
286  % MethodCode
287  sipRes = true;
288  % End
289 #endif
290 
295  long featureCount() const;
296 
297  protected:
298 
307  void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
308 
316  void featureRemoved( QgsFeatureId fid );
317 
328  bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
329 
330 
331  signals:
332 
342  void progress( int i, bool &cancel ) SIP_SKIP;
343 
347  void finished();
348 
354  void cachedLayerDeleted();
355 
360  void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
361 
369  void featureAdded( QgsFeatureId fid );
370 
376  void invalidated();
377 
378  private slots:
379  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
380  void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
381  void featureDeleted( QgsFeatureId fid );
382  void onFeatureAdded( QgsFeatureId fid );
383  void attributeAdded( int field );
384  void attributeDeleted( int field );
385  void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
386  void layerDeleted();
387  void invalidate();
388 
389  private:
390 
391  void connectJoinedLayers() const;
392 
393  inline void cacheFeature( QgsFeature &feat )
394  {
395  QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this );
396  mCache.insert( feat.id(), cachedFeature );
397  }
398 
399  QgsVectorLayer *mLayer = nullptr;
400  QCache< QgsFeatureId, QgsCachedFeature > mCache;
401 
402  bool mCacheGeometry = true;
403  bool mFullCache = false;
404  QList<QgsAbstractCacheIndex *> mCacheIndices;
405 
406  QgsAttributeList mCachedAttributes;
407 
410  friend class QgsCachedFeature;
411 
420  bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
421 
422  friend class TestVectorLayerCache;
423 };
424 #endif // QgsVectorLayerCache_H
QgsFeatureIterator getFeatures(const QString &expression)
Query the layer for features matching a given expression.
QgsFeatureId id
Definition: qgsfeature.h:64
Wrapper for iterator of features from vector data provider or vector layer.
QgsWkbTypes::Type wkbType() const
Returns the geometry type for features in the cache.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
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 removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
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.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Is emitted when an attribute is changed.
QgsFields fields() const
Returns the fields associated with features in the cache.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are 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:68
Uses another iterator as backend and writes features to the cache.
#define SIP_SKIP
Definition: qgis_sip.h:119
bool isFidCached(QgsFeatureId fid) const
Check if a certain feature id is 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)...
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.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
bool hasFullCache() const
Returns true if the cache is complete, ie it contains all features.
This class caches features of a given QgsVectorLayer.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
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).
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.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown...