Hi everyone,
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
When I give an expression as a multiline string, I get an error like this one:
Invalid map
Parse error
ERROR: parse error
ERROR: An error occurred while running r.mapcalc
with kind regards,
Panos
On Fri, Oct 30, 2015 at 8:14 AM, Panagiotis Mavrogiorgos <pmav99@gmail.com>
wrote:
Hi everyone,
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
Hi, yes, this should be possible. I think I actually used it some time ago
both the multiline and eval expressions. I think this is it:
# import
from grass.script.raster import mapcalc as rmapcalc
# prepare strings
mask_expression =
mask_template = '{target} = if({mask}, {source}, null())'
# create expressions
mask_expression.append(
mask_template.format(
source=speed, target=speed_masked, mask=buffered_mask))
mask_expression.append(
mask_template.format(
source=direction, target=direction_masked, mask=buffered_mask))
# concatenate list of strings and execute
rmapcalc('\n'.join(mask_expression))
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
You will need to include the actual code your are using.
Best,
Vaclav
with kind regards,
Panos
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user
Panagiotis Mavrogiorgos:
Hi everyone,
Hi Pano!
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
I think I'd use `eval`, see:
- <https://grass.osgeo.org/grass70/manuals/r.mapcalc.html#eval-function>
- <http://grass.osgeo.org/uploads/grass/history_docs/mapcalc-algebra.pdf>,
always worth studying (pages 14 and on)
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
What is the exact expression?
Nikos
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@gclements.plus.com>
On Mon, Nov 2, 2015 at 3:21 AM, Glynn Clements <glynn@gclements.plus.com> wrote:
Panagiotis Mavrogiorgos wrote:
Is it possible to perform multiple computations using
grass.script.raster.mapcalc()?
...
I have added Glynn's answer to
https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Example_for_embedding_r.mapcalc_.28map_algebra.29
Markus
PS: since it is a Wiki, feel free to further improve that or the other pages