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 const QStringList fieldTypes = QStringList( {QObject::tr(
"Float" ), QObject::tr(
"Integer" ), QObject::tr(
"String" ), QObject::tr(
"Date" ) } );
70 std::unique_ptr< QgsProcessingParameterString > fieldName = std::make_unique< QgsProcessingParameterString > ( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QVariant(),
false );
71 std::unique_ptr< QgsProcessingParameterEnum > fieldType = std::make_unique< QgsProcessingParameterEnum > ( QStringLiteral(
"FIELD_TYPE" ), QObject::tr(
"Result field type" ), fieldTypes,
false, 0 );
72 std::unique_ptr< QgsProcessingParameterNumber > fieldLength = std::make_unique< QgsProcessingParameterNumber > ( QStringLiteral(
"FIELD_LENGTH" ), QObject::tr(
"Result field length" ),
QgsProcessingParameterNumber::Integer, QVariant( 0 ),
false, 0 );
73 std::unique_ptr< QgsProcessingParameterNumber > fieldPrecision = std::make_unique< QgsProcessingParameterNumber > ( QStringLiteral(
"FIELD_PRECISION" ), QObject::tr(
"Result field precision" ),
QgsProcessingParameterNumber::Integer, QVariant( 0 ),
false, 0 );
74 std::unique_ptr< QgsProcessingParameterExpression > expression = std::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 const QVariant::Type fieldType = fieldTypes[fieldTypeIdx];
121 if ( fieldName.isEmpty() )
132 mFields = source->fields();
134 const int fieldIdx = mFields.indexFromName(
field.
name() );
138 mFields.append(
field );
142 feedback->
pushWarning( QObject::tr(
"Field name %1 already exists and will be replaced" ).arg(
field.
name() ) );
147 mFieldIdx = mFields.lookupField(
field.
name() );
150 const QString expressionString = parameterAsString( parameters, QStringLiteral(
"FORMULA" ), context );
151 mExpressionContext = createExpressionContext( parameters, context, source.get() );
156 mExpression.setGeomCalculator( &mDa );
158 mExpression.setAreaUnits( context.
areaUnit() );
160 if ( mExpression.hasParserError() )
162 .arg( expressionString, mExpression.parserErrorString() ) );
164 mExpression.prepare( &mExpressionContext );
172 const QStringList fieldNames = mFields.names();
173 for (
const QString &fieldName : fieldNames )
177 if ( attributeIndex >= 0 )
178 attributes[attributeIndex] = feature.
attribute( fieldName );
181 if ( mExpression.isValid() )
183 mExpressionContext.setFeature( feature );
184 mExpressionContext.lastScope()->setVariable( QStringLiteral(
"row_number" ), mRowNumber );
186 const QVariant value = mExpression.evaluate( &mExpressionContext );
188 if ( mExpression.hasEvalError() )
191 .arg( mExpression.expression(), mExpression.evalErrorString() ) );
194 attributes[mFieldIdx] = value;
198 attributes[mFieldIdx] = QVariant();
207 bool QgsFieldCalculatorAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
Class for parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
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.
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsUnitTypes::AreaUnit areaUnit() const
Returns the area unit to use for area calculations.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsUnitTypes::DistanceUnit distanceUnit() const
Returns the distance unit to use for distance calculations.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
Custom exception class for processing related exceptions.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QList< QgsFeature > QgsFeatureList