Bug report #9906

processing - python advanced field calculator fails with $ character

Added by Regis Haubourg over 6 years ago. Updated over 5 years ago.

Status:Closed
Priority:Normal
Assignee:Victor Olaya
Category:Processing/Core
Affected QGIS version:2.2.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:18420

Description

Hi, when trying to calculate $area in python script, that character makes the script fail.
current workaround is to define a text variable and to modify runalg line to call the variable instead of $area text.
I didn't find any escape character to make that work

History

#1 Updated by Alexander Bruy over 6 years ago

  • Status changed from Open to Feedback

Python field calculator used different syntax. See http://gis-lab.info/qa/fieldpyculator.html and http://anitagraser.com/2013/04/20/advanced-python-field-calculator/. If you want use same syntax, as QGIS Field Calculator, use Field calculator algorithm

#2 Updated by Regis Haubourg over 6 years ago

In fact, I'm not using advanced python field calculator but simple field calculator

#3 Updated by Alexander Bruy over 6 years ago

Can't reproduce. I tested with two layers one in WGS84, another in projected CRS, in both cases $area expression works fine and new field with values created

#4 Updated by Regis Haubourg over 6 years ago

following script :

##calcul_surface=name
##vectorlayer_tablea=vector
##output_layer_alg0=output vector
outputs_0=Processing.runalg("qgis:fieldcalculator", vectorlayer_tablea, A_obj_sfce, 0, 20, 5, True, $area, output_layer_alg0)

raises following error:

Algorithm calcul_surface starting...
invalid syntax (, line 5) See log for more details

#5 Updated by Alexander Bruy over 6 years ago

You use wrong syntax for runalg call. Try to execute Field calculator from toolbox and look at algorithm commandline in Processing log.
Formula should be quoted, in your case

##calcul_surface=name
##vectorlayer_tablea=vector
##output_layer_alg0=output vector
outputs_0=Processing.runalg("qgis:fieldcalculator", vectorlayer_tablea, A_obj_sfce, 0, 20, 5, True, "$area", output_layer_alg0)

#6 Updated by Regis Haubourg over 6 years ago

Well,
I exported a model to a script, I did not type it by hand. I suppose that export process just forget double quotes around the formula. Thanks for your help. Anyone ready to debug the export process?

#7 Updated by Regis Haubourg over 6 years ago

  • Status changed from Feedback to Closed

Closing it since it is a very uncommon issue

#8 Updated by Giovanni Manghi over 5 years ago

  • Category changed from 94 to Processing/Core

Also available in: Atom PDF