QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include <unordered_set>
28 #include <deque>
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 qgis::listToSet( mCache.keys() ); }
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 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  if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
398  {
399  mCacheUnorderedKeys.insert( feat.id() );
400  mCacheOrderedKeys.emplace_back( feat.id() );
401  }
402  }
403 
404  QgsVectorLayer *mLayer = nullptr;
405  QCache< QgsFeatureId, QgsCachedFeature > mCache;
406 
407  // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
408  // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
409  // the ordered list alone is far too slow to handle this -- searching for existing items in a list is magnitudes slower than the unordered_set
410  std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
411  std::deque< QgsFeatureId > mCacheOrderedKeys;
412 
413  bool mCacheGeometry = true;
414  bool mFullCache = false;
415  QList<QgsAbstractCacheIndex *> mCacheIndices;
416 
417  QgsAttributeList mCachedAttributes;
418 
421  friend class QgsCachedFeature;
422 
431  bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
432 
433  friend class TestVectorLayerCache;
434 };
435 #endif // QgsVectorLayerCache_H
QgsCachedFeatureWriterIterator
Uses another iterator as backend and writes features to the cache.
Definition: qgscachedfeatureiterator.h:103
QgsVectorLayerCache::setCacheSubsetOfAttributes
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
Definition: qgsvectorlayercache.cpp:84
qgsfeaturerequest.h
QgsVectorLayerCache
This class caches features of a given QgsVectorLayer.
Definition: qgsvectorlayercache.h:46
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:217
QgsVectorLayerCache::wkbType
QgsWkbTypes::Type wkbType() const
Returns the geometry type for features in the cache.
Definition: qgsvectorlayercache.cpp:202
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:135
QgsVectorLayerCache::QgsVectorLayerCache
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
Definition: qgsvectorlayercache.cpp:27
QgsVectorLayerCache::~QgsVectorLayerCache
~QgsVectorLayerCache() override
Definition: qgsvectorlayercache.cpp:49
qgsfeatureiterator.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsVectorLayerCache::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
Definition: qgsvectorlayercache.cpp:197
QgsVectorLayerCache::removeCachedFeature
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
Definition: qgsvectorlayercache.cpp:176
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
Definition: qgsvectorlayercache.cpp:400
field
const QgsField & field
Definition: qgsfield.h:463
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:451
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsVectorLayerCache::getFeature
QgsFeature getFeature(QgsFeatureId fid)
Query the layer for the feature with the given id.
Definition: qgsvectorlayercache.h:199
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
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
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:207
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:130
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:234
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsVectorLayerCache::featureCount
long long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown.
Definition: qgsvectorlayercache.cpp:212
QgsVectorLayerCache::layer
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
Definition: qgsvectorlayercache.cpp:192
QgsVectorLayerCache::featureAtId
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
Definition: qgsvectorlayercache.cpp:147
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
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:446
QgsVectorLayerCache::setFullCache
void setFullCache(bool fullCache)
This enables or disables full caching.
Definition: qgsvectorlayercache.cpp:89
QgsVectorLayerCache::hasFullCache
bool hasFullCache() const
Returns true if the cache is complete, ie it contains all features.
Definition: qgsvectorlayercache.h:164
QgsVectorLayerCache::cacheSize
int cacheSize()
Returns the maximum number of features this cache will hold.
Definition: qgsvectorlayercache.cpp:60
QgsCachedFeatureIterator
Delivers features from the cache.
Definition: qgscachedfeatureiterator.h:32
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorLayerCache::QgsCachedFeature
friend class QgsCachedFeature
Definition: qgsvectorlayercache.h:421
qgsfield.h
QgsVectorLayerCache::setCacheGeometry
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
Definition: qgsvectorlayercache.cpp:65
QgsVectorLayerCache::setCacheSize
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
Definition: qgsvectorlayercache.cpp:55
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QString &expression)
Query the layer for features matching a given expression.
Definition: qgsvectorlayercache.h:190
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
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:217
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureIds &fids)
Query the layer for the features with the given ids.
Definition: qgsvectorlayercache.h:209
QgsVectorLayerCache::cacheGeometry
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
Definition: qgsvectorlayercache.h:125
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsVectorLayerCache::cachedFeatureIds
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are cached.
Definition: qgsvectorlayercache.h:235