Skip to content

GDAL Reference

Zach Levitt edited this page Oct 9, 2023 · 16 revisions

get GDAL set up:

echo 'export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH' >> ~/.bash_profile<br>
source ~/.bash_profile

fix GDAL error: Fatal Python error: config_get_locale_encoding: failed to get the locale encoding: nl_langinfo(CODESET) failed

export LANG='' in Terminal

reproject raster

gdalwarp -t_srs '+proj=aea +lat_0=33.790633 +lon_0=67.6757813 +lat_1=28.9919149 +lat_2=38.5893511 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs' -overwrite INPUT.tif OUTPUT.tif

get min and max values of tiff

gdalinfo -mm dem.tif

convert first band of raster to polygons polygonize with values placed in field value

gdal_polygonize.py raster.tif vector.shp -b 1 -f "ESRI Shapefile" vector value

georeference a PNG with extent coordinates and projection

gdal_translate -of GTiff -a_srs '+proj=aea +lat_0=33.790633 +lon_0=67.6757813 +lat_1=28.9919149 +lat_2=38.5893511 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs' -a_ullr -1160186.438 746557.603 927280.328 -753678.566 image.png image_georeferenced.tif

antimeridian netcdf fix

gdalwarp -t_srs EPSG:4326 -r bilinear -of GTIFF netcdf:INPUT.nc OUTPUT.tif
gdal_translate -a_srs EPSG:4326 -a_ullr -0.1250000 90.1250000 359.875 -90.125 -of VRT OUTPUT.tif ANOM_OUTPUT.vrt
gdalwarp -t_srs WGS84 ANOM_OUTPUT.vrt OUTPUT_ALIGNED.tif -wo SOURCE_EXTRA=1000 -te -180.125 -90.125 180.875 90.125

rescale raster to 16-bit unsigned integers

gdal_translate -of GTiff -ot Byte -scale 1493 3336 0 65535 nm-projected.tif nm-projected-rescaled.tif

prepare raster for blender

gdalwarp -t_srs '+proj=aea +lat_0=33.790633 +lon_0=67.6757813 +lat_1=28.9919149 +lat_2=38.5893511 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs' -overwrite dem.tif projected.tif && gdal_translate -of GTiff -ot UInt16 -scale -31 7833 0 65535 projected.tif projected-rescaled.tif

HILLSHADE with igor shading (vertical exaggeration x2)

gdaldem hillshade dem.tif hillshade.tif -igor -z 2

get certain bands

gdal_calc.py -A INPUT.tif --outfile=OUTPUT.tif --calc="A*logical_and(A>20,A<25)"

reclassify

gdal_calc.py -A INPUT.tif --calc="(A==21)*1 + (A==22)*2 + (A==23)*3 +(A==24)*4" --outfile OUTPUT.tif

output as mbtiles and create tiles

gdal_translate INPUT.tif OUTPUT.mbtiles -of MBTILES && gdaladdo -r nearest OUTPUT.mbtiles 2 4 8 16

crop to extent

gdalwarp -t_srs "EPSG:3857" -te_srs "EPSG:4326" -te -99.810246 29.378322 -94.712825 33.748783 -co COMPRESS=LZW -overwrite data.tif  data_crop.tif

export one band

gdal_translate -b 76 data.grib2 data_b1.tif

convert tiff to ASCII

gdal_translate  -of AAIGrid -co "FORCE_CELLSIZE=TRUE" [INPUT_FILENAME].tif [OUTPUT_FILENAME].asc

resample tif

gdalwarp -tr 0.02 0.02 -r bilinear [INPUT_FILENAME].tif [OUTPUT_FILENAME].tif

Merge rasters

gdal_merge.py -o dem_all.tif dem1.tif dem2.tif

crop raster to shapefile

gdalwarp -cutline usa.shp -crop_to_cutline dem.tif dem_cropped.tif      

remove null values in raster

use r.null in GRASS in qgis

remove geographic info from raster

gdal_edit.py -a_srs None data.tif

project alaska and hawaii dems for albersusa

AK is x0.37
+proj=aea +lat_1=55 +lat_2=70 +lat_0=65 +lon_0=-148 +x_0=0 +y_0=0

HI is x1 (no scaling)
+proj=aea +lat_1=19 +lat_2=24 +lat_0=20.9 +lon_0=-156.5 +x_0=0 +y_0=0

GDAL cheat sheet from Derek Watkins

Clone this wiki locally