Exporting rasters with the gdal_translate and gdalwarp GDAL commands
In this recipe, you will see a couple of main options for exporting PostGIS rasters to different raster formats. They are both provided as command-line tools, gdal_translate
and gdalwarp
, by GDAL.
Getting ready
You need the following in place before you can proceed with the steps required for the recipe:
You need to have gone through the previous recipe and imported the tmax 2012 datasets (12
.bil
files) as a single multiband (12 bands) raster in PostGIS.You must have the PostGIS raster format enabled in GDAL. For this purpose, check the output of the following command:
$ gdalinfo --formats | grep -i postgis
The output of the preceding command is as follows:
PostGISRaster (rw): PostGIS Raster driver
You should have already learned how to use the GDAL PostGIS raster driver in the previous two recipes. You need to use a connection string composed of the following parameters:
$ gdalinfo PG:"host=localhost port=5432 dbname='postgis_cookbook' user='me' password='mypassword' schema='chp01'password='mypassword' schema='chp01' table='tmax_2012_multi' mode='2'"
Refer to the previous two recipes for more information about the preceding parameters.
How to do it...
The steps you need to follow to complete this recipe are as follows:
As an initial test, you will export the first six months of the tmax for 2012 (the first six bands in the
tmax_2012_multi
PostGIS raster table) using thegdal_translate
command:$ gdal_translate -b 1 -b 2 -b 3 -b 4 -b 5 -b 6 PG:"host=localhost port=5432 dbname='postgis_cookbook' user='me' password='mypassword' schema='chp01' table='tmax_2012_multi' mode='2'" tmax_2012_multi_123456.tif
As the second test, you will export all of the bands, but only for the geographic area containing Italy. Use the
ST_Extent
command for getting the geographic extent of that zone:postgis_cookbook=# SELECT ST_Extent(the_geom) FROM chp01.countries WHERE name = 'Italy';
The output of the preceding command is as follows:
st_extent ------------------------------------------------------------ BOX(6.61975999999999 36.649162,18.514999 47.0947189999999) (1 row)
Now use the
gdal_translate
command with the-projwin
option for obtaining the desired purpose:$ gdal_translate -projwin 6.619 47.095 18.515 36.649 PG:"host=localhost port=5432 dbname='postgis_cookbook' user='me' password='mypassword' schema='chp01' table='tmax_2012_multi' mode='2'" tmax_2012_multi.tif
There is another GDAL command,
gdalwarp
, that is still a convert utility with reprojection and advanced warping functionalities. You can use it, for example, to export a PostGIS raster table, reprojecting it to a different spatial reference system. This will convert the PostGIS raster table to GeoTiff and reproject it fromEPSG:4326
toEPSG:3857
:gdalwarp -t_srs EPSG:3857 PG:"host=localhost port=5432 dbname='postgis_cookbook' user='me' password='mypassword' schema='chp01' table='tmax_2012_multi' mode='2'" tmax_2012_multi_3857.tif
How it works...
Both gdal_translate
and gdalwarp
can transform rasters from the PostGIS raster to all the GDAL-supported formats. To have a complete list of the supported formats, you can use the --formats
option of one of the GDAL's command line as follows:
$ gdalinfo --formats
For both these GDAL commands, the default output format is GeoTiff; if you need a different format, you must use the -of
option and assign to it one of the outputs produced by the previous command line.
In this recipe, you have tried some of the most common options for these two commands. As they are complex tools, you may try some more command options as a bonus step.
See also
To have a better understanding, you should check out the excellent documentation on the GDAL website:
The information about the
gdal_translate
command is available at http://www.gdal.org/gdal_translate.htmlThe information about the
gdalwarp
command is available at http://www.gdal.org/gdalwarp.html