QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
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 QGS_MARK_ALGORITHM_SOURCE
139
140 QgsFeature f = feature;
141 QgsAttributes attributes = f.attributes();
142 for ( const int index : mFieldIndices )
143 {
144 attributes.remove( index );
145 }
146 f.setAttributes( attributes );
147 return QgsFeatureList() << f;
148}
149
150bool QgsDropTableFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
151{
152 Q_UNUSED( layer )
153 return false;
154}
155
156
157//
158// QgsRetainTableFieldsAlgorithm
159//
160
161Qgis::ProcessingAlgorithmFlags QgsRetainTableFieldsAlgorithm::flags() const
162{
164}
165
166QString QgsRetainTableFieldsAlgorithm::name() const
167{
168 return u"retainfields"_s;
169}
170
171QString QgsRetainTableFieldsAlgorithm::displayName() const
172{
173 return QObject::tr( "Retain fields" );
174}
175
176QString QgsRetainTableFieldsAlgorithm::shortHelpString() const
177{
178 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." );
179}
180
181QString QgsRetainTableFieldsAlgorithm::shortDescription() const
182{
183 return QObject::tr( "Retains selected fields from a vector layer." );
184}
185
186QStringList QgsRetainTableFieldsAlgorithm::tags() const
187{
188 return QObject::tr( "drop,delete,remove,retain,keep,other,fields,columns,attributes" ).split( ',' );
189}
190
191QString QgsRetainTableFieldsAlgorithm::group() const
192{
193 return QObject::tr( "Vector table" );
194}
195
196QString QgsRetainTableFieldsAlgorithm::groupId() const
197{
198 return u"vectortable"_s;
199}
200
201QString QgsRetainTableFieldsAlgorithm::outputName() const
202{
203 return QObject::tr( "Retained fields" );
204}
205
206QList<int> QgsRetainTableFieldsAlgorithm::inputLayerTypes() const
207{
208 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
209}
210
211Qgis::ProcessingFeatureSourceFlags QgsRetainTableFieldsAlgorithm::sourceFlags() const
212{
214}
215
216QgsRetainTableFieldsAlgorithm *QgsRetainTableFieldsAlgorithm::createInstance() const
217{
218 return new QgsRetainTableFieldsAlgorithm();
219}
220
221void QgsRetainTableFieldsAlgorithm::initParameters( const QVariantMap & )
222{
223 addParameter( new QgsProcessingParameterField( u"FIELDS"_s, QObject::tr( "Fields to retain" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, true ) );
224}
225
226QgsFields QgsRetainTableFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
227{
228 // loop through twice - first we need to build up a list of original attribute indices
229 for ( const QString &field : mFieldsToRetain )
230 {
231 const int index = inputFields.lookupField( field );
232 if ( index >= 0 )
233 mFieldIndices.append( index );
234 }
235
236 std::sort( mFieldIndices.begin(), mFieldIndices.end() );
237
238 // this second time we make a cleaned version of the fields
239 QgsFields outFields;
240 for ( const int index : std::as_const( mFieldIndices ) )
241 {
242 outFields.append( inputFields.at( index ) );
243 }
244 return outFields;
245}
246
247bool QgsRetainTableFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
248{
249 mFieldsToRetain = parameterAsStrings( parameters, u"FIELDS"_s, context );
250
251 if ( feedback )
252 {
253 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
254 if ( source )
255 {
256 for ( const QString &field : std::as_const( mFieldsToRetain ) )
257 {
258 const int index = source->fields().lookupField( field );
259 if ( index < 0 )
260 {
261 feedback->pushInfo( QObject::tr( "Field “%1” does not exist in input layer " ).arg( field ) );
262 }
263 }
264 }
265 }
266
267 return true;
268}
269
270QgsFeatureList QgsRetainTableFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
271{
272 QGS_MARK_ALGORITHM_SOURCE
273
274 QgsFeature f = feature;
275 const QgsAttributes inputAttributes = f.attributes();
276 QgsAttributes outputAttributes;
277 outputAttributes.reserve( mFieldIndices.count() );
278 for ( const int index : mFieldIndices )
279 {
280 outputAttributes.append( inputAttributes.at( index ) );
281 }
282 f.setAttributes( outputAttributes );
283 return QgsFeatureList() << f;
284}
285
286bool QgsRetainTableFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
287{
288 Q_UNUSED( layer )
289 return false;
290}
291
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3755
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3826
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3930
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
Definition qgis.h:3807
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3941
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
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
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