[GRASS-user] grass.script.raster.mapcalc and multiple computations

Glynn Clements glynn at gclements.plus.com
Sun Nov 1 18:21:33 PST 2015


Panagiotis Mavrogiorgos wrote:

> Is it possible to perform multiple computations using
> grass.script.raster.mapcalc()? What I want is to run something like this
> (example taken from r.mapcalc documentation):
> 
> r.mapcalc <<EOF
> > $GIS_OPT_OUTPUT.r = r#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 -
> > .$GIS_OPT_PERCENT) * r#$GIS_OPT_SECOND
> > $GIS_OPT_OUTPUT.g = g#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 -
> > .$GIS_OPT_PERCENT) * g#$GIS_OPT_SECOND
> > $GIS_OPT_OUTPUT.b = b#$GIS_OPT_FIRST * .$GIS_OPT_PERCENT + (1.0 -
> > .$GIS_OPT_PERCENT) * b#$GIS_OPT_SECOND
> > EOF

expr = ";".join([
	"$out.r = r#$first * $frac + (1.0 - $frac) * r#$second",
	"$out.g = g#$first * $frac + (1.0 - $frac) * g#$second",
	"$out.b = b#$first * $frac + (1.0 - $frac) * b#$second"])
grass.mapcalc(expr, out=out, first=first, second=second, frac=percent/100.0)

> When I give an expression as a multiline string, I get an error like this
> one:
> 
> Invalid map <i>
> > Parse error
> > ERROR: parse error
> > ERROR: An error occurred while running r.mapcalc

There shouldn't be any issues with multi-line strings per se, but you
can use a semicolon instead of a newline.

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


More information about the grass-user mailing list