[GRASS-dev] Popen and r.mapcalc, pygrass' implications

Pietro peter.zamb at gmail.com
Tue Aug 12 08:35:53 PDT 2014


Dear devs,

Pygrass is not working properly with r.mapcalc, the problem is how the
Popen class is used internally, so playing with Popen class:

{{{
In [1]: import subprocess as sub

In [2]: cmd_list = ['r.mapcalc', 'expression="elev100=elevation*100"', '--o']

In [3]: cmd_shell = ' '.join(cmd_list)

In [4]: sub.Popen(cmd_list)
Out[4]: <subprocess.Popen at 0x7f1b46bf03d0>

syntax error, unexpected $end, expecting '='
Parse error
ERROR: parse error


In [5]: sub.Popen(cmd_shell, shell=True)
Out[5]: <subprocess.Popen at 0x7f1b46bf0c50>
100%

}}}

I saw that the function in grass.script.raster.mapcalc is using the
stdin to pass the expression to r.mapcalc, so instead of:

{{{
In [6]: from grass.pygrass.modules.shortcuts import raster as r

In [7]: r.mapcalc(expression="elev100=elevation*100", overwrite=True)
syntax error, unexpected $end, expecting '='
Parse error
ERROR: parse error

[...cut...]
CalledModuleError: Module run r.mapcalc r.mapcalc
expression='elev100=elevation*100' --o ended with error
Process ended with non-zero return code 1. See errors in the (error) output.
}}}

we can write:

{{{
In [8]: r.mapcalc(file='-', stdin_="elev100=elevation*100", overwrite=True)
 100%
Out[8]: Module('r.mapcalc')

}}}

but I think is less intuitive, do you see any other solutions?

Kind regards

Pietro


More information about the grass-dev mailing list