GDAL / OGR cheatsheet

A wonderful cheatsheet for using GDAL and OGR – taken from Derek Watkins GitHub page

Vector operations

Get vector information

ogrinfo -so input.shp layer-name

Or, for all layers

ogrinfo -al -so input.shp

Print vector extent

ogrinfo input.shp layer-name | grep Extent

List vector drivers

ogr2ogr --formats

Convert between vector formats

ogr2ogr -f "GeoJSON" output.json input.shp

Clip vectors by bounding box

ogr2ogr -f "ESRI Shapefile" output.shp input.shp -clipsrc <x_min> <y_min> <x_max> <y_max>

Clip one vector by another

ogr2ogr -clipsrc clipping_polygon.shp output.shp input.shp

Reproject vector:

ogr2ogr output.shp -t_srs "EPSG:4326" input.shp

Add a spatial index:

ogrinfo example.shp -sql "CREATE SPATIAL INDEX ON example"

Merge vector files:

ogr2ogr merged.shp input1.shp
ogr2ogr -update -append merged.shp input2.shp -nln merged

Extract from a vector file based on query

To extract features with STATENAME ‘New York’,’New Hampshire’, etc. from states.shp

ogr2ogr -where 'STATENAME like "New%"' states_subset.shp states.shp

To extract type ‘pond’ from water.shp

ogr2ogr -where "type = pond" ponds.shp water.shp

Raster operations

Get raster information

gdalinfo input.tif

List raster drivers

gdal_translate --formats

Report PROJ.4 projection info, including bounding box (requires libgeotiff)

listgeo -proj4 mappy.tif

Convert between raster formats

gdal_translate -of "GTiff" input.grd output.tif

Convert a directory of raster files of the same format to another raster format

basename -s.img *.img | xargs -n1 -I % gdal_translate -of "GTiff" %.img %.tif

Reproject raster:

gdalwarp -t_srs "EPSG:102003" input.tif output.tif

Be sure to add -r bilinear if reprojecting elevation data to prevent funky banding artifacts.

Georeference an unprojected image with known bounding coordinates:

gdal_translate -of GTiff -a_ullr <top_left_lon> <top_left_lat> <bottom_right_lon> <bottom_right_lat> \
-a_srs EPSG:4269 input.png output.tif

Clip raster by bounding box

gdalwarp -te <x_min> <y_min> <x_max> <y_max> input.tif clipped_output.tif

Clip raster to SHP / NoData for pixels beyond polygon boundary

gdalwarp -dstnodata <nodata_value> -cutline input_polygon.shp input.tif clipped_output.tif

Crop raster dimensions to vector bounding box

gdalwarp -cutline cropper.shp -crop_to_cutline input.tif cropped_output.tif

Merge rasters -o merged.tif input1.tif input2.tif


gdalwarp input1.tif input2.tif merged.tif

Or, to preserve nodata values:

gdalwarp input1.tif input2.tif merged.tif -srcnodata <nodata_value> -dstnodata <merged_nodata_value>

Raster calculation (map algebra)

Average two rasters: -A input1.tif -B input2.tif --outfile=output.tif --calc="(A+B)/2"

Add two rasters: -A input1.tif -B input2.tif --outfile=output.tif --calc="A+B"


Create a hillshade from a DEM

gdaldem hillshade -of PNG input.tif hillshade.png

Change light direction:

gdaldem hillshade -of PNG -az 135 input.tif hillshade_az135.png 

Resample (resize) raster

gdalwarp -ts <width> <height> -r cubic dem.tif resampled_dem.tif

Entering 0 for either width or height guesses based on current dimensions.


gdal_translate -outsize 10% 10% -r cubic dem.tif resampled_dem.tif

For both of these, -r cubic specifies cubic interpolation: when resampling continuous data (like a DEM), the default nearest neighbor interpolation can result in “stair step” artifacts.

Burn vector into raster

gdal_rasterize -b 1 -i -burn -32678 -l layername input.shp input.tif

Extract polygons from raster input.tif -f "GeoJSON" output.json

Create contours from DEM

gdal_contour -a elev -i 50 input_dem.tif output_contours.shp

Get values for a specific location in a raster

gdallocationinfo -xml -wgs84 input.tif <lon> <lat>