Tutorial: Introduction to GDAL
Conditions d’achèvement
10. Batch conversion
Desktop GIS applications are great for one‑off operations, but they become inefficient when you need to repeat the same task for many files. In those cases, simple scripting can save a lot of time. You have already explored pipelines. Here we'll create a loop in a batch file.
In this example, we work with a dataset from a land‑use model of Dublin. The rasters are stored in IDRISI (
.rst) format, with one file per year from 1990 to 2030. Our goal is to convert all of them to the GeoTIFF (.tif) format.1. Unzip
landuse.zip provided with the course data to a new folder and check the contents.If we were converting a single file, the new GDAL command would be:
gdal raster convert -f 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 plain-text editor, e.g. Notepad
3. Add the following code:
for %%f in (*.rst) do ( echo Converting %%~nf gdal raster convert -f 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 raster convert command with output format GeoTiff. At the end of the line we add the .tif extension to the filename. The quotes around the file names were added to take care of spaces in file names.5. Execute the batch file. Type
rst2tif <ENTER>6. Check the results