[GIS] Clipping raster with vector layer using GDAL

gdalpython

I have installed GDAL using the Osgeo installer. How can I clip a raster layer with a vector layer programmatically? Is there any GDAL API available which can help me with this? I am using Python.

Best Answer

I'm not sure about the gdal api, there is void* GDALWarpOptions::hCutline in the Warp Options referenced from the Warp API tutorial, but no explicit examples. Are you sure you need a programmatic answer? The command line utilities can do it out of the box:

  1. create a shapefile containing just the area of interest clipping polygon
  2. use ogrinfo to determine the extent of the clipping shapefile
  3. use gdal_translate to clip to the shape extents
  4. use gdalwarp with -cutline parameter

Steps 2 & 3 are for optimization, you could get by with just gdalwarp -cutline ....

See Clipping rasters with GDAL using polygons from Linfinity for linux based solution all wrapped up in one script. Another cutline example can be seen in Michael Corey's tutorial creating hillshades for Mapnik.