Tuesday, 9 October 2018

Batch processing DEM rasters in QGIS/GDAL



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")

Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...