QGIS API Documentation  3.0.2-Girona (307d082)
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 
21 
22 QString QgsAddIncrementalFieldAlgorithm::name() const
23 {
24  return QStringLiteral( "addautoincrementalfield" );
25 }
26 
27 QString QgsAddIncrementalFieldAlgorithm::displayName() const
28 {
29  return QObject::tr( "Add autoincremental field" );
30 }
31 
32 QString QgsAddIncrementalFieldAlgorithm::shortHelpString() const
33 {
34  return QObject::tr( "This algorithm adds a new integer field to a vector layer, with a sequential value for each feature.\n\n"
35  "This field can be used as a unique ID for features in the layer. The new attribute "
36  "is not added to the input layer but a new layer is generated instead.\n\n"
37  "The initial starting value for the incremental series can be specified.\n\n"
38  "Optionally, grouping fields can be specified. If group fields are present, then the field value will "
39  "be reset for each combination of these group field values." );
40 }
41 
42 QStringList QgsAddIncrementalFieldAlgorithm::tags() const
43 {
44  return QObject::tr( "add,create,serial,primary,key,unique,fields" ).split( ',' );
45 }
46 
47 QString QgsAddIncrementalFieldAlgorithm::group() const
48 {
49  return QObject::tr( "Vector table" );
50 }
51 
52 QString QgsAddIncrementalFieldAlgorithm::groupId() const
53 {
54  return QStringLiteral( "vectortable" );
55 }
56 
57 QString QgsAddIncrementalFieldAlgorithm::outputName() const
58 {
59  return QObject::tr( "Incremented" );
60 }
61 
62 QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes() const
63 {
64  return QList<int>() << QgsProcessing::TypeVector;
65 }
66 
67 QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance() const
68 {
69  return new QgsAddIncrementalFieldAlgorithm();
70 }
71 
72 QgsProcessingFeatureSource::Flag QgsAddIncrementalFieldAlgorithm::sourceFlags() const
73 {
75 }
76 
77 void QgsAddIncrementalFieldAlgorithm::initParameters( const QVariantMap & )
78 {
79  addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ), QStringLiteral( "AUTO" ) ) );
80  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "START" ), QObject::tr( "Start values at" ),
82  addParameter( new QgsProcessingParameterField( QStringLiteral( "GROUP_FIELDS" ), QObject::tr( "Group values by" ), QVariant(),
83  QStringLiteral( "INPUT" ), QgsProcessingParameterField::Any, true, true ) );
84 }
85 
86 QgsFields QgsAddIncrementalFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
87 {
88  QgsFields outFields = inputFields;
89  outFields.append( QgsField( mFieldName, QVariant::LongLong ) );
90  mFields = outFields;
91  return outFields;
92 }
93 
94 bool QgsAddIncrementalFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
95 {
96  mStartValue = parameterAsInt( parameters, QStringLiteral( "START" ), context );
97  mValue = mStartValue;
98  mFieldName = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
99  mGroupedFieldNames = parameterAsFields( parameters, QStringLiteral( "GROUP_FIELDS" ), context );
100  return true;
101 }
102 
103 QgsFeatureList QgsAddIncrementalFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
104 {
105  if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
106  {
107  for ( const QString &field : qgis::as_const( mGroupedFieldNames ) )
108  {
109  int idx = mFields.lookupField( field );
110  if ( idx >= 0 )
111  mGroupedFields << idx;
112  }
113  }
114 
115  QgsFeature f = feature;
116  QgsAttributes attributes = f.attributes();
117  if ( mGroupedFields.empty() )
118  {
119  attributes.append( mValue );
120  mValue++;
121  }
122  else
123  {
124  QgsAttributes groupAttributes;
125  for ( int index : qgis::as_const( mGroupedFields ) )
126  {
127  groupAttributes << f.attribute( index );
128  }
129  long long value = mGroupedValues.value( groupAttributes, mStartValue );
130  attributes.append( value );
131  value++;
132  mGroupedValues[ groupAttributes ] = value;
133  }
134  f.setAttributes( attributes );
135  return QgsFeatureList() << f;
136 }
137 
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
A vector layer or feature source field parameter for processing algorithms.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
Container of fields for a vector layer.
Definition: qgsfields.h:42
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
A numeric parameter for processing algorithms.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
A vector of attributes.
Definition: qgsattributes.h:58
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:255
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.
QgsAttributes attributes
Definition: qgsfeature.h:72