I'm quite new to Grass Gis and I'm trying to make DEM (digital elevation model) for modelling flooding. I have 170 *.xyz files, which need to be scanned for extents (r.in.xyz -s input=...\filename.xyz output=filename fs= ' '), then region needs to be set with previously recieved data (g.region n=... s=... e=... w=... res=2), importing lidar data (r.in.xyz input=...\filename.xyz output=filename fs= ' ') and at last interpolating that data (r.surf.idw input=filename@Siim output=filenameidw).

I did it successfully with one file, but it would be waste of time to do it manually for 170 files. Can anyone recommend me a loop, which I can use?

1 Answer 1


Here's a snippet of a bash shell script I use:

for f in precip_accum*.txt; do
    precip_rast=`basename ${f} .txt`
    reg=`r.in.xyz -s -g input=${f} output=dummy fs=, |  awk '{print $1" "$2" "$3" "$4}'`
    g.region --quiet $reg
    # Also set resolution to 1/30 degree (about 3 km)
    g.region --quiet res=0.033

    r.in.xyz --quiet --overwrite input=${f} output=${precip_rast} fs=, method=mean
    r.null --quiet $precip_rast setnull=0
    ... more lines with additional processing...

I'd like to add that with dense lidar point data you might not need to interpolate at all. This gives a big savings in time. If you have at least one lidar point per raster cell, then just use (as above) method=mean and each raster cell will get the mean height of the lidar points in that cell.

For a python script you would start with something like:

import os
import grass.script as grass
for f in os.listdir(...directory of your filename.xyz...):
    if f.endswith(".xyz"):
        f_out, ext = os.path.splitext(f)
        grass.run_command('r.in.xyz', input=f, output=f_out)

and similarly the rest of the grass modules you need

  • As I use Python in w7, what should i do differently?
    – Siim
    Feb 22, 2015 at 12:51
  • Also, I have a problem that GRASS commands like r.in.xyz and g.region don't work on the command line. Only when I select the appropriate GUI wizard from the program menu... Any advice on that?
    – Siim
    Feb 22, 2015 at 13:36
  • @Siim: which error does g.region give on the command line? (note that the GUI effectively calls g.region)
    – markusN
    Feb 25, 2015 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.