25QString QgsFieldCalculatorAlgorithm::name()
const
27 return QStringLiteral(
"fieldcalculator" );
30QString QgsFieldCalculatorAlgorithm::displayName()
const
32 return QObject::tr(
"Field calculator" );
35QStringList QgsFieldCalculatorAlgorithm::tags()
const
37 return QObject::tr(
"field,calculator,vector" ).split(
',' );
40QString QgsFieldCalculatorAlgorithm::group()
const
42 return QObject::tr(
"Vector table" );
45QString QgsFieldCalculatorAlgorithm::groupId()
const
47 return QStringLiteral(
"vectortable" );
50QString QgsFieldCalculatorAlgorithm::outputName()
const
52 return QObject::tr(
"Calculated" );
55QList<int> QgsFieldCalculatorAlgorithm::inputLayerTypes()
const
65void QgsFieldCalculatorAlgorithm::initParameters(
const QVariantMap &configuration )
67 Q_UNUSED( configuration )
69 QStringList fieldTypes;
71 fieldTypes.reserve( 11 );
73 for (
const auto &type :
74 std::vector<std::pair<QMetaType::Type, QMetaType::Type>> {
75 { QMetaType::Type::Double, QMetaType::Type::UnknownType },
76 { QMetaType::Type::Int, QMetaType::Type::UnknownType },
77 { QMetaType::Type::QString, QMetaType::Type::UnknownType },
78 { QMetaType::Type::QDate, QMetaType::Type::UnknownType },
79 { QMetaType::Type::QTime, QMetaType::Type::UnknownType },
80 { QMetaType::Type::QDateTime, QMetaType::Type::UnknownType },
81 { QMetaType::Type::Bool, QMetaType::Type::UnknownType },
82 { QMetaType::Type::QByteArray, QMetaType::Type::UnknownType },
83 { QMetaType::Type::QStringList, QMetaType::Type::UnknownType },
84 { QMetaType::Type::QVariantList, QMetaType::Type::Int },
85 { QMetaType::Type::QVariantList, QMetaType::Type::Double }
92 std::unique_ptr<QgsProcessingParameterString> fieldName = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QVariant(),
false );
93 std::unique_ptr<QgsProcessingParameterEnum> fieldType = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"FIELD_TYPE" ), QObject::tr(
"Result field type" ), fieldTypes,
false, 0 );
94 fieldType->setMetadata(
95 { QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"icons" ), icons } } ) } } )
99 std::unique_ptr<QgsProcessingParameterNumber> fieldLength = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"FIELD_LENGTH" ), QObject::tr(
"Result field length" ),
Qgis::ProcessingNumberParameterType::Integer, QVariant( 0 ),
false, 0 );
100 std::unique_ptr<QgsProcessingParameterNumber> fieldPrecision = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"FIELD_PRECISION" ), QObject::tr(
"Result field precision" ),
Qgis::ProcessingNumberParameterType::Integer, QVariant( 0 ),
false, 0 );
101 std::unique_ptr<QgsProcessingParameterExpression> expression = std::make_unique<QgsProcessingParameterExpression>( QStringLiteral(
"FORMULA" ), QObject::tr(
"Formula" ), QVariant(), QStringLiteral(
"INPUT" ),
false );
103 expression->setMetadata( QVariantMap( { {
"inlineEditor",
true } } ) );
105 addParameter( fieldName.release() );
106 addParameter( fieldType.release() );
107 addParameter( fieldLength.release() );
108 addParameter( fieldPrecision.release() );
109 addParameter( expression.release() );
117QString QgsFieldCalculatorAlgorithm::shortHelpString()
const
119 return QObject::tr(
"This algorithm computes a new vector layer with the same features of the input layer, "
120 "but either overwriting an existing attribute or adding an additional attribute. The values of this field "
121 "are computed from each feature using an expression, based on the properties and attributes of the feature. "
122 "Note that if \"Field name\" is an existing field in the layer then all the rest of the field settings are ignored." );
125QgsFieldCalculatorAlgorithm *QgsFieldCalculatorAlgorithm::createInstance()
const
127 return new QgsFieldCalculatorAlgorithm();
133 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
139 const int fieldTypeIdx = parameterAsInt( parameters, QStringLiteral(
"FIELD_TYPE" ), context );
140 const int fieldLength = parameterAsInt( parameters, QStringLiteral(
"FIELD_LENGTH" ), context );
141 const int fieldPrecision = parameterAsInt( parameters, QStringLiteral(
"FIELD_PRECISION" ), context );
142 const QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
144 QMetaType::Type fieldType = QMetaType::Type::QString;
145 QMetaType::Type fieldSubType = QMetaType::Type::UnknownType;
146 switch ( fieldTypeIdx )
149 fieldType = QMetaType::Type::Double;
152 fieldType = QMetaType::Type::Int;
155 fieldType = QMetaType::Type::QString;
158 fieldType = QMetaType::Type::QDate;
161 fieldType = QMetaType::Type::QTime;
164 fieldType = QMetaType::Type::QDateTime;
167 fieldType = QMetaType::Type::Bool;
170 fieldType = QMetaType::Type::QByteArray;
173 fieldType = QMetaType::Type::QStringList;
174 fieldSubType = QMetaType::Type::QString;
177 fieldType = QMetaType::Type::QVariantList;
178 fieldSubType = QMetaType::Type::Int;
181 fieldType = QMetaType::Type::QVariantList;
182 fieldSubType = QMetaType::Type::Double;
186 if ( fieldName.isEmpty() )
199 mFields = source->fields();
201 const int fieldIdx = mFields.indexFromName( field.name() );
205 mFields.append( field );
209 feedback->
pushWarning( QObject::tr(
"Field name %1 already exists and will be replaced" ).arg( field.name() ) );
212 mFieldIdx = mFields.lookupField( field.name() );
215 const QString expressionString = parameterAsString( parameters, QStringLiteral(
"FORMULA" ), context );
216 mExpressionContext = createExpressionContext( parameters, context, source.get() );
221 mExpression.setGeomCalculator( &mDa );
223 mExpression.setAreaUnits( context.
areaUnit() );
225 if ( mExpression.hasParserError() )
227 .arg( expressionString, mExpression.parserErrorString() ) );
229 mExpression.prepare( &mExpressionContext );
237 const QStringList fieldNames = mFields.names();
238 for (
const QString &fieldName : fieldNames )
242 if ( attributeIndex >= 0 )
243 attributes[attributeIndex] = feature.
attribute( fieldName );
246 if ( mExpression.isValid() )
248 mExpressionContext.setFeature( feature );
249 mExpressionContext.lastScope()->setVariable( QStringLiteral(
"row_number" ), mRowNumber );
251 const QVariant value = mExpression.evaluate( &mExpressionContext );
253 if ( mExpression.hasEvalError() )
256 .arg( mExpression.expression(), mExpression.evalErrorString() ) );
259 attributes[mFieldIdx] = value;
263 attributes[mFieldIdx] = QVariant();
272bool QgsFieldCalculatorAlgorithm::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...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
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.
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.
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Qgis::AreaUnit areaUnit() const
Returns the area unit to use for area calculations.
Qgis::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.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
QList< QgsFeature > QgsFeatureList