QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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 <deque>
23#include <unordered_set>
24
25#include "qgis_core.h"
26#include "qgis_sip.h"
27#include "qgsfeatureiterator.h"
28#include "qgsfeaturerequest.h"
29#include "qgsfield.h"
30
31#include <QCache>
32
33class QgsVectorLayer;
34class QgsFeature;
37
46
47class CORE_EXPORT QgsVectorLayerCache : public QObject
48{
49 Q_OBJECT
50
51 private:
52
58 class CORE_EXPORT QgsCachedFeature
59 {
60 public:
61
70 QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache, bool allAttributesFetched, bool geometryFetched )
71 : mCache( vlCache )
72 , mAllAttributesFetched( allAttributesFetched )
73 , mGeometryFetched( geometryFetched )
74 {
75 mFeature = std::make_unique<QgsFeature>( feat );
76 }
77
78 ~QgsCachedFeature()
79 {
80 // That's the reason we need this wrapper:
81 // Inform the cache that this feature has been removed
82 mCache->featureRemoved( mFeature->id() );
83
84 }
85
86 inline const QgsFeature *feature() { return mFeature.get(); }
87
88 bool allAttributesFetched() const;
89
90 bool geometryFetched() const;
91
92 private:
93 std::unique_ptr<QgsFeature> mFeature;
94 QgsVectorLayerCache *mCache = nullptr;
95 bool mAllAttributesFetched = true;
96 bool mGeometryFetched = false;
97
98 friend class QgsVectorLayerCache;
99 Q_DISABLE_COPY( QgsCachedFeature )
100 };
101
102 public:
103 QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
104 ~QgsVectorLayerCache() override;
105
112 void setCacheSize( int cacheSize );
113
121 int cacheSize();
122
129 void setCacheGeometry( bool cacheGeometry );
130
135 bool cacheGeometry() const { return mCacheGeometry; }
136
143 void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
144
152 QgsAttributeList cacheSubsetOfAttributes( ) const;
153
160 void setCacheAddedAttributes( bool cacheAddedAttributes );
161
175 void setFullCache( bool fullCache );
176
183 bool hasFullCache() const { return mFullCache; }
184
193 void addCacheIndex( QgsAbstractCacheIndex *cacheIndex SIP_TRANSFER );
194
204 QgsFeatureIterator getFeatures( const QgsFeatureRequest &featureRequest = QgsFeatureRequest() );
205
209 inline QgsFeatureIterator getFeatures( const QString &expression )
210 {
211 return getFeatures( QgsFeatureRequest( expression ) );
212 }
213
219 {
220 QgsFeature feature;
221 getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
222 return feature;
223 }
224
229 {
230 return getFeatures( QgsFeatureRequest( fids ) );
231 }
232
236 inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
237 {
238 return getFeatures( QgsFeatureRequest( rectangle ) );
239 }
240
247 bool isFidCached( QgsFeatureId fid ) const;
248
253 QgsFeatureIds cachedFeatureIds() const;
254
262 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
263
277 bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
278
292 bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
293
299 bool removeCachedFeature( QgsFeatureId fid );
300
304 QgsVectorLayer *layer();
305
309 QgsCoordinateReferenceSystem sourceCrs() const;
310
314 QgsFields fields() const;
315
319 Qgis::WkbType wkbType() const;
320
321#ifdef SIP_RUN
322
327 int __len__() const;
328 % MethodCode
329 sipRes = sipCpp->featureCount();
330 % End
331
333 int __bool__() const;
334 % MethodCode
335 sipRes = true;
336 % End
337#endif
338
343 long long featureCount() const;
344
345 protected:
346
355 void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
356
364 void featureRemoved( QgsFeatureId fid );
365
376 bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
377
378
379 signals:
380
390 void progress( int i, bool &cancel ) SIP_SKIP;
391
395 void finished();
396
403
408 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
409
418
425
426 private slots:
427 void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
428 void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
429 void featureDeleted( QgsFeatureId fid );
430 void onFeatureAdded( QgsFeatureId fid );
431 void attributeAdded( int field );
432 void attributeDeleted( int field );
433 void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
434 void layerDeleted();
435 void invalidate();
436
437 private:
438
439 void connectJoinedLayers() const;
440
441 inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched, bool geometryFetched = false )
442 {
443 QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched || mCacheGeometry );
444 mCache.insert( feat.id(), cachedFeature );
445 if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
446 {
447 mCacheUnorderedKeys.insert( feat.id() );
448 mCacheOrderedKeys.emplace_back( feat.id() );
449 }
450 }
451
452 QgsVectorLayer *mLayer = nullptr;
453 QCache< QgsFeatureId, QgsCachedFeature > mCache;
454
455 // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
456 // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
457 // 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
458 std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
459 std::deque< QgsFeatureId > mCacheOrderedKeys;
460
461 bool mCacheGeometry = true;
462 bool mFullCache = false;
463 QList<QgsAbstractCacheIndex *> mCacheIndices;
464
465 QgsAttributeList mCachedAttributes;
466
469 friend class QgsCachedFeature;
470
479 bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
480
482};
483#endif // QgsVectorLayerCache_H
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
Abstract base class for cache indices.
Delivers features from 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.
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.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
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.
friend class QgsCachedFeatureWriterIterator
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 ...
friend class TestVectorLayerCache
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
int cacheSize()
Returns the maximum number of features this cache will hold.
friend class QgsCachedFeatureIterator
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.
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:134
#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:28