24QString QgsAddIncrementalFieldAlgorithm::name()
const
26 return QStringLiteral(
"addautoincrementalfield" );
29QString QgsAddIncrementalFieldAlgorithm::displayName()
const
31 return QObject::tr(
"Add autoincremental field" );
34QString QgsAddIncrementalFieldAlgorithm::shortHelpString()
const
36 return QObject::tr(
"This algorithm adds a new integer field to a vector layer, with a sequential value for each feature.\n\n"
37 "This field can be used as a unique ID for features in the layer. The new attribute "
38 "is not added to the input layer but a new layer is generated instead.\n\n"
39 "The initial starting value for the incremental series can be specified.\n\n"
40 "Specifying an optional modulus value will restart the count to START whenever the field value reaches the modulus value.\n\n"
41 "Optionally, grouping fields can be specified. If group fields are present, then the field value will "
42 "be reset for each combination of these group field values.\n\n"
43 "The sort order for features may be specified, if so, then the incremental field will respect "
47QString QgsAddIncrementalFieldAlgorithm::shortDescription()
const
49 return QObject::tr(
"Adds a new integer field to a vector layer, with a sequential value for each feature, "
50 "usable as a unique ID for features in the layer." );
53QStringList QgsAddIncrementalFieldAlgorithm::tags()
const
55 return QObject::tr(
"add,create,serial,primary,key,unique,fields" ).split(
',' );
58QString QgsAddIncrementalFieldAlgorithm::group()
const
60 return QObject::tr(
"Vector table" );
63QString QgsAddIncrementalFieldAlgorithm::groupId()
const
65 return QStringLiteral(
"vectortable" );
68QString QgsAddIncrementalFieldAlgorithm::outputName()
const
70 return QObject::tr(
"Incremented" );
73QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes()
const
78QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance()
const
80 return new QgsAddIncrementalFieldAlgorithm();
88void QgsAddIncrementalFieldAlgorithm::initParameters(
const QVariantMap & )
90 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QStringLiteral(
"AUTO" ) ) );
96 auto sortExp = std::make_unique<QgsProcessingParameterExpression>( QStringLiteral(
"SORT_EXPRESSION" ), QObject::tr(
"Sort expression" ), QVariant(), QStringLiteral(
"INPUT" ),
true );
98 addParameter( sortExp.release() );
99 auto sortAscending = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"SORT_ASCENDING" ), QObject::tr(
"Sort ascending" ),
true );
101 addParameter( sortAscending.release() );
102 auto sortNullsFirst = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"SORT_NULLS_FIRST" ), QObject::tr(
"Sort nulls first" ),
false );
104 addParameter( sortNullsFirst.release() );
107QgsFields QgsAddIncrementalFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
110 outFields.
append(
QgsField( mFieldName, QMetaType::Type::LongLong ) );
117 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
118 mStartValue = parameterAsInt( parameters, QStringLiteral(
"START" ), context );
119 mValue = mStartValue;
120 mModulusValue = parameterAsInt( parameters, QStringLiteral(
"MODULUS" ), context );
121 mFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
122 mGroupedFieldNames = parameterAsStrings( parameters, QStringLiteral(
"GROUP_FIELDS" ), context );
124 mSortExpressionString = parameterAsExpression( parameters, QStringLiteral(
"SORT_EXPRESSION" ), context );
125 mSortAscending = parameterAsBoolean( parameters, QStringLiteral(
"SORT_ASCENDING" ), context );
126 mSortNullsFirst = parameterAsBoolean( parameters, QStringLiteral(
"SORT_NULLS_FIRST" ), context );
128 if ( source->fields().lookupField( mFieldName ) >= 0 )
130 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( mFieldName ) );
138 if ( mSortExpressionString.isEmpty() )
146 if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
148 for (
const QString &field : std::as_const( mGroupedFieldNames ) )
150 int idx = mFields.lookupField( field );
152 mGroupedFields << idx;
158 if ( mGroupedFields.empty() )
160 attributes.append( mValue );
162 if ( mModulusValue != 0 && ( mValue % mModulusValue ) == 0 )
163 mValue = mStartValue;
168 groupAttributes.reserve( mGroupedFields.size() );
169 for (
int index : std::as_const( mGroupedFields ) )
173 long long value = mGroupedValues.value( groupAttributes, mStartValue );
174 attributes.append( value );
176 if ( mModulusValue != 0 && ( value % mModulusValue ) == 0 )
178 mGroupedValues[groupAttributes] = value;
184bool 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