QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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:
57 class CORE_EXPORT QgsCachedFeature
58 {
59 public:
68 QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache, bool allAttributesFetched, bool geometryFetched )
69 : mCache( vlCache )
70 , mAllAttributesFetched( allAttributesFetched )
71 , mGeometryFetched( geometryFetched )
72 {
73 mFeature = 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 }
82
86 inline QgsFeature feature() const { return mFeature; }
87
88 bool allAttributesFetched() const;
89
90 bool geometryFetched() const;
91
92 private:
93 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( long long cacheSize );
113
121 long long cacheSize() const;
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 ) { return getFeatures( QgsFeatureRequest( expression ) ); }
210
216 {
217 QgsFeature feature;
218 getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
219 return feature;
220 }
221
225 inline QgsFeatureIterator getFeatures( const QgsFeatureIds &fids ) { return getFeatures( QgsFeatureRequest( fids ) ); }
226
230 inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle ) { return getFeatures( QgsFeatureRequest( rectangle ) ); }
231
238 bool isFidCached( QgsFeatureId fid ) const;
239
244 QgsFeatureIds cachedFeatureIds() const;
245
253 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
254
268 bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
269
283 bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
284
290 bool removeCachedFeature( QgsFeatureId fid );
291
295 QgsVectorLayer *layer();
296
300 QgsCoordinateReferenceSystem sourceCrs() const;
301
305 QgsFields fields() const;
306
310 Qgis::WkbType wkbType() const;
311
312#ifdef SIP_RUN
313 // clang-format off
314
319 int __len__() const;
320 % MethodCode
321 sipRes = sipCpp->featureCount();
322 % End
323
325 int __bool__() const;
326 % MethodCode
327 sipRes = true;
328 % End
329// clang-format on
330#endif
331
336 long long featureCount() const;
337
338 protected:
347 void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
348
356 void featureRemoved( QgsFeatureId fid );
357
368 bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
369
370
371 signals:
372
382 void progress( int i, bool &cancel ) SIP_SKIP;
383
387 void finished();
388
395
405 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
406
417
431
444
460
467
468 private slots:
469 void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
470 void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
471 void onFeatureDeleted( QgsFeatureId fid );
472 void onFeatureAdded( QgsFeatureId fid );
473 void attributeAdded( int field );
474 void attributeDeleted( int field );
475 void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
476 void layerDeleted();
477 void invalidate();
478
479 private:
480 void connectJoinedLayers() const;
481
482 inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched, bool geometryFetched = false )
483 {
484 QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched || mCacheGeometry );
485 mCache.insert( feat.id(), cachedFeature );
486 if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
487 {
488 mCacheUnorderedKeys.insert( feat.id() );
489 mCacheOrderedKeys.emplace_back( feat.id() );
490 }
491 }
492
493 QgsVectorLayer *mLayer = nullptr;
494 QCache< QgsFeatureId, QgsCachedFeature > mCache;
495
496 // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
497 // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
498 // 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
499 std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
500 std::deque< QgsFeatureId > mCacheOrderedKeys;
501
502 bool mCacheGeometry = true;
503 bool mFullCache = false;
504 QList<QgsAbstractCacheIndex *> mCacheIndices;
505 bool mCacheAddedAttributes = false;
506
507 QgsAttributeList mCachedAttributes;
508
511 friend class QgsCachedFeature;
512
521 bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
522
524};
525#endif // QgsVectorLayerCache_H
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:314
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:60
QgsFeatureId id
Definition qgsfeature.h:63
Container of fields for a vector layer.
Definition qgsfields.h:45
A geometry is the spatial representation of a feature.
A rectangle specified with double values.
void featureAboutToBeAdded(QgsFeatureId fid)
Emitted when a new feature is about to be added to the cache (as a result of feature addition to the ...
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.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted from the layer and this cache.
void setCacheSize(long long cacheSize)
Sets the maximum number of features to keep 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.
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.
void featureAboutToBeDeleted(QgsFeatureId fid)
Emitted when a feature is about to be deleted from this cache (as a result of a feature deletion from...
friend class QgsCachedFeatureIterator
long long cacheSize() const
Returns the maximum number of features this cache will hold.
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:52
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:30