[GRASS-user] script in python
Gabriele Nolè
gis.gn at libero.it
Mon Dec 1 05:57:21 EST 2008
Hi Glynn.
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?
Unfortunately, I made several attempts, but I have an error:
Traceback (most recent call last):
File "/home/gab/scripts_grass/v.rischio_incendi.py", line
75, in <module>
main()
File "/home/gab/scripts_grass/v.rischio_incendi.py", line
70, in main
grass.run_command('v.buffer', input = vect_1, output =
buffer_200m, buffer = 200)
NameError: global name 'vect_1' is not defined
I do not know how "to treat" the output of the v.buffer (in this case,
buffer_200m).
Thanks Glynn
Gabriele
PS
With grass 7 will work scripts in bash?
Glynn Clements wrote:
>
>
> 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 a gclements.plus.com>
> _______________________________________________
> grass-user mailing list
> grass-user a lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>
>
--
View this message in context: http://www.nabble.com/script-in-python-tp20758838p20769746.html
Sent from the Grass - Users mailing list archive at Nabble.com.
More information about the grass-user
mailing list