QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsvectordataproviderfeaturepool.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectordataproviderfeaturepool.h
3 --------------------------------------
4Date : 3.9.2018
5Copyright : (C) 2018 by Matthias Kuhn
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgsthreadingutils.h"
18
19#include "qgsfeaturerequest.h"
20
22 : QgsFeaturePool( layer )
23 , mSelectedOnly( selectedOnly )
24{
25 // Build spatial index
26 QgsFeature feature;
28 QgsFeatureIds featureIds;
29 if ( selectedOnly )
30 {
31 featureIds = layer->selectedFeatureIds();
32 req.setFilterFids( featureIds );
33 }
34
36 while ( it.nextFeature( feature ) )
37 {
38 if ( feature.hasGeometry() && !feature.geometry().isEmpty() )
39 {
40 insertFeature( feature );
41 featureIds.insert( feature.id() );
42 }
43 else
44 {
45 featureIds.remove( feature.id() );
46 }
47 }
48 setFeatureIds( featureIds );
49}
50
52{
53 Q_UNUSED( flags )
54 QgsFeatureList features;
55 features.append( feature );
56
57 bool res = false;
58
59 auto addFeatureSynchronized = [this, &features, &res]() {
60 QgsVectorLayer *lyr = layer();
61 if ( lyr )
62 res = lyr->dataProvider()->addFeatures( features );
63 };
64
65 QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
66
67 if ( !res )
68 return false;
69
70 feature.setId( features.front().id() );
71 if ( mSelectedOnly )
72 {
73 QgsThreadingUtils::runOnMainThread( [this, feature]() {
74 QgsVectorLayer *lyr = layer();
75 if ( lyr )
76 {
77 QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
78 selectedFeatureIds.insert( feature.id() );
79 lyr->selectByIds( selectedFeatureIds );
80 }
81 } );
82 }
83
84 insertFeature( feature );
85
86 return res;
87}
88
90{
91 Q_UNUSED( flags )
92
93 bool res = false;
94
95 auto addFeatureSynchronized = [this, &features, &res]() {
96 QgsVectorLayer *lyr = layer();
97 if ( lyr )
98 res = lyr->dataProvider()->addFeatures( features );
99 };
100
101 QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
102
103 if ( !res )
104 return false;
105
106 if ( mSelectedOnly )
107 {
108 QgsThreadingUtils::runOnMainThread( [this, features]() {
109 QgsVectorLayer *lyr = layer();
110 if ( lyr )
111 {
112 QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
113 for ( const QgsFeature &feature : std::as_const( features ) )
114 selectedFeatureIds.insert( feature.id() );
115 lyr->selectByIds( selectedFeatureIds );
116 }
117 } );
118 }
119
120 for ( const QgsFeature &feature : std::as_const( features ) )
121 insertFeature( feature );
122
123 return res;
124}
125
127{
128 QgsFeature origFeature;
129 getFeature( feature.id(), origFeature );
130
131 QgsGeometryMap geometryMap;
132 geometryMap.insert( feature.id(), feature.geometry() );
133 QgsChangedAttributesMap changedAttributesMap;
134 QgsAttributeMap attribMap;
135 const int attributeCount = feature.attributeCount();
136 for ( int i = 0, n = attributeCount; i < n; ++i )
137 {
138 attribMap.insert( i, feature.attributes().at( i ) );
139 }
140 changedAttributesMap.insert( feature.id(), attribMap );
141
142 QgsThreadingUtils::runOnMainThread( [this, geometryMap, changedAttributesMap]() {
143 QgsVectorLayer *lyr = layer();
144 if ( lyr )
145 {
146 lyr->dataProvider()->changeGeometryValues( geometryMap );
147 lyr->dataProvider()->changeAttributeValues( changedAttributesMap );
148 }
149 } );
150
151 refreshCache( feature, origFeature );
152}
153
155{
156 removeFeature( fid );
158 QgsVectorLayer *lyr = layer();
159 if ( lyr )
160 {
161 lyr->dataProvider()->deleteFeatures( QgsFeatureIds() << fid );
162 }
163 } );
164}
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
A feature pool is based on a vector layer and caches features.
void refreshCache(QgsFeature feature, const QgsFeature &origFeature)
Changes a feature in the cache and the spatial index.
void insertFeature(const QgsFeature &feature, bool skipLock=false)
Inserts a feature into the cache and the spatial index.
void setFeatureIds(const QgsFeatureIds &ids)
Sets all the feature ids governed by this feature pool.
void removeFeature(const QgsFeatureId featureId)
Removes a feature from the cache and the spatial index.
QgsVectorLayer * layer() const
Gets a pointer to the underlying layer.
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFeatureId id
Definition qgsfeature.h:66
int attributeCount() const
Returns the number of attributes attached to the feature.
void setId(QgsFeatureId id)
Sets the feature id for this feature.
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
static bool runOnMainThread(const Func &func, QgsFeedback *feedback=nullptr)
Guarantees that func is executed on the main thread.
void deleteFeature(QgsFeatureId fid) override
Removes a feature from this pool.
void updateFeature(QgsFeature &feature) override
Updates a feature in this pool.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
QgsVectorDataProviderFeaturePool(QgsVectorLayer *layer, bool selectedOnly=false)
Creates a new feature pool for the data provider of layer.
virtual bool changeGeometryValues(const QgsGeometryMap &geometry_map)
Changes geometries of existing features.
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features from the provider.
bool addFeatures(QgsFeatureList &flist, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
QMap< int, QVariant > QgsAttributeMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features