QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "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 CORE_EXPORT QgsCachedFeature
58 {
59 public:
60
68 QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache, bool allAttributesFetched )
69 : mCache( vlCache )
70 , mAllAttributesFetched( allAttributesFetched )
71 {
72 mFeature = new QgsFeature( feat );
73 }
74
75 ~QgsCachedFeature()
76 {
77 // That's the reason we need this wrapper:
78 // Inform the cache that this feature has been removed
79 mCache->featureRemoved( mFeature->id() );
80 delete mFeature;
81 }
82
83 inline const QgsFeature *feature() { return mFeature; }
84
85 bool allAttributesFetched() const;
86
87 private:
88 QgsFeature *mFeature = nullptr;
89 QgsVectorLayerCache *mCache = nullptr;
90 bool mAllAttributesFetched = true;
91
92 friend class QgsVectorLayerCache;
93 Q_DISABLE_COPY( QgsCachedFeature )
94 };
95
96 public:
97 QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
98 ~QgsVectorLayerCache() override;
99
106 void setCacheSize( int cacheSize );
107
115 int cacheSize();
116
123 void setCacheGeometry( bool cacheGeometry );
124
130 bool cacheGeometry() const { return mCacheGeometry; }
131
138 void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
139
147 QgsAttributeList cacheSubsetOfAttributes( ) const;
148
155 void setCacheAddedAttributes( bool cacheAddedAttributes );
156
170 void setFullCache( bool fullCache );
171
179 bool hasFullCache() const { return mFullCache; }
180
189 void addCacheIndex( QgsAbstractCacheIndex *cacheIndex SIP_TRANSFER );
190
200 QgsFeatureIterator getFeatures( const QgsFeatureRequest &featureRequest = QgsFeatureRequest() );
201
205 inline QgsFeatureIterator getFeatures( const QString &expression )
206 {
207 return getFeatures( QgsFeatureRequest( expression ) );
208 }
209
215 {
216 QgsFeature feature;
217 getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
218 return feature;
219 }
220
225 {
226 return getFeatures( QgsFeatureRequest( fids ) );
227 }
228
232 inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
233 {
234 return getFeatures( QgsFeatureRequest( rectangle ) );
235 }
236
243 bool isFidCached( QgsFeatureId fid ) const;
244
250 QgsFeatureIds cachedFeatureIds() const;
251
259 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
260
274 bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
275
281 bool removeCachedFeature( QgsFeatureId fid );
282
286 QgsVectorLayer *layer();
287
291 QgsCoordinateReferenceSystem sourceCrs() const;
292
296 QgsFields fields() const;
297
301 Qgis::WkbType wkbType() const;
302
303#ifdef SIP_RUN
304
309 int __len__() const;
310 % MethodCode
311 sipRes = sipCpp->featureCount();
312 % End
313
315 int __bool__() const;
316 % MethodCode
317 sipRes = true;
318 % End
319#endif
320
325 long long featureCount() const;
326
327 protected:
328
337 void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
338
346 void featureRemoved( QgsFeatureId fid );
347
358 bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
359
360
361 signals:
362
372 void progress( int i, bool &cancel ) SIP_SKIP;
373
377 void finished();
378
385
390 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
391
400
407
408 private slots:
409 void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
410 void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
411 void featureDeleted( QgsFeatureId fid );
412 void onFeatureAdded( QgsFeatureId fid );
413 void attributeAdded( int field );
414 void attributeDeleted( int field );
415 void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
416 void layerDeleted();
417 void invalidate();
418
419 private:
420
421 void connectJoinedLayers() const;
422
423 inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched )
424 {
425 QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched );
426 mCache.insert( feat.id(), cachedFeature );
427 if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
428 {
429 mCacheUnorderedKeys.insert( feat.id() );
430 mCacheOrderedKeys.emplace_back( feat.id() );
431 }
432 }
433
434 QgsVectorLayer *mLayer = nullptr;
435 QCache< QgsFeatureId, QgsCachedFeature > mCache;
436
437 // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
438 // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
439 // 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
440 std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
441 std::deque< QgsFeatureId > mCacheOrderedKeys;
442
443 bool mCacheGeometry = true;
444 bool mFullCache = false;
445 QList<QgsAbstractCacheIndex *> mCacheIndices;
446
447 QgsAttributeList mCachedAttributes;
448
451 friend class QgsCachedFeature;
452
461 bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
462
463 friend class TestVectorLayerCache;
464};
465#endif // QgsVectorLayerCache_H
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:182
Abstract base class for cache indices.
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.
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
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.
A rectangle specified with double values.
This class caches features of 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 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
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:27