23QString QgsAddIncrementalFieldAlgorithm::name()
const
25 return QStringLiteral(
"addautoincrementalfield" );
28QString QgsAddIncrementalFieldAlgorithm::displayName()
const
30 return QObject::tr(
"Add autoincremental field" );
33QString QgsAddIncrementalFieldAlgorithm::shortHelpString()
const
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 "
46QString QgsAddIncrementalFieldAlgorithm::shortDescription()
const
48 return QObject::tr(
"Adds a new integer field to a vector layer, with a sequential value for each feature, "
49 "usable as a unique ID for features in the layer." );
52QStringList QgsAddIncrementalFieldAlgorithm::tags()
const
54 return QObject::tr(
"add,create,serial,primary,key,unique,fields" ).split(
',' );
57QString QgsAddIncrementalFieldAlgorithm::group()
const
59 return QObject::tr(
"Vector table" );
62QString QgsAddIncrementalFieldAlgorithm::groupId()
const
64 return QStringLiteral(
"vectortable" );
67QString QgsAddIncrementalFieldAlgorithm::outputName()
const
69 return QObject::tr(
"Incremented" );
72QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes()
const
77QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance()
const
79 return new QgsAddIncrementalFieldAlgorithm();
87void QgsAddIncrementalFieldAlgorithm::initParameters(
const QVariantMap & )
89 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QStringLiteral(
"AUTO" ) ) );
95 auto sortExp = std::make_unique<QgsProcessingParameterExpression>( QStringLiteral(
"SORT_EXPRESSION" ), QObject::tr(
"Sort expression" ), QVariant(), QStringLiteral(
"INPUT" ),
true );
97 addParameter( sortExp.release() );
98 auto sortAscending = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"SORT_ASCENDING" ), QObject::tr(
"Sort ascending" ),
true );
100 addParameter( sortAscending.release() );
101 auto sortNullsFirst = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"SORT_NULLS_FIRST" ), QObject::tr(
"Sort nulls first" ),
false );
103 addParameter( sortNullsFirst.release() );
106QgsFields QgsAddIncrementalFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
109 outFields.
append(
QgsField( mFieldName, QMetaType::Type::LongLong ) );
116 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
117 mStartValue = parameterAsInt( parameters, QStringLiteral(
"START" ), context );
118 mValue = mStartValue;
119 mModulusValue = parameterAsInt( parameters, QStringLiteral(
"MODULUS" ), context );
120 mFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
121 mGroupedFieldNames = parameterAsStrings( parameters, QStringLiteral(
"GROUP_FIELDS" ), context );
123 mSortExpressionString = parameterAsExpression( parameters, QStringLiteral(
"SORT_EXPRESSION" ), context );
124 mSortAscending = parameterAsBoolean( parameters, QStringLiteral(
"SORT_ASCENDING" ), context );
125 mSortNullsFirst = parameterAsBoolean( parameters, QStringLiteral(
"SORT_NULLS_FIRST" ), context );
127 if ( source->fields().lookupField( mFieldName ) >= 0 )
129 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( mFieldName ) );
137 if ( mSortExpressionString.isEmpty() )
145 if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
147 for (
const QString &field : std::as_const( mGroupedFieldNames ) )
149 int idx = mFields.lookupField( field );
151 mGroupedFields << idx;
157 if ( mGroupedFields.empty() )
159 attributes.append( mValue );
161 if ( mModulusValue != 0 && ( mValue % mModulusValue ) == 0 )
162 mValue = mStartValue;
167 groupAttributes.reserve( mGroupedFields.size() );
168 for (
int index : std::as_const( mGroupedFields ) )
172 long long value = mGroupedValues.value( groupAttributes, mStartValue );
173 attributes.append( value );
175 if ( mModulusValue != 0 && ( value % mModulusValue ) == 0 )
177 mGroupedValues[groupAttributes] = value;
183bool QgsAddIncrementalFieldAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
@ 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.
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...
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.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Base class for all map layer types.
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