QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
25 #include <QElapsedTimer>
35 connect( mLayer, &QgsVectorLayer::destroyed,
this, &QgsVectorLayerCache::layerDeleted );
46 connectJoinedLayers();
51 qDeleteAll( mCacheIndices );
52 mCacheIndices.clear();
62 return mCache.maxCost();
68 bool mustInvalidate = shouldCacheGeometry && !mCacheGeometry;
69 mCacheGeometry = shouldCacheGeometry;
86 mCachedAttributes = attributes;
91 mFullCache = fullCache;
100 .setSubsetOfAttributes( mCachedAttributes )
113 if ( t.elapsed() > 1000 )
132 mCacheIndices.append( cacheIndex );
137 if ( cacheAddedAttributes )
149 bool featureFound =
false;
155 cachedFeature = mCache[ featureId ];
160 feature =
QgsFeature( *cachedFeature->feature() );
164 .setFilterFid( featureId )
165 .setSubsetOfAttributes( mCachedAttributes )
167 .nextFeature( feature ) )
169 cacheFeature( feature );
178 bool removed = mCache.remove( fid );
181 if (
auto unorderedIt = std::find( mCacheUnorderedKeys.begin(), mCacheUnorderedKeys.end(), fid ); unorderedIt != mCacheUnorderedKeys.end() )
183 mCacheUnorderedKeys.erase( unorderedIt );
185 if (
auto orderedIt = std::find( mCacheOrderedKeys.begin(), mCacheOrderedKeys.end(), fid ); orderedIt != mCacheOrderedKeys.end() )
186 mCacheOrderedKeys.erase( orderedIt );
199 return mLayer->
crs();
220 if ( fids.count() <= mCache.size() )
222 for (
const auto &idx : std::as_const( mCacheIndices ) )
224 idx->requestCompleted( featureRequest, fids );
236 const auto constMCacheIndices = mCacheIndices;
239 idx->flushFeature( fid );
243 void QgsVectorLayerCache::onAttributeValueChanged(
QgsFeatureId fid,
int field,
const QVariant &value )
249 cachedFeat->mFeature->setAttribute(
field, value );
255 void QgsVectorLayerCache::onJoinAttributeValueChanged(
QgsFeatureId fid,
int field,
const QVariant &value )
257 const QgsVectorLayer *joinLayer = qobject_cast<const QgsVectorLayer *>( sender() );
259 const auto constVectorJoins = mLayer->
vectorJoins();
262 if ( joinLayer == info.joinLayer() )
266 const QString fieldName = info.prefixedFieldName( joinLayer->
fields().
field(
field ) );
269 if ( feature.
isValid() && fieldIndex != -1 )
271 onAttributeValueChanged( feature.
id(), fieldIndex, value );
278 void QgsVectorLayerCache::featureDeleted(
QgsFeatureId fid )
280 mCache.remove( fid );
282 if (
auto it = mCacheUnorderedKeys.find( fid ); it != mCacheUnorderedKeys.end() )
284 mCacheUnorderedKeys.erase( it );
285 if (
auto orderedIt = std::find( mCacheOrderedKeys.begin(), mCacheOrderedKeys.end(), fid ); orderedIt != mCacheOrderedKeys.end() )
286 mCacheOrderedKeys.erase( orderedIt );
290 void QgsVectorLayerCache::onFeatureAdded(
QgsFeatureId fid )
305 void QgsVectorLayerCache::attributeAdded(
int field )
308 mCachedAttributes.append(
field );
312 void QgsVectorLayerCache::attributeDeleted(
int field )
315 mCachedAttributes.clear();
317 const auto constAttrs = attrs;
318 for (
int attr : constAttrs )
321 mCachedAttributes << attr;
322 else if ( attr >
field )
323 mCachedAttributes << attr - 1;
333 cachedFeat->mFeature->setGeometry( geom );
337 void QgsVectorLayerCache::layerDeleted()
343 void QgsVectorLayerCache::invalidate()
346 mCacheOrderedKeys.clear();
347 mCacheUnorderedKeys.clear();
355 const auto constMCacheIndices = mCacheIndices;
358 if ( idx->getCacheIterator( it, featureRequest ) )
369 if ( mCache.contains( featureRequest.
filterFid() ) )
378 if ( qgis::listToSet( mCache.keys() ).contains( featureRequest.
filterFids() ) )
403 bool requiresWriterIt =
true;
411 requiresWriterIt =
false;
416 requiresWriterIt = !canUseCacheForRequest( featureRequest, it );
423 requiresWriterIt =
false;
433 if ( mCacheGeometry && mLayer->
isSpatial() )
437 QSet<int> attrs = qgis::listToSet( featureRequest.
subsetOfAttributes() ) + qgis::listToSet( mCachedAttributes );
448 return mCache.contains( fid );
465 const auto constRequestedAttributes = requestedAttributes;
466 for (
int attr : constRequestedAttributes )
468 if ( !mCachedAttributes.contains( attr ) )
476 && !mCacheGeometry );
479 void QgsVectorLayerCache::connectJoinedLayers()
const
481 const auto constVectorJoins = mLayer->
vectorJoins();
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsCoordinateReferenceSystem crs
@ FilterFid
Filter using feature ID.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
const QgsFeatureIds & filterFids() const
Returns the feature IDs that should be fetched.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
@ NoFilter
No spatial filtering of features.
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
QgsWkbTypes::Type wkbType() const
Returns the geometry type for features in the cache.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
QgsFeatureId filterFid() const
Returns the feature ID that should be fetched.
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
~QgsVectorLayerCache() override
FilterType filterType() const
Returns the attribute/ID filter type which is currently set on this request.
Container of fields for a vector layer.
@ FilterFids
Filter using feature IDs.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
void attributeDeleted(int idx)
Will be emitted, when an attribute has been deleted from this vector layer.
QgsRectangle filterRect() const
Returns the rectangle from which features will be taken.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFeature targetedFeatureOf(const QgsVectorLayerJoinInfo *info, const QgsFeature &feature) const
Returns the targeted feature corresponding to the joined feature.
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
Type
The WKB type describes the number of dimensions a geometry has.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
void invalidated()
The cache has been invalidated and cleared.
QList< int > QgsAttributeList
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
@ SubsetOfAttributes
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
QgsAttributeList attributeList() const
Returns list of attribute indexes.
Flags flags() const
Returns the flags which affect how features are fetched.
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Emitted when an attribute is changed.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
QgsFields fields() const
Returns the fields associated with features in the cache.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
bool isValid() const
Returns the validity of this feature.
const QList< QgsVectorLayerJoinInfo > vectorJoins() const
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer and this cache.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
bool contains(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle contains other rectangle.
friend class QgsCachedFeatureWriterIterator
long long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
Defines left outer join from our vector layer to some other vector layer. The join is done based on [...
@ FilterExpression
Filter using expression.
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
QSet< QgsFeatureId > QgsFeatureIds
Qgis::SpatialFilterType spatialFilterType() const
Returns the spatial filter type which is currently set on this request.
This class represents a coordinate reference system (CRS).
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
QgsRectangle extent() const FINAL
Returns the extent of the layer.
Abstract base class for cache indices.
bool isFidCached(QgsFeatureId fid) const
Check if a certain feature id is cached.
void dataChanged()
Data of layer changed.
void setFullCache(bool fullCache)
This enables or disables full caching.
int cacheSize()
Returns the maximum number of features this cache will hold.
bool nextFeature(QgsFeature &f)
A geometry is the spatial representation of a feature.
Represents a vector layer which manages a vector based data sets.
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
friend class QgsCachedFeature
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
@ FilterNone
No filter is applied.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void updatedFields()
Emitted whenever the fields available from this layer have been changed.
QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.
void attributeAdded(int idx)
Will be emitted, when a new attribute has been added to this vector layer.
Wrapper for iterator of features from vector data provider or vector layer.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsVectorLayerJoinBuffer * joinBuffer()
Returns the join buffer object.
friend class QgsCachedFeatureIterator
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.