Tutorial: Introduction to GDAL

5. Batch conversion

Desktop GIS programmes are very useful for GIS operations, but are hard to use if we have to repeat the same task for many GIS layers. Then scripting can be a solution.

Here we have an example dataset from a land-use model of Dublin. The data are in IDRISI raster format (.rst), with one layer for each year between 1990 and 2030. Our task is to convert all layers to GeoTiff (.tif )format.

1. Unzip landuse.zip provided with the course data to a new folder and check the contents.

Remember that for one raster file conversion we would use this:
gdal_translate -of GTiff 01_State19900101.rst 01_State19900101.tif

Now we're going to make a batch file (see Command Line tutorial) which includes a loop to convert all files in the folder.

2. Open a text editor, e.g. Notepad
3. Add the following code:

for %%f in (*.rst) do (
   echo %%~nf
   gdal_translate -of GTiff %%f %%~nf.tif
)

4. Save the batchfile as rst2tif.bat in the folder with the land-use rasters (don't forget to change the extension if you use Notepad, classic mistake!)

Try to understand the code. This is a for loop that loops over all *.rst files in the folder. %%f is the variable that contains the filename of each file. With echo we can print something to the screen. Here we print %%~nf , which is the part of the filename before the dot that separates it from the extension. Then we use the gdal_translate command with output format GeoTiff. At the end of the line we add the .tif extension to the filename.

5. Execute the batchfile. Type
rst2tif <ENTER>

6. Check the results