[GRASS-user] script in python
Jose A. Ruiz-Arias
jararias at ujaen.es
Mon Dec 1 06:30:47 EST 2008
Hi Gabriele,
I suggest to try
grass.run_command('v.buffer','input = vect_1','output =
buffer_200m','buffer= 200')
assuming that grass.run_command is a wrapper to subprocess.popen or so.
Specifically, you could try
import subprocess
vbuffer=subprocess.Popen('v.buffer', 'input=vect_1',
'output=buffer_200m', 'buffer=200')
retcode=vbuffer.wait()
within the proper GRASS environment session. Read Python's Reference
Manual regarding subprocess
Cheers,
Jose
Gabriele Nolè escribió:
> 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 at gclements.plus.com>
>> _______________________________________________
>> grass-user mailing list
>> grass-user at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/grass-user
>>
>>
>
--
José A. Ruiz-Arias
Solar Radiation and Atmosphere Modelling Group
http://www.ujaen.es/investiga/tep220
Physics Department, University of Jaén
Campus Lagunillas, Building A3 066
23071 Jaén Spain
Tlf: +34 953 212 474
Fax: +34 953 212 838
More information about the grass-user
mailing list