QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmdropfields.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmdropfields.cpp
3 ---------------------------------
4 begin : November 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
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
19
21
22Qgis::ProcessingAlgorithmFlags QgsDropTableFieldsAlgorithm::flags() const
23{
25}
26
27QString QgsDropTableFieldsAlgorithm::name() const
28{
29 return QStringLiteral( "deletecolumn" );
30}
31
32QString QgsDropTableFieldsAlgorithm::displayName() const
33{
34 return QObject::tr( "Drop field(s)" );
35}
36
37QString QgsDropTableFieldsAlgorithm::shortHelpString() const
38{
39 return QObject::tr( "This algorithm takes a vector layer and generates a new one that has the exact same content but without the selected columns." );
40}
41
42QString QgsDropTableFieldsAlgorithm::shortDescription() const
43{
44 return QObject::tr( "Deletes fields from a vector layer." );
45}
46
47QStringList QgsDropTableFieldsAlgorithm::tags() const
48{
49 return QObject::tr( "drop,delete,remove,fields,columns,attributes" ).split( ',' );
50}
51
52QString QgsDropTableFieldsAlgorithm::group() const
53{
54 return QObject::tr( "Vector table" );
55}
56
57QString QgsDropTableFieldsAlgorithm::groupId() const
58{
59 return QStringLiteral( "vectortable" );
60}
61
62QString QgsDropTableFieldsAlgorithm::outputName() const
63{
64 return QObject::tr( "Remaining fields" );
65}
66
67QList<int> QgsDropTableFieldsAlgorithm::inputLayerTypes() const
68{
69 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
70}
71
72Qgis::ProcessingFeatureSourceFlags QgsDropTableFieldsAlgorithm::sourceFlags() const
73{
75}
76
77QgsDropTableFieldsAlgorithm *QgsDropTableFieldsAlgorithm::createInstance() const
78{
79 return new QgsDropTableFieldsAlgorithm();
80}
81
82void QgsDropTableFieldsAlgorithm::initParameters( const QVariantMap & )
83{
84 addParameter( new QgsProcessingParameterField( QStringLiteral( "COLUMN" ), QObject::tr( "Fields to drop" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, true ) );
85}
86
87QgsFields QgsDropTableFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
88{
89 QgsFields outFields = inputFields;
90 // loop through twice - first we need to build up a list of original attribute indices
91 for ( const QString &field : mFieldsToDelete )
92 {
93 const int index = inputFields.lookupField( field );
94 if ( index >= 0 )
95 mFieldIndices.append( index );
96 }
97
98 // important - make sure we remove from the end so we aren't changing used indices as we go
99 std::sort( mFieldIndices.begin(), mFieldIndices.end(), std::greater<int>() );
100
101 // this second time we make a cleaned version of the fields
102 for ( const int index : std::as_const( mFieldIndices ) )
103 {
104 outFields.remove( index );
105 }
106 return outFields;
107}
108
109bool QgsDropTableFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
110{
111 mFieldsToDelete = parameterAsStrings( parameters, QStringLiteral( "COLUMN" ), context );
112
113 if ( feedback )
114 {
115 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
116 if ( source )
117 {
118 for ( const QString &field : std::as_const( mFieldsToDelete ) )
119 {
120 const int index = source->fields().lookupField( field );
121 if ( index < 0 )
122 {
123 feedback->pushInfo( QObject::tr( "Field “%1” does not exist in input layer " ).arg( field ) );
124 }
125 }
126 }
127 }
128
129 return true;
130}
131
132QgsFeatureList QgsDropTableFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
133{
134 QgsFeature f = feature;
135 QgsAttributes attributes = f.attributes();
136 for ( const int index : mFieldIndices )
137 {
138 attributes.remove( index );
139 }
140 f.setAttributes( attributes );
141 return QgsFeatureList() << f;
142}
143
144bool QgsDropTableFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
145{
146 Q_UNUSED( layer )
147 return false;
148}
149
150
151//
152// QgsRetainTableFieldsAlgorithm
153//
154
155Qgis::ProcessingAlgorithmFlags QgsRetainTableFieldsAlgorithm::flags() const
156{
158}
159
160QString QgsRetainTableFieldsAlgorithm::name() const
161{
162 return QStringLiteral( "retainfields" );
163}
164
165QString QgsRetainTableFieldsAlgorithm::displayName() const
166{
167 return QObject::tr( "Retain fields" );
168}
169
170QString QgsRetainTableFieldsAlgorithm::shortHelpString() const
171{
172 return QObject::tr( "This algorithm takes a vector layer and generates a new one that retains only the selected fields. All other fields will be dropped." );
173}
174
175QString QgsRetainTableFieldsAlgorithm::shortDescription() const
176{
177 return QObject::tr( "Retains selected fields from a vector layer." );
178}
179
180QStringList QgsRetainTableFieldsAlgorithm::tags() const
181{
182 return QObject::tr( "drop,delete,remove,retain,keep,other,fields,columns,attributes" ).split( ',' );
183}
184
185QString QgsRetainTableFieldsAlgorithm::group() const
186{
187 return QObject::tr( "Vector table" );
188}
189
190QString QgsRetainTableFieldsAlgorithm::groupId() const
191{
192 return QStringLiteral( "vectortable" );
193}
194
195QString QgsRetainTableFieldsAlgorithm::outputName() const
196{
197 return QObject::tr( "Retained fields" );
198}
199
200QList<int> QgsRetainTableFieldsAlgorithm::inputLayerTypes() const
201{
202 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
203}
204
205Qgis::ProcessingFeatureSourceFlags QgsRetainTableFieldsAlgorithm::sourceFlags() const
206{
208}
209
210QgsRetainTableFieldsAlgorithm *QgsRetainTableFieldsAlgorithm::createInstance() const
211{
212 return new QgsRetainTableFieldsAlgorithm();
213}
214
215void QgsRetainTableFieldsAlgorithm::initParameters( const QVariantMap & )
216{
217 addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELDS" ), QObject::tr( "Fields to retain" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, true ) );
218}
219
220QgsFields QgsRetainTableFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
221{
222 // loop through twice - first we need to build up a list of original attribute indices
223 for ( const QString &field : mFieldsToRetain )
224 {
225 const int index = inputFields.lookupField( field );
226 if ( index >= 0 )
227 mFieldIndices.append( index );
228 }
229
230 std::sort( mFieldIndices.begin(), mFieldIndices.end() );
231
232 // this second time we make a cleaned version of the fields
233 QgsFields outFields;
234 for ( const int index : std::as_const( mFieldIndices ) )
235 {
236 outFields.append( inputFields.at( index ) );
237 }
238 return outFields;
239}
240
241bool QgsRetainTableFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
242{
243 mFieldsToRetain = parameterAsStrings( parameters, QStringLiteral( "FIELDS" ), context );
244
245 if ( feedback )
246 {
247 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
248 if ( source )
249 {
250 for ( const QString &field : std::as_const( mFieldsToRetain ) )
251 {
252 const int index = source->fields().lookupField( field );
253 if ( index < 0 )
254 {
255 feedback->pushInfo( QObject::tr( "Field “%1” does not exist in input layer " ).arg( field ) );
256 }
257 }
258 }
259 }
260
261 return true;
262}
263
264QgsFeatureList QgsRetainTableFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
265{
266 QgsFeature f = feature;
267 const QgsAttributes inputAttributes = f.attributes();
268 QgsAttributes outputAttributes;
269 outputAttributes.reserve( mFieldIndices.count() );
270 for ( const int index : mFieldIndices )
271 {
272 outputAttributes.append( inputAttributes.at( index ) );
273 }
274 f.setAttributes( outputAttributes );
275 return QgsFeatureList() << f;
276}
277
278bool QgsRetainTableFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
279{
280 Q_UNUSED( layer )
281 return false;
282}
283
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3410
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3507
A vector of attributes.
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
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
void remove(int fieldIdx)
Removes the field with the given index.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Contains information about the context in which a processing algorithm is executed.
Qgis::ProcessingAlgorithmFlags flags() const override
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A vector layer or feature source field parameter for processing algorithms.
QList< QgsFeature > QgsFeatureList