September 11, 2009

Gdal tools

Saving a portion of a map with gdal_translate

gdal_translate -of JPEG world.topo.bathy.200407.3x21600x21600.A1.png world.topo.bathy.200407.3x21600x21600.A1.jpg -srcwin 0 0 1000 1000 -co "WORLDFILE=YES"

Tiling with gdal for geoserver pyramid serving
gdal_retile -of JPEG -pyramidOnly -ps 256 256 -levels 5 -targetDir piramid world.topo.bathy.200407.3x21600x21600.A1.png

or for Geotiffs;

gdal_retile -levels 5 -pyramidOnly -targetDir pyramid bathymetry.tif

or
gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co "TILED=YES"
-co "COMPRESS=JPEG" -targetDir bmpyramid bmreduced.tiff



With geoserver 2.0.2 it is a piece of cake to load this pyramid into geoserver...


Indexing with gdaltindex
geotiffPyramid\pyramid>gdaltindex -tileindex location test 1\*.tif

Creating a 3D image file from elevation data
gdaldem hillshade elevationGeotif.tif elevation.png

Adding overlay
gdaladdo -r average bath.tif 2 4 8 16 32 64 128

Merging geotiffs
gdal_merge -o c:\merged.tif *.tif

Translating a png image to geotiff
You should have a world file for the png file .If you know the bounds of the image and the resolution, you can produce this info from here http://egb13.net/2009/03/worldfile-calculator/.
You can download the image files from here :
http://visibleearth.nasa.gov/view_detail.php?id=7105

and the image bounds are (get more details from readme.pdf) :
[X].3x21600x21600.A1.[Y] 90N 180W 0N 90W 240
[X].3x21600x21600.B1.[Y] 90N 90W 0N 0W 240
[X].3x21600x21600.C1.[Y] 90N 0W 0N 90E 240
[X].3x21600x21600.D1.[Y] 90N 90E 0N 180E 240
[X].3x21600x21600.A2.[Y] 0N 180W 90S 90W 240
[X].3x21600x21600.B2.[Y] 0N 90W 90S 0W 240
[X].3x21600x21600.C2.[Y] 0N 0W 90S 90E 240
[X].3x21600x21600.D2.[Y] 0N 90E 90S 180E 240
An example world file:

0.004166666666666667
0.00000
0.00000
-0.004166666666666667
-180
90
and the tranlation command:
gdal_translate -a_srs wktGeoreferenceFile -of Gtiff input.png output.tif

Example wkt file content(wgs 84) :

GEOGCS["WGS 84",
DATUM["World Geodetic System 1984",
SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]],
UNIT["degree", 0.017453292519943295],
AXIS["Geodetic longitude", EAST],
AXIS["Geodetic latitude", NORTH],
AUTHORITY["EPSG","4326"]]
Tiling a geotiff to smaller pieces
gdal_retile -targetDir targetdir -ps 2400 2400 a1.tif


Adding overviews to tiffs in a directory using sfk tool
sfk run -yes "gdaladdo -r average $file 2 4 8 16 32" .

Changing data type format of a Geotiff format (eg int16->Float32)
gdal_translate -ot Float32 input.tiff output.tiff


Scaling data in a Geotiff File(eg from meters to centimetes vice versa)
gdal_translate -scale 12615 12731 1.1 2.1  input.tif  WroclawDEM_output_scaled.tif