[GRASS-user] script in python

Glynn Clements glynn at gclements.plus.com
Mon Dec 1 23:25:45 EST 2008


Gabriele Nolè wrote:

> I use ubuntu 8.10 and grass64 svn
> I made the changes based on your claims.
> This is my code: 
> -------------------------------------------------------------------------------------
> def main():
> 	# con = assegno le variabili
> 	vect1 = options['vect_1']
> 	vect2 = options['vect_2']
> 	vect3 = options['vect_3']	
> 
> 	grass.run_command('v.buffer', input = vect_1, output = buffer_200m, buffer = 200)
> 
> if __name__ == "__main__":
>             options, flags = grass.parser()
>             main()
> ---------------------------------------------------------------------------------------------
> But  if __name__ == "__main__": etc      ...... what is your job? 

__name__ contains the name of the current module. For a Python script
which is being executed (rather than "import"ed), the name is "__main__".

The check allows scripts to contain code which is only executed if the
script is being executed directly.

> Unfortunately, I made several attempts, but I have an error: 

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

> NameError: global name 'vect_1' is not defined

The above code assigns the variable "vect1":

> 	vect1 = options['vect_1']

but references "vect_1":

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

The two must match.

> I do not know how "to treat" the output of the v.buffer (in this case, buffer_200m).

As buffer_200m is a literal string, not a variable, it needs to be
quoted, e.g.:

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

> With grass 7 will work scripts in bash?

You can script GRASS commands in any language, but we intend to
convert all of the shell scripts which are included in GRASS to Python
for GRASS 7.

Apart from anything else, it's hard to write shell scripts which work
reliably on Windows. More generally, it's hard to write anything other
than simple scripts using bash (or any Bourne shell).

The intention is that it will be possible to run all of GRASS on
Windows without requiring a shell or Unix utilities.

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


More information about the grass-user mailing list