[GRASS-user] Re: Trying python scripts.

Marcello Gorini gorini at gmail.com
Thu May 27 12:27:52 EDT 2010


Oops, I guess I quoted the text in the wrong way. Sorry for that.


Marcello Gorini wrote:
> 
> Hello Nikos,
> 
> Marcello Gorini.
> 
> Nikos Alexandris wrote:
>> 
>> 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
>> 
>> 
>> No, it is not there. To make sure I set the region to the input map:
>> 
>> g.region rast=map1 -pa
>> d.mon x0 && d.rast map1
>> 
>> OK, it is there. Then I ran the script: 
>> 
>> ./myscript.py output=outmap raster1=map1 raster2=map2 
>> 
>> After processing, I tried:
>> 
>> d.rast outmap
>> 
>> But then:
>> 
>> ERROR: Raster map <outmap> not found
>> 
>> What could be wrong?
>> 
>> Best regards,
>> 
>> Marcello.
>> 
>> P.S.: By the way, is this the right way to quote answers?
>> 
>> _______________________________________________
>> grass-user mailing list
>> grass-user at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/grass-user
>> 
>> 
> 
> 

-- 
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Trying-python-scripts-tp5109105p5109444.html
Sent from the Grass - Users mailing list archive at Nabble.com.


More information about the grass-user mailing list