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

Vaclav Petras wenzeslaus at gmail.com
Fri Oct 30 07:13:22 PDT 2015


On Fri, Oct 30, 2015 at 8:14 AM, Panagiotis Mavrogiorgos <pmav99 at 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 at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20151030/c860d60d/attachment.html>


More information about the grass-user mailing list