24 QString QgsFieldCalculatorAlgorithm::name()
const
26 return QStringLiteral(
"fieldcalculator" );
29 QString QgsFieldCalculatorAlgorithm::displayName()
const
31 return QObject::tr(
"Field calculator" );
34 QStringList QgsFieldCalculatorAlgorithm::tags()
const
36 return QObject::tr(
"field,calculator,vector" ).split(
',' );
39 QString QgsFieldCalculatorAlgorithm::group()
const
41 return QObject::tr(
"Vector table" );
44 QString QgsFieldCalculatorAlgorithm::groupId()
const
46 return QStringLiteral(
"vectortable" );
49 QString QgsFieldCalculatorAlgorithm::outputName()
const
51 return QObject::tr(
"Calculated" );
54 QList<int> QgsFieldCalculatorAlgorithm::inputLayerTypes()
const
64 void QgsFieldCalculatorAlgorithm::initParameters(
const QVariantMap &configuration )
66 Q_UNUSED( configuration );
68 QStringList fieldTypes = QStringList( {QObject::tr(
"Float" ), QObject::tr(
"Integer" ), QObject::tr(
"String" ), QObject::tr(
"Date" ) } );
70 std::unique_ptr< QgsProcessingParameterString > fieldName = qgis::make_unique< QgsProcessingParameterString > ( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QVariant(),
false );
71 std::unique_ptr< QgsProcessingParameterEnum > fieldType = qgis::make_unique< QgsProcessingParameterEnum > ( QStringLiteral(
"FIELD_TYPE" ), QObject::tr(
"Result field type" ), fieldTypes,
false, 0 );
72 std::unique_ptr< QgsProcessingParameterNumber > fieldLength = qgis::make_unique< QgsProcessingParameterNumber > ( QStringLiteral(
"FIELD_LENGTH" ), QObject::tr(
"Result field length" ),
QgsProcessingParameterNumber::Integer, QVariant( 0 ),
false, 0 );
73 std::unique_ptr< QgsProcessingParameterNumber > fieldPrecision = qgis::make_unique< QgsProcessingParameterNumber > ( QStringLiteral(
"FIELD_PRECISION" ), QObject::tr(
"Result field precision" ),
QgsProcessingParameterNumber::Integer, QVariant( 0 ),
false, 0 );
74 std::unique_ptr< QgsProcessingParameterExpression > expression = qgis::make_unique< QgsProcessingParameterExpression> ( QStringLiteral(
"FORMULA" ), QObject::tr(
"Formula" ), QVariant(), QStringLiteral(
"INPUT" ),
false );
76 expression->setMetadata( QVariantMap( {{
"inlineEditor",
true}} ) );
78 addParameter( fieldName.release() );
79 addParameter( fieldType.release() );
80 addParameter( fieldLength.release() );
81 addParameter( fieldPrecision.release() );
82 addParameter( expression.release() );
90 QString QgsFieldCalculatorAlgorithm::shortHelpString()
const
92 return QObject::tr(
"This algorithm computes a new vector layer with the same features of the input layer, "
93 "but either overwriting an existing attribute or adding an additional attribute. The values of this field "
94 "are computed from each feature using an expression, based on the properties and attributes of the feature. "
95 "Note that if \"Field name\" is an existing field in the layer then all the rest of the field settings are ignored." );
98 QgsFieldCalculatorAlgorithm *QgsFieldCalculatorAlgorithm::createInstance()
const
100 return new QgsFieldCalculatorAlgorithm();
106 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
111 QList<QVariant::Type> fieldTypes( {QVariant::Double, QVariant::Int, QVariant::String, QVariant::Date} );
114 const int fieldTypeIdx = parameterAsInt( parameters, QStringLiteral(
"FIELD_TYPE" ), context );
115 const int fieldLength = parameterAsInt( parameters, QStringLiteral(
"FIELD_LENGTH" ), context );
116 const int fieldPrecision = parameterAsInt( parameters, QStringLiteral(
"FIELD_PRECISION" ), context );
117 const QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
119 QVariant::Type fieldType = fieldTypes[fieldTypeIdx];
121 if ( fieldName.isEmpty() )
132 mFields = source->fields();
134 int fieldIdx = mFields.lookupField(
field.
name() );
137 mFields.append(
field );
141 mFieldIdx = mFields.lookupField(
field.
name() );
144 QString expressionString = parameterAsString( parameters, QStringLiteral(
"FORMULA" ), context );
145 mExpressionContext = createExpressionContext( parameters, context, source.get() );
150 mExpression.setGeomCalculator( &mDa );
152 mExpression.setAreaUnits( context.
areaUnit() );
154 if ( mExpression.hasParserError() )
156 .arg( expressionString, mExpression.parserErrorString() ) );
158 mExpression.prepare( &mExpressionContext );
166 const QStringList fieldNames = mFields.names();
167 for (
const QString &fieldName : fieldNames )
171 if ( attributeIndex >= 0 )
172 attributes[attributeIndex] = feature.
attribute( fieldName );
175 if ( mExpression.isValid() )
177 mExpressionContext.setFeature( feature );
178 mExpressionContext.lastScope()->setVariable( QStringLiteral(
"row_number" ), mRowNumber );
180 const QVariant value = mExpression.evaluate( &mExpressionContext );
182 if ( mExpression.hasEvalError() )
185 .arg( mExpression.expression(), mExpression.evalErrorString() ) );
188 attributes[mFieldIdx] = value;
192 attributes[mFieldIdx] = QVariant();
201 bool QgsFieldCalculatorAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const