[GRASS-user] Trying python scripts.

Nikos Alexandris nikos.alexandris at felis.uni-freiburg.de
Thu May 27 11:47:34 EDT 2010


Hi Marcello,

Marcello Gorini:
> This is kind of my first e-mail ever to ANY mailing list, so please forgive
> me if I don't express myself in the best way. I will improve eventually.
> I am a total beginner in python and GRASS (and any other programming
> language), so I am trying to run the first examples found in
> http://grass.osgeo.org/wiki/GRASS_and_Python.
> I managed to run the shaded_relief one OK, but the one with respect to
> mapcalc, I couldn't.
> I copied a script found in
> http://www.mail-archive.com/grass-dev@lists.osgeo.org/msg14669.html
> (transcribed below) and tried to run it:
> 
> #!/usr/bin/env python
> 
> # g.parser demo script for python programing
> 
> #%module
> #%  description: g.parser test script (python) TUN
> #%end
> #%flag
> #%  key: f
> #%  description: A flag
> #%end
> #%flag
> #%  key: n
> #%  description: Another flag
> #%end
> #%option
> #%  key: raster1
> #%  type: string
> #%  gisprompt: old,cell,raster
> #%  description: Raster input map
> #%  required : yes
> #%end
> #%option
> #%  key: raster2
> #%  type: string
> #%  gisprompt: old,cell,raster
> #%  description: Raster2 input map
> #%  required : yes
> #%end
> #%option
> #%  key: output
> #%  type: string
> #%  gisprompt: new,cell,raster
> #%  description: Output
> #%  required : yes
> #%end
> 
> import os
> import sys
> 
> import grass.script as grass
> 
> def main():
>     output = options['output']
>     raster1 = options['raster1']
>     raster2 = options['raster2']
>     grass.mapcalc("$out = $rast1 + $rast2", out = output, rast1 = raster1,
> rast2 = raster2)
>     if flags['f']:
>         print "Flag -f set"
>     else:
>         print "Flag -f not set"
> 
>     if flags['n']:
>         print "Flag -n foi definida mas nao devia"
>     else:
>         print "Flag -n not set- GOOD"
>     print options
>     print flags
>     return 0
> 
> if __name__ == "__main__":
>     options, flags = grass.parser()
>     sys.exit(main())
> 
> 
> It seems to run OK and “100%” is displayed in the GRASS prompt, but when I
> tried to display the map, I found out that it wasn't created at all.

If there are no errors and the script ran fine, maybe you just need to check 
the region settings?

> Just for you to know, to run the script I simply create it (say
> “myscript.py”), give permissions and run it in a GRASS session with the
> necessary options: > ./myscript.py output=outmap raster1=map1 raster2=map2
> I am using Ubuntu 9.04 64bits and GRASS 6.4.ORC5.

For example:

# check region extent, etc.
g.region -p

# set region extent and resolution to the map of your interest
g.region rast=outmap -pa

# give color
r.colors rast=outmap color=rainbow

# try to display
d.mon x0 && d.rast outmap

# is it there?

Regards, Nikos


More information about the grass-user mailing list