Skip to content

Instantly share code, notes, and snippets.

@abelcallejo
Last active November 5, 2024 03:52
Show Gist options
  • Save abelcallejo/a0bf7d1b5d6172926d6d37e3f360c57b to your computer and use it in GitHub Desktop.
Save abelcallejo/a0bf7d1b5d6172926d6d37e3f360c57b to your computer and use it in GitHub Desktop.
GDAL cheatsheet

GDAL cheatsheet

Contents

Reprojection

GeoTiff

gdalwarp -overwrite -wm 1500 -t_srs EPSG:4326 -of GTiff /path/to/input.tif /path/to/output.tif

ENVI .hdr labeled

gdalwarp -overwrite -wm 1500 -t_srs EPSG:4326 -of ENVI /path/to/input.tif /path/to/output.tif

Where the input is an existing raster file /path/to/input.tif and the output is a new raster file /path/to/output.tif

Downscaling

gdalwarp -t_srs EPSG:4326 -r cubic -tr 0.000185185 0.000185185 input-90m.tif output-20m.tif
# Mapping under 20 degrees latitude
# 0.000046296 is  5 meters
# 0.000092593 is 10 meters
# 0.000185185 is 20 meters
# 0.000277778 is 30 meters
# 0.000833333 is 90 meters
Tables Are Cool
col 3 is right-aligned $1600
col 2 is centered $12
zebra stripes are neat $1

Clipping

gdalwarp -wm 1500 -cutline /path/to/mold.shp -crop_to_cutline -tr 20.0 20.0 -of GTiff /path/to/input.tif /path/to/output.tif

Where the inputs are an existing raster file /path/to/input.tif and an existing shape file /path/to/mold.shp the output is a new raster file /path/to/output.tif

Merging

# Step 1
# Create a VRT from a bunch of tiles
gdalbuildvrt /path/to/mosaic.vrt /path/to/tiles/*.tif

# Step 2
# Create a GeoTIFF from the VRT
gdal_translate /path/to/mosaic.vrt /path/to/mosaic.tif

Hillshading

gdaldem hillshade input.tif output.tif -of GTiff -b 1 -z 0.00000956 -s 1.0 -az 315.0 -alt 56.25
# Sun setting
# -alt 45.00 is too dark
# -alt 56.25 is just about right
# -alt 67.50 is too shinny

See the gdaldem reference manual for more details

Creating an RGBA multiband from a single band

gdaldem color-relief input.mosaic.vrt input.color.txt output.mosaic.vrt -alpha -of VRT -nearest_color_entry

See the syntax for input.color.txt.

Rasterizing

gdal_rasterize -l RiceAtlas -a PLANT_PK1 -tr 0.01 0.01 -a_nodata 0.0 -ot Int16 -of GTiff "/path/to/input.shp" "/path/to/output.tif"

Where:

  • -a PLANT_PK1 refers to the PLANT_PK1 attribute of the shapefile

Tiling

gdal2tiles.py --processes=2 --zoom=2-16 /path/to/input.vrt /path/to/output

QGIS layouts

North: 21.288

South: 4.143

West: 109.974

East: 134.221

Alternatively

North: 19.654 South: 4.583 West: 116.925 East: 126.980

Shape extraction from shape

ogr2ogr -where "attribute_name = 'desired_value'" output_shapefile.shp input_shapefile.shp

Tips

Layman's resolution (meters) GIS resolution (EPSG:4326) Max zoom level (gdal2tiles.py) Ease using standard computer Ease using super computer
90 0.000833333 12 Easy Easy
30 0.000277778 13 Medium Easy
20 0.000185185 13 Medium Easy
10 0.000092593 14 Hard To be tested
5 0.000046296 15 Very hard To be tested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment