3

I'm trying to use the Advanced Python field calculator in a script like so:

processing.runalg('qgis:advancedpythonfieldcalculator', output_layer, 'Latitude', 1, 12, 5, '', '$y', output_layer2)

Not sure exactly how to put the arguments

ALGORITHM: Advanced Python field calculator
INPUT_LAYER <ParameterVector>
FIELD_NAME <ParameterString>
FIELD_TYPE <ParameterSelection>
FIELD_LENGTH <ParameterNumber>
FIELD_PRECISION <ParameterNumber>
GLOBAL <ParameterString>
FORMULA <ParameterString>
OUTPUT_LAYER <OutputVector>

Particularly, the GLOBAL and FORMULA args. Docs don't show much on this.. I'm getting a return of an empty file (i.e. no points), but the attribute table has the new field name in it. Any ideas?

0

1 Answer 1

3

When using the Advanced Python Field Calculator, you need to explicity define the QgsGeometry functions, which in the normal Field Calculator is usually done for you.

E.g.

  • Field Calculator: $y
  • Advanced Python Field Calculator: $geom.centroid().asPoint().y()

So you can replace the $y expression in your algorithm with the one mentioned. Also, you need to add value = in the Formula parameter. Therefore, your algorithm should look like:

processing.runalg('qgis:advancedpythonfieldcalculator', output_layer, 'Latitude', 1, 12, 5, "", "value = $geom.centroid().asPoint().y()", output_layer2)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.