[GRASS-user] Using a r.mapcalc expression in grass.mapcalc

Glynn Clements glynn at gclements.plus.com
Thu May 19 12:26:08 EDT 2011


Jenny Turner wrote:

> I need to use this expression output=@input (input has labels and I want to
> export them so i need to do this). How can I do this in grass.mapcalc in
> Python? Does it accept @?

grass.mapcalc() is just a wrapper around r.mapcalc, so it supports
whatever r.mapcalc supports.

This:

	grass.mapcalc('output = @input')

will work, but you wouldn't normally hard-code map names in a script.

grass.mapcalc() uses Python's string.Template class to simplify using
variables in expressions. This:

	grass.mapcalc('$output = @$input', output = 'output', input = 'input')

is equivalent to the previous expression, but you can easily replace
the hard-coded names with variables, e.g.:

	grass.mapcalc('$output = @$input', output = output, input = input)

Passing parameters which aren't used in the string is harmless, so you
can use Python's "**" syntax to pass a dictionary of parameters, e.g. 
the "options" dictionary obtained from grass.parser():

	grass.mapcalc('$output = @$input', **options)

The values of the output= and input= options will be substituted into
the expression.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list