QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 = std::make_unique<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 }
82
83 inline const QgsFeature *feature() { return mFeature.get(); }
84
85 bool allAttributesFetched() const;
86
87 bool geometryFetched() const;
88
89 private:
90 std::unique_ptr<QgsFeature> mFeature;
91 QgsVectorLayerCache *mCache = nullptr;
92 bool mAllAttributesFetched = true;
93 bool mGeometryFetched = false;
94
95 friend class QgsVectorLayerCache;
96 Q_DISABLE_COPY( QgsCachedFeature )
97 };
98
99 public:
100 QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent SIP_TRANSFERTHIS = nullptr );
101 ~QgsVectorLayerCache() override;
102
109 void setCacheSize( int cacheSize );
110
118 int cacheSize();
119
126 void setCacheGeometry( bool cacheGeometry );
127
132 bool cacheGeometry() const { return mCacheGeometry; }
133
140 void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
141
149 QgsAttributeList cacheSubsetOfAttributes() const;
150
157 void setCacheAddedAttributes( bool cacheAddedAttributes );
158
172 void setFullCache( bool fullCache );
173
180 bool hasFullCache() const { return mFullCache; }
181
190 void addCacheIndex( QgsAbstractCacheIndex *cacheIndex SIP_TRANSFER );
191
201 QgsFeatureIterator getFeatures( const QgsFeatureRequest &featureRequest = QgsFeatureRequest() );
202
206 inline QgsFeatureIterator getFeatures( const QString &expression ) { return getFeatures( QgsFeatureRequest( expression ) ); }
207
213 {
214 QgsFeature feature;
215 getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
216 return feature;
217 }
218
222 inline QgsFeatureIterator getFeatures( const QgsFeatureIds &fids ) { return getFeatures( QgsFeatureRequest( fids ) ); }
223
227 inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle ) { return getFeatures( QgsFeatureRequest( rectangle ) ); }
228
235 bool isFidCached( QgsFeatureId fid ) const;
236
241 QgsFeatureIds cachedFeatureIds() const;
242
250 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
251
265 bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
266
280 bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );
281
287 bool removeCachedFeature( QgsFeatureId fid );
288
292 QgsVectorLayer *layer();
293
297 QgsCoordinateReferenceSystem sourceCrs() const;
298
302 QgsFields fields() const;
303
307 Qgis::WkbType wkbType() const;
308
309#ifdef SIP_RUN
310 // clang-format off
311
316 int __len__() const;
317 % MethodCode
318 sipRes = sipCpp->featureCount();
319 % End
320
322 int __bool__() const;
323 % MethodCode
324 sipRes = true;
325 % End
326// clang-format on
327#endif
328
333 long long featureCount() const;
334
335 protected:
344 void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
345
353 void featureRemoved( QgsFeatureId fid );
354
365 bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
366
367
368 signals:
369
379 void progress( int i, bool &cancel ) SIP_SKIP;
380
384 void finished();
385
392
397 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
398
407
414
415 private slots:
416 void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
417 void onJoinAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
418 void featureDeleted( QgsFeatureId fid );
419 void onFeatureAdded( QgsFeatureId fid );
420 void attributeAdded( int field );
421 void attributeDeleted( int field );
422 void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
423 void layerDeleted();
424 void invalidate();
425
426 private:
427 void connectJoinedLayers() const;
428
429 inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched, bool geometryFetched = false )
430 {
431 QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched || mCacheGeometry );
432 mCache.insert( feat.id(), cachedFeature );
433 if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
434 {
435 mCacheUnorderedKeys.insert( feat.id() );
436 mCacheOrderedKeys.emplace_back( feat.id() );
437 }
438 }
439
440 QgsVectorLayer *mLayer = nullptr;
441 QCache< QgsFeatureId, QgsCachedFeature > mCache;
442
443 // we need two containers here. One is used for efficient tracking of the IDs which have been added to the cache, the other
444 // is used to store the order of the incoming feature ids, so that we can correctly iterate through features in the original order.
445 // 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
446 std::unordered_set< QgsFeatureId > mCacheUnorderedKeys;
447 std::deque< QgsFeatureId > mCacheOrderedKeys;
448
449 bool mCacheGeometry = true;
450 bool mFullCache = false;
451 QList<QgsAbstractCacheIndex *> mCacheIndices;
452
453 QgsAttributeList mCachedAttributes;
454
457 friend class QgsCachedFeature;
458
467 bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
468
470};
471#endif // QgsVectorLayerCache_H
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
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:68
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:52
#define SIP_SKIP
Definition qgis_sip.h:133
#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