QGIS API Documentation 3.43.0-Master (32433f7016e)
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
45class CORE_EXPORT QgsVectorLayerCache : public QObject
46{
47 Q_OBJECT
48
49 private:
50
56 class CORE_EXPORT QgsCachedFeature
57 {
58 public:
59
68 QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache, bool allAttributesFetched, bool geometryFetched )
69 : mCache( vlCache )
70 , mAllAttributesFetched( allAttributesFetched )
71 , mGeometryFetched( geometryFetched )
72 {
73 mFeature = new QgsFeature( feat );
74 }
75
76 ~QgsCachedFeature()
77 {
78 // That's the reason we need this wrapper:
79 // Inform the cache that this feature has been removed
80 mCache->featureRemoved( mFeature->id() );
81 delete mFeature;
82 }
83
84 inline const QgsFeature *feature() { return mFeature; }
85
86 bool allAttributesFetched() const;
87
88 bool geometryFetched() const;
89
90 private:
91 QgsFeature *mFeature = nullptr;
92 QgsVectorLayerCache *mCache = nullptr;
93 bool mAllAttributesFetched = true;
94 bool mGeometryFetched = false;
95
96 friend class QgsVectorLayerCache;
97 Q_DISABLE_COPY( QgsCachedFeature )
98 };
99
100 public:
101 QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
102 ~QgsVectorLayerCache() override;
103
110 void setCacheSize( int cacheSize );
111
119 int cacheSize();
120
127 void setCacheGeometry( bool cacheGeometry );
128
133 bool cacheGeometry() const { return mCacheGeometry; }
134
141 void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
142
150 QgsAttributeList cacheSubsetOfAttributes( ) const;
151
158 void setCacheAddedAttributes( bool cacheAddedAttributes );
159
173 void setFullCache( bool fullCache );
174
181 bool hasFullCache() const { return mFullCache; }
182
191 void addCacheIndex( QgsAbstractCacheIndex *cacheIndex SIP_TRANSFER );
192
202 QgsFeatureIterator getFeatures( const QgsFeatureRequest &featureRequest = QgsFeatureRequest() );
203
207 inline QgsFeatureIterator getFeatures( const QString &expression )
208 {
209 return getFeatures( QgsFeatureRequest( expression ) );
210 }
211
217 {
218 QgsFeature feature;
219 getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
220 return feature;
221 }
222
227 {
228 return getFeatures( QgsFeatureRequest( fids ) );
229 }
230
234 inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
235 {
236 return getFeatures( QgsFeatureRequest( rectangle ) );
237 }
238
245 bool isFidCached( QgsFeatureId fid ) const;
246
251 QgsFeatureIds cachedFeatureIds() const;
252
260 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
261
275 bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
276
290 bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
291
297 bool removeCachedFeature( QgsFeatureId fid );
298
302 QgsVectorLayer *layer();
303
307 QgsCoordinateReferenceSystem sourceCrs() const;
308
312 QgsFields fields() const;
313
317 Qgis::WkbType wkbType() const;
318
319#ifdef SIP_RUN
320
325 int __len__() const;
326 % MethodCode
327 sipRes = sipCpp->featureCount();
328 % End
329
331 int __bool__() const;
332 % MethodCode
333 sipRes = true;
334 % End
335#endif
336
341 long long featureCount() const;
342
343 protected:
344
353 void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
354
362 void featureRemoved( QgsFeatureId fid );
363
374 bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
375
376
377 signals:
378
388 void progress( int i, bool &cancel ) SIP_SKIP;
389
393 void finished();
394
401
406 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
407
416
423
424 private slots:
425 void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
426 void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
427 void featureDeleted( QgsFeatureId fid );
428 void onFeatureAdded( QgsFeatureId fid );
429 void attributeAdded( int field );
430 void attributeDeleted( int field );
431 void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
432 void layerDeleted();
433 void invalidate();
434
435 private:
436
437 void connectJoinedLayers() const;
438
439 inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched, bool geometryFetched = false )
440 {
441 QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched || mCacheGeometry );
442 mCache.insert( feat.id(), cachedFeature );
443 if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
444 {
445 mCacheUnorderedKeys.insert( feat.id() );
446 mCacheOrderedKeys.emplace_back( feat.id() );
447 }
448 }
449
450 QgsVectorLayer *mLayer = nullptr;
451 QCache< QgsFeatureId, QgsCachedFeature > mCache;
452
453 // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
454 // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
455 // 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
456 std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
457 std::deque< QgsFeatureId > mCacheOrderedKeys;
458
459 bool mCacheGeometry = true;
460 bool mFullCache = false;
461 QList<QgsAbstractCacheIndex *> mCacheIndices;
462
463 QgsAttributeList mCachedAttributes;
464
467 friend class QgsCachedFeature;
468
477 bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
478
479 friend class TestVectorLayerCache;
480};
481#endif // QgsVectorLayerCache_H
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
Abstract base class for cache indices.
Delivers features from the cache.
Uses another iterator as backend and writes features to the cache.
Represents a coordinate reference system (CRS).
Wrapper for iterator of features from vector data provider or vector layer.
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:58
QgsFeatureId id
Definition qgsfeature.h:66
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry is the spatial representation of a feature.
A rectangle specified with double values.
Caches features for a given QgsVectorLayer.
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 invalidated()
The cache has been invalidated and cleared.
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.
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 attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Emitted when an attribute is changed.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
Represents a vector layer which manages a vector based dataset.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:27