[GRASS-user] mapcalc/mapcalculator in python script

Glynn Clements glynn at gclements.plus.com
Wed Feb 2 05:06:17 EST 2011

Johannes Radinger wrote:

> I am running GRASS GIS 6.5 and want to run a python script
> including a mapcaluculator-expression of a simple formula:
> A=if(B,C)
> grass.mapcalc("$upstream_shreve = if(${upstream_part},${shreve})",
>                   upstream_shreve = options['upstream_shreve'],
>                   upstream_part = options['upstream_part'],
>                   shreve = options['shreve'])
> but that isn't working... I don't know whats excaclty the problem....

Are you sure you mean "if(B,C)" not e.g. "if(B,B,C)". The two argument
form returns the second argument if the first argument is nonzero and
zero if the the first argument is zero, i.e. "if(B,C,0)".

Other than that, the above should work; if it doesn't, that suggests a
bug somewhere (most of the Python code was written for 7.0 and
back-ported to 6.5; sometimes the code requires changes to work in

Also, if your variable names exactly match the keys in the options
dictionary, you can just pass the entire dictionary using Python's
"**" syntax, i.e.:

grass.mapcalc("$upstream_shreve = if(${upstream_part},${shreve})", **options)

Any extra options will be ignored.

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list