GDAL Conversion Tips

Note: this is a post from 2009, transferred to this new board with small updates for 2023.

GDAL. This open-source raster library (used in many apps) has some support for map projected PDS3, PDS4, ISIS2, ISIS3, VICAR, FITS reading (and some write capabilities). It can export raw, ASCII or a tons of other formats like GeoTiff. It tries to maintain map projection information and some metadata to output formats that allow it. The GDAL utility applications have stretching (scaling options), map projection transformation support, clipping, resampling and mosaic capabilities.

There are many binary versions available, but our current favorite is to use anaconda. First download and install miniconda installed or anaconda installed, then to get GDAL, run conda install -c conda-forge gdal. more

for the main gdal_translate help: http://gdal.org/gdal_translate.html

  • to see what formats are currently supported:
    > gdal_translate --formats

  • convert input bit type = same output type in Tiff (ISIS2/3 cub example ):
    > gdal_translate -of GTiff input.cub output.tif
    - GTiff is the default format so you can actually skip “-of GTiff” in this case

  • 16bit Tiff (truncate floats):
    > gdal_translate -of GTiff -ot Int16 input.cub output.tif

  • 16bit UInt Tiff (truncate ALL negative values and floats):
    > gdal_translate -ot UInt16 -of GTiff -a_nodata 0 input.cub output.tif

  • scaled 16bit Uint Tiff Good for 3D applications like Blender, Bryce, Vue, changes pixel values across a positive 16bit range:
    > gdal_translate -ot UInt16 -of GTiff -a_nodata 0 -scale -767 2296 1 65536 moon11s308_8.cub moon.tif
    - output range has been scaled to: 1 65536. To set the same integer range for this example try (where 2296 - -767 + 1 = 3064) or:
    > gdal_translate -ot UInt16 -of GTiff -a_nodata 0 -scale -767 2296 1 3064 moon11s308_8.cub moon.tif

  • convert GeoTIFF (or other) to ISIS3+ cube
    > gdal_translate -of ISIS3 input.tif output.cub

  • convert GeoTIFF (or other) to ISIS3+ cube, but maintain GeoTIFF with a detached ISIS3+ label (will work in ISIS applications).
    > gdal_translate -of ISIS3 -co DATA_LOCATION=GEOTIFF input.tif output.lbl
    - note a label extension is used for the output file

  • output raw (ENVI compatible):
    > gdal_translate -of ENVI input.cub out.raw
    - This will create a ENVI header (*.hdr) with parameters spelled out

  • stretch to PNG 8 bit and save out PNG GIS worldfile (recommended):
    > gdal_translate -of PNG -scale -co worldfile=yes input.cub out.png

  • manual stretch to PNG 8 bit (no GIS worldfile):
    > gdal_translate -of PNG -scale in_min in_max out_min out_max input.cub out.png
    - note I use output as 1 to 255 so that 0 is maintained as NULL - example:
    > gdal_translate -of PNG -scale 0.0232 0.12 1 255 -a_nodata 0 input.cub out.png

  • scale (resize) to PNG 8 bit at 10% its original size for a browse image:
    > gdal_translate -of PNG -scale -size 10% 10% input.cub out_browse.png

  • scale (resize) to JPG 8 bit at 100xN pixels for web image (aspect maintained):
    >gdal_translate -of JPEG -scale -size 100 0 input.cub out100xN.jpg

  • scale (resize) to JPG 8 bit at 100x200 pixels for web image (aspect not maintained):
    > gdal_translate -of JPEG -scale -size 100 200 input.cub out100x200.jpg

  • ESRI ASCII Grid format:
    > gdal_translate -of AAGrid input.cub output.asc


  • MOLA DEM example (16bit signed to unsigned, file)
    > gdal_translate -of UINT16 -a_nodata 0 -of GTIFF -scale -8201 21241 1 29443 Mars_MGS_MOLA_DEM_mosaic_global_463m.tif my_positive_MOLA.tif
    - of UINT16 = output Type unisgned 16 bit (good for photoshop)
    - a_nodata 0 = set a new NoData value to 0 (special value to be ignored).
    - of GTiff = output Format is GeoTiff (for Tiff with Geospatial tags)
    - scale = from min_in and max_in stretch to a min_out and max_out values. Here we are just pushing all values to positive, where:
    29443 = 21241 - -8201 + 1 We add one to account for the NoData value.

  • information about image:
    > gdalinfo input.cub

  • stats about image:
    > gdalinfo -stats input.cub

  • min/max range:
    > gdalinfo -mm input.cub

  • want the label as JSON, try to dump the “mdd” metadata
    > gdalinfo -mdd all -json input_pds4.xml
    - note for PDS4 labels, this will simply wrap the PDS4 XML header inside a JSON block. It will not convert the XML label to JSON. For label parsing, I would recommend Python’s element tree or lxml2 for XML or for PDS3/ISIS cubes, use Python’s pvl library.

  • To project to new projection use the program gdalwarp.

more:

3 Likes