QGIS API Documentation 3.43.0-Master (56aa1fd18d7)
qgsalgorithmaddincrementalfield.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmaddincrementalfield.cpp
3 -----------------------------------
4 begin : April 2017
5 copyright : (C) 2017 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#include "qgsfeaturerequest.h"
20
22
23QString QgsAddIncrementalFieldAlgorithm::name() const
24{
25 return QStringLiteral( "addautoincrementalfield" );
26}
27
28QString QgsAddIncrementalFieldAlgorithm::displayName() const
29{
30 return QObject::tr( "Add autoincremental field" );
31}
32
33QString QgsAddIncrementalFieldAlgorithm::shortHelpString() const
34{
35 return QObject::tr( "This algorithm adds a new integer field to a vector layer, with a sequential value for each feature.\n\n"
36 "This field can be used as a unique ID for features in the layer. The new attribute "
37 "is not added to the input layer but a new layer is generated instead.\n\n"
38 "The initial starting value for the incremental series can be specified.\n\n"
39 "Specifying an optional modulus value will restart the count to START whenever the field value reaches the modulus value.\n\n"
40 "Optionally, grouping fields can be specified. If group fields are present, then the field value will "
41 "be reset for each combination of these group field values.\n\n"
42 "The sort order for features may be specified, if so, then the incremental field will respect "
43 "this sort order." );
44}
45
46QStringList QgsAddIncrementalFieldAlgorithm::tags() const
47{
48 return QObject::tr( "add,create,serial,primary,key,unique,fields" ).split( ',' );
49}
50
51QString QgsAddIncrementalFieldAlgorithm::group() const
52{
53 return QObject::tr( "Vector table" );
54}
55
56QString QgsAddIncrementalFieldAlgorithm::groupId() const
57{
58 return QStringLiteral( "vectortable" );
59}
60
61QString QgsAddIncrementalFieldAlgorithm::outputName() const
62{
63 return QObject::tr( "Incremented" );
64}
65
66QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes() const
67{
68 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
69}
70
71QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance() const
72{
73 return new QgsAddIncrementalFieldAlgorithm();
74}
75
76Qgis::ProcessingFeatureSourceFlags QgsAddIncrementalFieldAlgorithm::sourceFlags() const
77{
79}
80
81void QgsAddIncrementalFieldAlgorithm::initParameters( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ), QStringLiteral( "AUTO" ) ) );
84 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "START" ), QObject::tr( "Start values at" ), Qgis::ProcessingNumberParameterType::Integer, 0, true ) );
85 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MODULUS" ), QObject::tr( "Modulus value" ), Qgis::ProcessingNumberParameterType::Integer, QVariant( 0 ), true ) );
86 addParameter( new QgsProcessingParameterField( QStringLiteral( "GROUP_FIELDS" ), QObject::tr( "Group values by" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, true, true ) );
87
88 // sort params
89 auto sortExp = std::make_unique<QgsProcessingParameterExpression>( QStringLiteral( "SORT_EXPRESSION" ), QObject::tr( "Sort expression" ), QVariant(), QStringLiteral( "INPUT" ), true );
90 sortExp->setFlags( sortExp->flags() | Qgis::ProcessingParameterFlag::Advanced );
91 addParameter( sortExp.release() );
92 auto sortAscending = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral( "SORT_ASCENDING" ), QObject::tr( "Sort ascending" ), true );
93 sortAscending->setFlags( sortAscending->flags() | Qgis::ProcessingParameterFlag::Advanced );
94 addParameter( sortAscending.release() );
95 auto sortNullsFirst = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral( "SORT_NULLS_FIRST" ), QObject::tr( "Sort nulls first" ), false );
96 sortNullsFirst->setFlags( sortNullsFirst->flags() | Qgis::ProcessingParameterFlag::Advanced );
97 addParameter( sortNullsFirst.release() );
98}
99
100QgsFields QgsAddIncrementalFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
101{
102 QgsFields outFields = inputFields;
103 outFields.append( QgsField( mFieldName, QMetaType::Type::LongLong ) );
104 mFields = outFields;
105 return outFields;
106}
107
108bool QgsAddIncrementalFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
109{
110 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
111 mStartValue = parameterAsInt( parameters, QStringLiteral( "START" ), context );
112 mValue = mStartValue;
113 mModulusValue = parameterAsInt( parameters, QStringLiteral( "MODULUS" ), context );
114 mFieldName = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
115 mGroupedFieldNames = parameterAsStrings( parameters, QStringLiteral( "GROUP_FIELDS" ), context );
116
117 mSortExpressionString = parameterAsExpression( parameters, QStringLiteral( "SORT_EXPRESSION" ), context );
118 mSortAscending = parameterAsBoolean( parameters, QStringLiteral( "SORT_ASCENDING" ), context );
119 mSortNullsFirst = parameterAsBoolean( parameters, QStringLiteral( "SORT_NULLS_FIRST" ), context );
120
121 if ( source->fields().lookupField( mFieldName ) >= 0 )
122 {
123 throw QgsProcessingException( QObject::tr( "A field with the same name (%1) already exists" ).arg( mFieldName ) );
124 }
125
126 return true;
127}
128
129QgsFeatureRequest QgsAddIncrementalFieldAlgorithm::request() const
130{
131 if ( mSortExpressionString.isEmpty() )
132 return QgsFeatureRequest();
133
134 return QgsFeatureRequest().setOrderBy( QgsFeatureRequest::OrderBy() << QgsFeatureRequest::OrderByClause( mSortExpressionString, mSortAscending, mSortNullsFirst ) );
135}
136
137QgsFeatureList QgsAddIncrementalFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
138{
139 if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
140 {
141 for ( const QString &field : std::as_const( mGroupedFieldNames ) )
142 {
143 int idx = mFields.lookupField( field );
144 if ( idx >= 0 )
145 mGroupedFields << idx;
146 }
147 }
148
149 QgsFeature f = feature;
150 QgsAttributes attributes = f.attributes();
151 if ( mGroupedFields.empty() )
152 {
153 attributes.append( mValue );
154 mValue++;
155 if ( mModulusValue != 0 && ( mValue % mModulusValue ) == 0 )
156 mValue = mStartValue;
157 }
158 else
159 {
160 QgsAttributes groupAttributes;
161 groupAttributes.reserve( mGroupedFields.size() );
162 for ( int index : std::as_const( mGroupedFields ) )
163 {
164 groupAttributes << f.attribute( index );
165 }
166 long long value = mGroupedValues.value( groupAttributes, mStartValue );
167 attributes.append( value );
168 value++;
169 if ( mModulusValue != 0 && ( value % mModulusValue ) == 0 )
170 value = mStartValue;
171 mGroupedValues[groupAttributes] = value;
172 }
173 f.setAttributes( attributes );
174 return QgsFeatureList() << f;
175}
176
177bool QgsAddIncrementalFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
178{
179 Q_UNUSED( layer )
180 return false;
181}
182
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
A vector of attributes.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
Represents a list of OrderByClauses, with the most important first and the least important last.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
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.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
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
Base class for all map layer types.
Definition qgsmaplayer.h:77
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
A numeric parameter for processing algorithms.
A string parameter for processing algorithms.
QList< QgsFeature > QgsFeatureList