[GRASS-dev] r.mapcal and python
Glynn Clements
glynn at gclements.plus.com
Mon Nov 10 11:04:30 EST 2008
Moritz Lennert wrote:
> > I'm a python newbie so have some patience :-)
> >
> > I'm trying to use r.mapcal with the grass.py function 'run_command' in
> > this way:
> >
> > e = "%s=%s-%s" % (out, dsm, dtm)
> > grass.run_command('r.mapcalc', expression = e)
> >
> > However, the new raster in "%out" doesn't appear, and a new raster
> > called 'expression' is created instead.
> >
> > I also tried with:
> >
> > grass.run_command('r.mapcalc', expression = "%s=%s-%s" % (out, dsm, dtm))
>
> The expression= parameter only exists in GRASS7, but not in grass6.x.
>
> So, r.mapcalc expression=k=1
>
> will give you
>
> in GRASS6.x: a map called 'expression' with the value 1
>
> in GRASS7: a map called 'k' with the value 1
>
> So everything depends on the version of GRASS you are running.
>
> > with the same result. And also with:
> >
> > grass.run_command('r.mapcalc', %s = "%s-%s" % (out, dsm, dtm))
>
> No expert, but shouldn't this be:
>
> grass.run_command('r.mapcalc', "%s = %s-%s" % (out, dsm, dtm))
>
> [note the changed location of the opening quote]
In terms of Python syntax, the latter is correct.
However, grass.run_command() is intended for modules which use
G_parser(). In particular, you can only pass arguments which have the
form "name=value".
You can fudge it for the 6.x version of r.mapcalc with:
grass.run_command('r.mapcalc', **{out: "%s-%s" % (dsm, dtm)})
But I would suggest either using subprocess.call():
subprocess.call(['r.mapcalc', '%s = %s-%s' % (out, dsm, dtm)])
or passing the expression via stdin using grass.write_command():
grass.write_command('r.mapcalc', stdin = '%s = %s-%s' % (out, dsm, dtm))
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-dev
mailing list