[GRASS-user] mapcalc/mapcalculator in python script

Hello,

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....

hopefully you can help me

thanks
Johannes
--
GMX DSL Doppel-Flat ab 19,99 Euro/mtl.! Jetzt mit
gratis Handy-Flat! http://portal.gmx.net/de/go/dsl

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/01/2011 03:28 PM, Johannes Radinger wrote:

Hello,

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....

I don't know anything about python, but I had a similar problem under R,
where the problem was escaping of the formula - the formula needed to be
enclosed in " in grass.
To diagnose: can you see the command executed under GRASS? Does it work
if you paste this into GRASS?

Cheers,

Rainer

hopefully you can help me

thanks
Johannes

- --
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel: +33 - (0)9 53 10 27 44
Cell: +27 - (0)8 39 47 90 42
Fax (SA): +27 - (0)8 65 16 27 82
Fax (D) : +49 - (0)3 21 21 25 22 44
Fax (FR): +33 - (0)9 58 10 27 44
email: Rainer@krugs.de

Skype: RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1IJqkACgkQoYgNqgF2egqR5gCdGBIrDkBUot4R7qZcSrcSOf/Q
vQcAn0r8YNJeNKZlpYklE4GtM+KYDoyd
=QJkU
-----END PGP SIGNATURE-----

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
6.x).

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@gclements.plus.com>