QGIS API Documentation 3.43.0-Master (ebb4087afc0)
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
69 QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache, bool allAttributesFetched, bool geometryFetched )
70 : mCache( vlCache )
71 , mAllAttributesFetched( allAttributesFetched )
72 , mGeometryFetched( geometryFetched )
73 {
74 mFeature = new QgsFeature( feat );
75 }
76
77 ~QgsCachedFeature()
78 {
79 // That's the reason we need this wrapper:
80 // Inform the cache that this feature has been removed
81 mCache->featureRemoved( mFeature->id() );
82 delete mFeature;
83 }
84
85 inline const QgsFeature *feature() { return mFeature; }
86
87 bool allAttributesFetched() const;
88
89 bool geometryFetched() const;
90
91 private:
92 QgsFeature *mFeature = nullptr;
93 QgsVectorLayerCache *mCache = nullptr;
94 bool mAllAttributesFetched = true;
95 bool mGeometryFetched = false;
96
97 friend class QgsVectorLayerCache;
98 Q_DISABLE_COPY( QgsCachedFeature )
99 };
100
101 public:
102 QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
103 ~QgsVectorLayerCache() override;
104
111 void setCacheSize( int cacheSize );
112
120 int cacheSize();
121
128 void setCacheGeometry( bool cacheGeometry );
129
134 bool cacheGeometry() const { return mCacheGeometry; }
135
142 void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
143
151 QgsAttributeList cacheSubsetOfAttributes( ) const;
152
159 void setCacheAddedAttributes( bool cacheAddedAttributes );
160
174 void setFullCache( bool fullCache );
175
182 bool hasFullCache() const { return mFullCache; }
183
192 void addCacheIndex( QgsAbstractCacheIndex *cacheIndex SIP_TRANSFER );
193
203 QgsFeatureIterator getFeatures( const QgsFeatureRequest &featureRequest = QgsFeatureRequest() );
204
208 inline QgsFeatureIterator getFeatures( const QString &expression )
209 {
210 return getFeatures( QgsFeatureRequest( expression ) );
211 }
212
218 {
219 QgsFeature feature;
220 getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
221 return feature;
222 }
223
228 {
229 return getFeatures( QgsFeatureRequest( fids ) );
230 }
231
235 inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
236 {
237 return getFeatures( QgsFeatureRequest( rectangle ) );
238 }
239
246 bool isFidCached( QgsFeatureId fid ) const;
247
252 QgsFeatureIds cachedFeatureIds() const;
253
261 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
262
276 bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
277
291 bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
292
298 bool removeCachedFeature( QgsFeatureId fid );
299
303 QgsVectorLayer *layer();
304
308 QgsCoordinateReferenceSystem sourceCrs() const;
309
313 QgsFields fields() const;
314
318 Qgis::WkbType wkbType() const;
319
320#ifdef SIP_RUN
321
326 int __len__() const;
327 % MethodCode
328 sipRes = sipCpp->featureCount();
329 % End
330
332 int __bool__() const;
333 % MethodCode
334 sipRes = true;
335 % End
336#endif
337
342 long long featureCount() const;
343
344 protected:
345
354 void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
355
363 void featureRemoved( QgsFeatureId fid );
364
375 bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
376
377
378 signals:
379
389 void progress( int i, bool &cancel ) SIP_SKIP;
390
394 void finished();
395
402
407 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
408
417
424
425 private slots:
426 void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
427 void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
428 void featureDeleted( QgsFeatureId fid );
429 void onFeatureAdded( QgsFeatureId fid );
430 void attributeAdded( int field );
431 void attributeDeleted( int field );
432 void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
433 void layerDeleted();
434 void invalidate();
435
436 private:
437
438 void connectJoinedLayers() const;
439
440 inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched, bool geometryFetched = false )
441 {
442 QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched || mCacheGeometry );
443 mCache.insert( feat.id(), cachedFeature );
444 if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
445 {
446 mCacheUnorderedKeys.insert( feat.id() );
447 mCacheOrderedKeys.emplace_back( feat.id() );
448 }
449 }
450
451 QgsVectorLayer *mLayer = nullptr;
452 QCache< QgsFeatureId, QgsCachedFeature > mCache;
453
454 // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
455 // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
456 // 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
457 std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
458 std::deque< QgsFeatureId > mCacheOrderedKeys;
459
460 bool mCacheGeometry = true;
461 bool mFullCache = false;
462 QList<QgsAbstractCacheIndex *> mCacheIndices;
463
464 QgsAttributeList mCachedAttributes;
465
468 friend class QgsCachedFeature;
469
478 bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
479
480 friend class TestVectorLayerCache;
481};
482#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.
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: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.
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
#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