QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
31class QgsVectorLayer;
32class QgsFeature;
35
46class 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
236
244 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
245
252
257
262
266 QgsFields fields() const;
267
271 Qgis::WkbType 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
355
360 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
361
370
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
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:155
Abstract base class for cache indices.
Definition: qgscacheindex.h:32
Delivers features from the cache.
Uses another iterator as backend and writes features to the cache.
This class represents a coordinate reference system (CRS).
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class caches features of a given QgsVectorLayer.
bool isFidCached(QgsFeatureId fid) const
Check if a certain feature id is cached.
void setFullCache(bool fullCache)
This enables or disables full caching.
bool hasFullCache() const
Returns true if the cache is complete, ie it contains all features.
QgsFeatureIterator getFeatures(const QString &expression)
Query the layer for features matching a given expression.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
void invalidated()
The cache has been invalidated and cleared.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
QgsFeatureIterator getFeatures(const QgsRectangle &rectangle)
Query the layer for the features which intersect the specified rectangle.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer and this cache.
QgsFields fields() const
Returns the fields associated with features in the cache.
QgsFeatureIterator getFeatures(const QgsFeatureIds &fids)
Query the layer for the features with the given ids.
QgsFeature getFeature(QgsFeatureId fid)
Query the layer for the feature with the given id.
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Emitted when an attribute is changed.
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
long long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are cached.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
Qgis::WkbType wkbType() const
Returns the geometry type for features in the cache.
int cacheSize()
Returns the maximum number of features this cache will hold.
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsField & field
Definition: qgsfield.h:501