I'm trying to use the 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?


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.


  • 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)
  • this is a working answer, strange, but it seems you can't overwrite the existing shapefile with the new field, you have to create another shapefile all together, is there a way around this with naming conventions or will I just need to blow away the old files used to get to where I want? – rickD Apr 2 '16 at 0:33
  • @rickdebbout - Like most processing tools, you cannot overwrite the input with the output as the input is being read by the tool. If you're going to do this in a script, then you may need to output the result as a temp layer and then use some Python commands to overwrite the original input layer with this. Personally, I would use some PyQGIS code to add a field and populate the attributes directly to the shapefile. This post is useful but you can ask another question if you still need help. – Joseph Apr 4 '16 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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