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?

Taras
  • 32,823
  • 4
  • 66
  • 137
rickD
  • 577
  • 3
  • 13

1 Answers1

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)
Joseph
  • 75,746
  • 7
  • 171
  • 282