QGIS API Documentation 3.99.0-Master (357b655ed83)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26Qgis::ProcessingAlgorithmFlags QgsDropTableFieldsAlgorithm::flags() const
27{
29}
30
31QString QgsDropTableFieldsAlgorithm::name() const
32{
33 return u"deletecolumn"_s;
34}
35
36QString QgsDropTableFieldsAlgorithm::displayName() const
37{
38 return QObject::tr( "Drop field(s)" );
39}
40
41QString QgsDropTableFieldsAlgorithm::shortHelpString() const
42{
43 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." );
44}
45
46QString QgsDropTableFieldsAlgorithm::shortDescription() const
47{
48 return QObject::tr( "Deletes fields from a vector layer." );
49}
50
51QStringList QgsDropTableFieldsAlgorithm::tags() const
52{
53 return QObject::tr( "drop,delete,remove,fields,columns,attributes" ).split( ',' );
54}
55
56QString QgsDropTableFieldsAlgorithm::group() const
57{
58 return QObject::tr( "Vector table" );
59}
60
61QString QgsDropTableFieldsAlgorithm::groupId() const
62{
63 return u"vectortable"_s;
64}
65
66QString QgsDropTableFieldsAlgorithm::outputName() const
67{
68 return QObject::tr( "Remaining fields" );
69}
70
71QList<int> QgsDropTableFieldsAlgorithm::inputLayerTypes() const
72{
73 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
74}
75
76Qgis::ProcessingFeatureSourceFlags QgsDropTableFieldsAlgorithm::sourceFlags() const
77{
79}
80
81QgsDropTableFieldsAlgorithm *QgsDropTableFieldsAlgorithm::createInstance() const
82{
83 return new QgsDropTableFieldsAlgorithm();
84}
85
86void QgsDropTableFieldsAlgorithm::initParameters( const QVariantMap & )
87{
88 addParameter( new QgsProcessingParameterField( u"COLUMN"_s, QObject::tr( "Fields to drop" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, true ) );
89}
90
91QgsFields QgsDropTableFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
92{
93 QgsFields outFields = inputFields;
94 // loop through twice - first we need to build up a list of original attribute indices
95 for ( const QString &field : mFieldsToDelete )
96 {
97 const int index = inputFields.lookupField( field );
98 if ( index >= 0 )
99 mFieldIndices.append( index );
100 }
101
102 // important - make sure we remove from the end so we aren't changing used indices as we go
103 std::sort( mFieldIndices.begin(), mFieldIndices.end(), std::greater<int>() );
104
105 // this second time we make a cleaned version of the fields
106 for ( const int index : std::as_const( mFieldIndices ) )
107 {
108 outFields.remove( index );
109 }
110 return outFields;
111}
112
113bool QgsDropTableFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
114{
115 mFieldsToDelete = parameterAsStrings( parameters, u"COLUMN"_s, context );
116
117 if ( feedback )
118 {
119 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
120 if ( source )
121 {
122 for ( const QString &field : std::as_const( mFieldsToDelete ) )
123 {
124 const int index = source->fields().lookupField( field );
125 if ( index < 0 )
126 {
127 feedback->pushInfo( QObject::tr( "Field “%1” does not exist in input layer " ).arg( field ) );
128 }
129 }
130 }
131 }
132
133 return true;
134}
135
136QgsFeatureList QgsDropTableFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
137{
138 QgsFeature f = feature;
139 QgsAttributes attributes = f.attributes();
140 for ( const int index : mFieldIndices )
141 {
142 attributes.remove( index );
143 }
144 f.setAttributes( attributes );
145 return QgsFeatureList() << f;
146}
147
148bool QgsDropTableFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
149{
150 Q_UNUSED( layer )
151 return false;
152}
153
154
155//
156// QgsRetainTableFieldsAlgorithm
157//
158
159Qgis::ProcessingAlgorithmFlags QgsRetainTableFieldsAlgorithm::flags() const
160{
162}
163
164QString QgsRetainTableFieldsAlgorithm::name() const
165{
166 return u"retainfields"_s;
167}
168
169QString QgsRetainTableFieldsAlgorithm::displayName() const
170{
171 return QObject::tr( "Retain fields" );
172}
173
174QString QgsRetainTableFieldsAlgorithm::shortHelpString() const
175{
176 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." );
177}
178
179QString QgsRetainTableFieldsAlgorithm::shortDescription() const
180{
181 return QObject::tr( "Retains selected fields from a vector layer." );
182}
183
184QStringList QgsRetainTableFieldsAlgorithm::tags() const
185{
186 return QObject::tr( "drop,delete,remove,retain,keep,other,fields,columns,attributes" ).split( ',' );
187}
188
189QString QgsRetainTableFieldsAlgorithm::group() const
190{
191 return QObject::tr( "Vector table" );
192}
193
194QString QgsRetainTableFieldsAlgorithm::groupId() const
195{
196 return u"vectortable"_s;
197}
198
199QString QgsRetainTableFieldsAlgorithm::outputName() const
200{
201 return QObject::tr( "Retained fields" );
202}
203
204QList<int> QgsRetainTableFieldsAlgorithm::inputLayerTypes() const
205{
206 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
207}
208
209Qgis::ProcessingFeatureSourceFlags QgsRetainTableFieldsAlgorithm::sourceFlags() const
210{
212}
213
214QgsRetainTableFieldsAlgorithm *QgsRetainTableFieldsAlgorithm::createInstance() const
215{
216 return new QgsRetainTableFieldsAlgorithm();
217}
218
219void QgsRetainTableFieldsAlgorithm::initParameters( const QVariantMap & )
220{
221 addParameter( new QgsProcessingParameterField( u"FIELDS"_s, QObject::tr( "Fields to retain" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, true ) );
222}
223
224QgsFields QgsRetainTableFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
225{
226 // loop through twice - first we need to build up a list of original attribute indices
227 for ( const QString &field : mFieldsToRetain )
228 {
229 const int index = inputFields.lookupField( field );
230 if ( index >= 0 )
231 mFieldIndices.append( index );
232 }
233
234 std::sort( mFieldIndices.begin(), mFieldIndices.end() );
235
236 // this second time we make a cleaned version of the fields
237 QgsFields outFields;
238 for ( const int index : std::as_const( mFieldIndices ) )
239 {
240 outFields.append( inputFields.at( index ) );
241 }
242 return outFields;
243}
244
245bool QgsRetainTableFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
246{
247 mFieldsToRetain = parameterAsStrings( parameters, u"FIELDS"_s, context );
248
249 if ( feedback )
250 {
251 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
252 if ( source )
253 {
254 for ( const QString &field : std::as_const( mFieldsToRetain ) )
255 {
256 const int index = source->fields().lookupField( field );
257 if ( index < 0 )
258 {
259 feedback->pushInfo( QObject::tr( "Field “%1” does not exist in input layer " ).arg( field ) );
260 }
261 }
262 }
263 }
264
265 return true;
266}
267
268QgsFeatureList QgsRetainTableFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
269{
270 QgsFeature f = feature;
271 const QgsAttributes inputAttributes = f.attributes();
272 QgsAttributes outputAttributes;
273 outputAttributes.reserve( mFieldIndices.count() );
274 for ( const int index : mFieldIndices )
275 {
276 outputAttributes.append( inputAttributes.at( index ) );
277 }
278 f.setAttributes( outputAttributes );
279 return QgsFeatureList() << f;
280}
281
282bool QgsRetainTableFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
283{
284 Q_UNUSED( layer )
285 return false;
286}
287
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3680
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
Definition qgis.h:3661
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
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:76
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:83
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