QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
23 QString QgsAddIncrementalFieldAlgorithm::name() const
24 {
25  return QStringLiteral( "addautoincrementalfield" );
26 }
27 
28 QString QgsAddIncrementalFieldAlgorithm::displayName() const
29 {
30  return QObject::tr( "Add autoincremental field" );
31 }
32 
33 QString 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  "Optionally, grouping fields can be specified. If group fields are present, then the field value will "
40  "be reset for each combination of these group field values.\n\n"
41  "The sort order for features may be specified, if so, then the incremental field will respect "
42  "this sort order." );
43 }
44 
45 QStringList QgsAddIncrementalFieldAlgorithm::tags() const
46 {
47  return QObject::tr( "add,create,serial,primary,key,unique,fields" ).split( ',' );
48 }
49 
50 QString QgsAddIncrementalFieldAlgorithm::group() const
51 {
52  return QObject::tr( "Vector table" );
53 }
54 
55 QString QgsAddIncrementalFieldAlgorithm::groupId() const
56 {
57  return QStringLiteral( "vectortable" );
58 }
59 
60 QString QgsAddIncrementalFieldAlgorithm::outputName() const
61 {
62  return QObject::tr( "Incremented" );
63 }
64 
65 QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes() const
66 {
67  return QList<int>() << QgsProcessing::TypeVector;
68 }
69 
70 QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance() const
71 {
72  return new QgsAddIncrementalFieldAlgorithm();
73 }
74 
75 QgsProcessingFeatureSource::Flag QgsAddIncrementalFieldAlgorithm::sourceFlags() const
76 {
78 }
79 
80 void QgsAddIncrementalFieldAlgorithm::initParameters( const QVariantMap & )
81 {
82  addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ), QStringLiteral( "AUTO" ) ) );
83  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "START" ), QObject::tr( "Start values at" ),
85  addParameter( new QgsProcessingParameterField( QStringLiteral( "GROUP_FIELDS" ), QObject::tr( "Group values by" ), QVariant(),
86  QStringLiteral( "INPUT" ), QgsProcessingParameterField::Any, true, true ) );
87 
88  // sort params
89  std::unique_ptr< QgsProcessingParameterExpression > sortExp = qgis::make_unique< QgsProcessingParameterExpression >( QStringLiteral( "SORT_EXPRESSION" ), QObject::tr( "Sort expression" ), QVariant(), QStringLiteral( "INPUT" ), true );
90  sortExp->setFlags( sortExp->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
91  addParameter( sortExp.release() );
92  std::unique_ptr< QgsProcessingParameterBoolean > sortAscending = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "SORT_ASCENDING" ), QObject::tr( "Sort ascending" ), true );
93  sortAscending->setFlags( sortAscending->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
94  addParameter( sortAscending.release() );
95  std::unique_ptr< QgsProcessingParameterBoolean > sortNullsFirst = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "SORT_NULLS_FIRST" ), QObject::tr( "Sort nulls first" ), false );
96  sortNullsFirst->setFlags( sortNullsFirst->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
97  addParameter( sortNullsFirst.release() );
98 }
99 
100 QgsFields QgsAddIncrementalFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
101 {
102  QgsFields outFields = inputFields;
103  outFields.append( QgsField( mFieldName, QVariant::LongLong ) );
104  mFields = outFields;
105  return outFields;
106 }
107 
108 bool QgsAddIncrementalFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
109 {
110  mStartValue = parameterAsInt( parameters, QStringLiteral( "START" ), context );
111  mValue = mStartValue;
112  mFieldName = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
113  mGroupedFieldNames = parameterAsFields( parameters, QStringLiteral( "GROUP_FIELDS" ), context );
114 
115  mSortExpressionString = parameterAsExpression( parameters, QStringLiteral( "SORT_EXPRESSION" ), context );
116  mSortAscending = parameterAsBoolean( parameters, QStringLiteral( "SORT_ASCENDING" ), context );
117  mSortNullsFirst = parameterAsBoolean( parameters, QStringLiteral( "SORT_NULLS_FIRST" ), context );
118 
119  return true;
120 }
121 
122 QgsFeatureRequest QgsAddIncrementalFieldAlgorithm::request() const
123 {
124  if ( mSortExpressionString.isEmpty() )
125  return QgsFeatureRequest();
126 
127  return QgsFeatureRequest().setOrderBy( QgsFeatureRequest::OrderBy() << QgsFeatureRequest::OrderByClause( mSortExpressionString, mSortAscending, mSortNullsFirst ) );
128 }
129 
130 QgsFeatureList QgsAddIncrementalFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
131 {
132  if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
133  {
134  for ( const QString &field : qgis::as_const( mGroupedFieldNames ) )
135  {
136  int idx = mFields.lookupField( field );
137  if ( idx >= 0 )
138  mGroupedFields << idx;
139  }
140  }
141 
142  QgsFeature f = feature;
143  QgsAttributes attributes = f.attributes();
144  if ( mGroupedFields.empty() )
145  {
146  attributes.append( mValue );
147  mValue++;
148  }
149  else
150  {
151  QgsAttributes groupAttributes;
152  groupAttributes.reserve( mGroupedFields.size() );
153  for ( int index : qgis::as_const( mGroupedFields ) )
154  {
155  groupAttributes << f.attribute( index );
156  }
157  long long value = mGroupedValues.value( groupAttributes, mStartValue );
158  attributes.append( value );
159  value++;
160  mGroupedValues[ groupAttributes ] = value;
161  }
162  f.setAttributes( attributes );
163  return QgsFeatureList() << f;
164 }
165 
166 bool QgsAddIncrementalFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
167 {
168  Q_UNUSED( layer )
169  return false;
170 }
171 
QgsFeatureRequest::OrderByClause
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
Definition: qgsfeaturerequest.h:130
qgsfeaturerequest.h
QgsProcessingParameterNumber
A numeric parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1960
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:38
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:423
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:477
field
const QgsField & field
Definition: qgsfield.h:456
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2324
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
qgsalgorithmaddincrementalfield.h
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:264
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:65
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1966
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:476
QgsFeatureRequest::OrderBy
Represents a list of OrderByClauses, with the most important first and the least important last.
Definition: qgsfeaturerequest.h:230
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsFeatureRequest::setOrderBy
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
Definition: qgsfeaturerequest.cpp:167
QgsAttributes
A vector of attributes.
Definition: qgsattributes.h:58
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:129
QgsProcessingParameterField::Any
@ Any
Accepts any field.
Definition: qgsprocessingparameters.h:2623
QgsProcessingParameterField
A vector layer or feature source field parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2617
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50