Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am attempting to add relief shading to map using .asc raster files, following this tutorial. It works well, but I have only tried it on single tile of raster data which is 20x20km. I want to generate the shading for a much larger area, which means multiple tiles.

I understand I can input GDAL commands in the Hillshade/DEM dialogue in QGIS, and the tutorial suggests this means I can perform a batch operation, but I don't know enough (read any) GDAL to do this.

I am using QGIS 1.9 on Ubuntu (though I do have a Windows installation as well).

Any ideas?

EDIT: I have written the following script, demraster.sh:

!/bin/sh 
for f in *.asc 
do
echo "Processing %f" 
gdaldem hillshade %f -z 1.0 -s 1.0 -az 315.0 -alt 45.0 
done

I have set it to be executable with chmod, and run it with sh demraster.sh but get a syntax error:

Syntax error: word unexpected (expecting "do")
share|improve this question
    
    
Tried the suggestions in there, no luck. –  ElendilTheTall Jan 19 '13 at 9:35
    
Including the addition of semicolons as per @BradHards answer? Note the pound (#) prior to !/bin/sh also. The next question - are you using a bash shell? –  Jay Laura Jan 19 '13 at 14:43
    
Tried BradHard's script: same error. I have no idea what shell I'm using. I have no idea what a shell is. –  ElendilTheTall Jan 20 '13 at 16:13
    
Done a bit of Googling. If I type echo $SHELL, it returns /bin/bash. I have tried to change shells with chsh, but when I do that and type echo $SHELL again, it still returns bash. –  ElendilTheTall Jan 20 '13 at 16:23

5 Answers 5

up vote 3 down vote accepted

Looking at the dialog here, you might have some odd POSIX user permission stuff getting in the way. Or possibly DOS line endings (rather than UNIX). Rather than making a file, just do the loop from the command prompt, by typing it out in the terminal. I do this routinely, without the need of making a shell script file.

Typing this the first time, you should see the prompt character in the first column change from $ to > (don't type these!) until the end of the loop block:

$ for f in *.asc
> do
> echo "Processing $f"
> gdaldem hillshade  -z 2.0 -az 345.0 -alt 50.0 $f $f-HS.tif
> done

(Note: this is identical to yours, except I replace %f with $f.) After this, you can type the up key to see the previous command.

for f in *.asc; do echo "Processing $f"; gdaldem hillshade  -z 2.0 -az 345.0 -alt 50.0 $f $f-HS.tif; done

It is shown as a one-line condensed version with semi-colons, which you can use and modify (if needed) for further work from the command prompt.

share|improve this answer
    
Inputting it manually worked! Though using @nhopton's script (yours is missing the destination files I think). Still can't get it run as a shell script though. But this is good enough for me. Thanks! –  ElendilTheTall Jan 21 '13 at 14:58
    
it's frustrating when simple things don't work one way, but work another way. I updated it to nhopton's command for completeness. –  Mike T Jan 21 '13 at 20:37

You probably want the use the shell and gdaldem:

http://gdal.org/gdaldem.html

gdaldem hillshade in.asc out.tif
share|improve this answer
    
I absolutely do, but I have no idea how to. I have the necessary GDAL command (gdaldem hillshade %f -z 1.0 -s 1.0 -az 315.0 -alt 45.0, where %f is the file) but I have no idea how to run this on multiple files. I have very little Ubuntu/Linux experience. –  ElendilTheTall Jan 18 '13 at 15:45
    
Okay, I see. Make a text file with one command on each line, call it dem.sh, then make it executable using: chmod u+x dem.sh and then run it: ./dem.sh google simple shell scripting, and that should get you there. –  kyle Jan 18 '13 at 15:50
    
or write a one line for * do command. Here is a good example: gis.stackexchange.com/a/42741/2695 –  Jay Laura Jan 18 '13 at 15:54
    
I have #!/bin/sh for f in *.asc do echo "Processing %f" gdaldem hillshade %f -z 1.0 -s 1.0 -az 315.0 -alt 45.0 done but it gives me: Syntax error: word unexpected (expecting "do") –  ElendilTheTall Jan 18 '13 at 15:55
    
In other words Jay Laura, I have pretty much the command you linked to and it gives me that syntax error –  ElendilTheTall Jan 18 '13 at 15:56

This should do the trick. Make a text file with the following lines:

for f in *.asc
do
  echo "Processing $f"
gdaldem hillshade  -z 2.0 -az 345.0 -alt 50.0 $f $f-HS.tif
done

Save it with the extension .sh (for example "hillshade.sh"). Drop the file in the directory with your ASC rasters and then run it from a terminal: "sh hillshade.sh".

N.

share|improve this answer
    
I'll try this ASAP, hopefully it won't throw up a syntax error like the script in my question. –  ElendilTheTall Jan 18 '13 at 19:56
    
It shouldn't, I've just tested it on a directory of *.asc rasters. Just like yours possibly, 20 km x 20 km DTMs; Ordnance Survey OpenData? :) –  nhopton Jan 18 '13 at 22:54
    
No luck, I still get the error: demraster.sh: 2: demraster.sh: Syntax error: word unexpected (expecting "do") –  ElendilTheTall Jan 19 '13 at 9:32
    
Is it something I have set up wrong in Ubuntu? Something to do with the shell? I am a total noob when it comes to Linux. –  ElendilTheTall Jan 19 '13 at 9:38
1  
I did create it in Windows, trying it with OSGeo4W. I'll recreate it in Ubuntu. Thanks! –  ElendilTheTall Jan 21 '13 at 17:33

My guess would be that you are having issues with line endings - given that we have tried almost everything else.

I would use the script provided by @BradHards (copied below) and manually retype it on your system in either vi or nano. My suggestion would be nano as you do not need to learn vi to get this working.

Step by step would be to:

  1. Open a terminal and cd to the directory with your *.asc files.
  2. execute the command nano
  3. Retype the code below - do not cut and paste.
  4. control-x to exit, name the file batch_dem.sh or something, and save it.
  5. chmod +x batch_dem.sh
  6. ./batch_dem.sh

    #!/bin/sh 
    for f in *.asc;
    do
    echo "Processing %f";
    gdaldem hillshade %f -z 1.0 -s 1.0 -az 315.0 -alt 45.0;
    done
    
share|improve this answer

There are 2 ways (i know) to handle this:

  1. Using Bash commands (for loop)[only in linux]

  2. Using scripting languages like Python (for loop)[every system that installed Python]

Here is the code. You can modify it easily for linux

import os

pathOfDems = 'F:/Gdem'

pathForHill = 'F:/Hillshade'

pathToGdalDem = 'C:/OSGeo4W/bin/gdaldem.exe'

dliste = os.listdir(pathOfDems)

for i in range (0,len(dliste),1):

os.system(pathToGdalDem + ' hillshade ' + pathOfDems + os.sep + dliste[i] + '    ' +  pathForHill + os.sep + 'Hillshade_' + dliste[i][8:-8] + '.tif')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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