[GRASS-user] script in python

Glynn Clements glynn at gclements.plus.com
Sun Nov 30 14:07:55 EST 2008


Gabriele Nolè wrote:

> I'm trying to write a little script in python but I have difficulty (I know
> a bit the bash).
> The script must first run a buffer and then some overlay.	
> I am following the wiki and seeing some script as v.type.py, color_table.py
> etc. ..
> Now I am at this point:

>     print "Value of GIS_OPT_vect_1: %s" % os.getenv("GIS_OPT_vect_1")
>     print "Value of GIS_OPT_vect_2:  %s" % os.getenv("GIS_OPT_vect_2")
>     print "Value of GIS_OPT_vect_3:    %s" % os.getenv("GIS_OPT_vect_3")

> if __name__ == "__main__":
>     args = ""
>     for arg in sys.argv:
>         args += arg+" "
>     try:
>         if ( sys.argv[1] != "@ARGS_PARSED@" ):
>             os.system("g.parser %s " % (args))
>     except IndexError:
>         os.system("g.parser %s" % (args))
> 
>     if sys.argv[1] == "@ARGS_PARSED@":
>         main(); 

Look at the Python scripts in 7.0 for reference:

http://trac.osgeo.org/grass/browser/grass/trunk/scripts

Explicitly calling g.parser and reading GIS_OPT_* environment
variables is no longer necessary (or desirable; g.parser may
eventually be changed to use some other mechanism for passing data
back to the script). Instead:

	def main():
	    vect1 = options['vect_1']
	    vect2 = options['vect_2']
	    vect3 = options['vect_3']
	
	    ...
	
	if __name__ == "__main__":
	    options, flags = grass.parser()
	    main()

>     grass.run_command('v.buffer', input = vect_1 output = buffer_200m buffer = 200)

You need commas between arguments, i.e.:

    grass.run_command('v.buffer', input = vect_1, output = buffer_200m, buffer = 200)

>    # or grass.exec_command ?

Usually not; exec_command will effectively terminate the script. Use
run_command if you want control to return to the script once the
command completes.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list