Tutorial: Introduction to GDAL

5. Convert GIS formats

5.1. Convert raster formats

The primary function of gdal raster convert is to convert between raster formats. The basic syntax is:

gdal raster convert -f FORMAT <inputFile> <outputFile>

All supported formats can be found here. For FORMAT you should use the Short Name from the table.

Now we are going to convert the DEM from GeoTiff to SAGA format. SAGA is a GIS which has its own -gdal-supported- binary format with the .sdat file extension.

1. Execute the following command:
gdal raster convert -f SAGA dem_rd.tif dem_rd.sdat

Some formats need more arguments. PCRaster for example needs a specification of the data type (boolean, nominal, scalar, etc.). Let's convert the same data to PCRaster format.

PCRaster is a software library for environmental modeling and map algebra, widely used in hydrology, ecology, and geography. It provides operators for spatial analysis, dynamic modeling, and simulation of environmental processes. In QGIS, PCRaster is available through the PCRaster Tools plugin, which integrates its functions directly into the Processing Toolbox so users can run PCRaster operations within QGIS.

2. Execute the following command:
gdal raster convert -f PCRaster --co VS=SCALAR dem_rd.tif dem_rd.map

This gives the following error:

GDAL is expecting a Float32 raster. When you have checked the information with gdal info, you might have seen that the data type of our DEM is INT16. Therefore, we first need to convert the data type of the input.

3. Execute the following command:

gdal raster set-type --ot Float32 dem_rd.tif dem_rd_float.tif

4. Now you can do the conversion by executing this command:

gdal raster convert -f PCRaster --co VS=SCALAR dem_rd_float.tif dem_rd.map

To convert to PCRaster format you need to know the data type of the raster. In the example above the DEM is continuous data, so the data type is scalar. If the layer was discrete (classes) then the data type was nominal. The table below shows the data types withe the corresponding --ot and --co arguments for respectively gdal raster set-type and gdal raster convert.

Data Type --ot --co
Boolean Byte VS=BOOLEAN
Nominal Int32 VS=NOMINAL
Ordinal Int32 VS=ORDINAL
Scalar Float32 or Float64 VS=SCALAR
Direction Float32 or Float64 VS=DIRECTION
LDD Int32 VS=LDD

More info about gdal raster convert can be found here.

More info about gdal raster set-type can be found here.