[GRASSLIST:7214] Re: grass commands with python script

David Finlayson david.p.finlayson at gmail.com
Fri Jun 17 14:16:51 EDT 2005


Here is a little example that shows  running a Python script from within grass:

**** start script ***

#!/cygdrive/c/python23/python
import os

# Run a command (no return)
os.system('d.rast dem_sd')

# Run a command and connect to the output stream (pipe)
pipe_in = os.popen('g.gisenv', 'r')

# Read the output from the pipe just like a file
output = pipe_in.readlines()

# Step through the output data
for line in output:
    field = line.split('=')
    print "%s is set to: %s" % (field[0], field[1])

**** end script *****

This script paints a raster (change the name to suit your dataset of
course) and then prints the grass environment variables.

You can also use popen to pipe data to a command. Note that this works
on Windows Grass too (in fact I am using the windows version of python
above!)

Hope that gets you started,

David

On 6/17/05, Stephan Holl <holl at gdf-hannover.de> wrote:
> Hello orkun,
> 
> On Thu, 16 Jun 2005 17:33:38 +0300 orkun <temiz at deprem.gov.tr> wrote:
> 
> > hello
> >
> > I was wondering if it was possible to write grass commands
> > in python script.
> >
> > can you send me an example, link, doc ?
> 
> As my python knowledge is near to zero here an untested code-snippet:
> <snippet>
> cmds = [ 'd.mon stop=x6',
>                  'g.region -d res=1',
>                  'r.colors map=ortho col=grey',
>                  'd.mon x6',
>                  'd.mon sel=x6',
>                  'd.rast ortho' ]
>         for cmd in cmds:
>                 dlg.ShowModal()
>                 dlg.Destroy()
>                 return
> </snippet>
> 
> Perhaps some python-gurus can provide better help...
> 
> Best
>         Stephan
> 
> --
> GDF Hannover - Solutions for spatial data analysis and remote sensing
> Hannover Office (TCH) -  Vahrenwalder Straße 7   -   D-30165 Hannover
> Internet: www.gdf-hannover.de      -      Email: holl at gdf-hannover.de
> Phone : ++49-(0)511-9357560       -          Fax: ++49-(0)511-9357100
> 
> 


-- 
David Finlayson
Marine Geology & Geophysics
School of Oceanography
Box 357940
University of Washington
Seattle, WA  98195-7940
USA

Office: Marine Sciences Building, Room 112
Phone: (206) 616-9407
Web: http://students.washington.edu/dfinlays




More information about the grass-user mailing list