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

gdal_merge.py -o merged.tif input1.tif input2.tif

Alternatively,

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:

gdal_calc.py -A input1.tif -B input2.tif --outfile=output.tif --calc="(A+B)/2"

Add two rasters:

gdal_calc.py -A input1.tif -B input2.tif --outfile=output.tif --calc="A+B"

etc.

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.

Alternatively,

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

gdal_polygonize.py 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>  

Sources

http://live.osgeo.org/en/quickstart/gdal_quickstart.html

https://github.com/nvkelso/geo-how-to/wiki/OGR-to-reproject,-modify-Shapefiles

ftp://ftp.remotesensing.org/gdal/presentations/OpenSource_Weds_Andre_CUGOS.pdf

http://developmentseed.org/blog/2009/jul/30/using-open-source-tools-make-elevation-maps-afghanistan-and-pakistan/

http://linfiniti.com/2010/12/a-workflow-for-creating-beautiful-relief-shaded-dems-using-gdal/

http://linfiniti.com/2009/09/clipping-rasters-with-gdal-using-polygons/

http://nautilus.baruch.sc.edu/twiki_dmcc/bin/view/Main/OGR_example

http://www.gdal.org/frmt_hdf4.html

http://planetflux.adamwilson.us/2010/06/modis-processing-with-r-gdal-and-nco.html

http://trac.osgeo.org/gdal/wiki/FAQRaster

http://www.mikejcorey.com/wordpress/2011/02/05/tutorial-create-beautiful-hillshade-maps-from-digital-elevation-models-with-gdal-and-mapnik/

http://dirkraffel.com/2011/07/05/best-way-to-merge-color-relief-with-shaded-relief-map/

http://gfoss.blogspot.com/2008/06/gdal-raster-data-tips-and-tricks.html

http://osgeo-org.1560.x6.nabble.com/gdal-dev-Dissolve-shapefile-using-GDAL-OGR-td5036930.html

https://www.mapbox.com/tilemill/docs/guides/terrain-data/

https://gist.github.com/ashaw/0862ec044c45b9aa3c76

https://github.com/gina-alaska/dans-gdal-scripts

Advertisements