Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests and fix backwards compatibility
  • Loading branch information
suricactus committed Sep 11, 2020
1 parent cbb47c7 commit bd81e2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Expand Up @@ -915,6 +915,22 @@ tests:
name: expected/field_calculator_points.gml
type: vector

- algorithm: native:fieldcalculator
name: Test field calculator points (new API)
params:
FIELD_LENGTH: 10
FIELD_PRECISION: 3
FIELD_TYPE: 0
FORMULA: "\"id2\" *2"
INPUT:
name: points.gml
type: vector
NEW_FIELD_NAME: test
results:
OUTPUT:
name: expected/field_calculator_points.gml
type: vector

- algorithm: qgis:advancedpythonfieldcalculator
name: Test advanced python calculator
params:
Expand Down
8 changes: 5 additions & 3 deletions src/analysis/processing/qgsalgorithmfieldcalculator.cpp
Expand Up @@ -92,7 +92,7 @@ QgsFields QgsFieldCalculatorAlgorithm::outputFields( const QgsFields & ) const
QString QgsFieldCalculatorAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm computes a new vector layer with the same features of the input layer, "
"but either overwriting an existing attribute or adding additional attribute. The values of this "
"but either overwriting an existing attribute or adding an additional attribute. The values of this field"
"are computed from each feature using an expression, based on the properties and attributes of the feature."
"Note that selecting a value in \"Result in existing field\" will ignore all the rest of the "
"field settings." );
Expand All @@ -119,12 +119,14 @@ bool QgsFieldCalculatorAlgorithm::prepareAlgorithm( const QVariantMap &parameter
const int fieldPrecision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
const QString existingFieldName = parameterAsString( parameters, QStringLiteral( "EXISTING_FIELD_NAME" ), context );
const QString newFieldName = parameterAsString( parameters, QStringLiteral( "NEW_FIELD_NAME" ), context );
bool isNewField = parameterAsBool( parameters, QStringLiteral( "NEW_FIELD_NAME" ), context );
QString fieldName = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );

QVariant::Type fieldType = fieldTypes[fieldTypeIdx];

// this is to keep backwards compatibility, "NEW_FIELD" flags what how "FIELD_NAME" should be treated
// since they are not defined parameters, they should be accessed directly from `parameters`
bool isNewField = parameters.value( QStringLiteral( "NEW_FIELD" ) ).toBool();
QString fieldName = parameters.value( QStringLiteral( "FIELD_NAME" ) ).toString();

// In a perfect universe there would be only "EXISTING_FIELD_NAME" and "NEW_FIELD_NAME"
if ( !parameters.contains( QStringLiteral( "NEW_FIELD" ) ) )
{
Expand Down

0 comments on commit bd81e2f

Please sign in to comment.