A feature I wish for almost every week is the ability to clip an image by an irregular polygon. I envision it working something like this:
- Bug Tracking
-
User Wiki
-
Developer Wiki
-
GDAL/OGR In Other Languages
-
Releases
Clip By Polygon
gdal -clip poly.shp infile.tif outfile.tif {options}
Cookie cutter an image. Outside the polygon is nodata, inside is input.tif values.
Output extent is set to shapefile projwin extents.
gdal -clip poly.shp -reverse in.tif out.tif
Punches a hole in the image. In output raster interior of polygon is nodata.
Output extent is the larger of raster or shapefile.
gdal -clip poly.shp -multi -name [attrib] in.tif outdir
Create tiles. For each polygon in poly.shp create a raster clipped to extent of that polygon.
Use [attrib] field in poly.shp to name output rasters. If -name is omitted, sequentially number output (in.tif --> outdir/in_00.tif).
General
If clipping shapefile consists of multiple polygons, clipping boundary becomes outer boundary of all polys (merge polygons before clip).
Polgons can be discontiguous (voids and islands are respected).
Clip by Raster
gdal -clip mask.tif -mask 255,0,0 in.tif out.tif
Same as clip by poly, but treat RGB value of 255,0,0 as the nodata area (e.g. is the outside of the clipping polygons).
Output extent is shrunk/expanded to range of data areas.
gdal -clip mask.tif -mask 255,0,0 -reverse in.tif out.tif
Punch a hole. Same as clip by poly reversed.
Output extent is larger of inputs.
General
If no mask is specified, clipping boundary is set to extents of clipping image.
Nodata in clipping image is nodata in output image.
Clip By File
gdal -clip points.shp -extent-only in.tif out.tif
gdal -clip lines.shp -extent-only in.tif out.tif
gdal -clip polys.shp -extent-only in.tif out.tif
gdal -clip input.jp2 -extent-only in.tif out.tif
Ignore all boundary/mask logic and clip raster to projwin extent of any ogr/gdal supported data source.
Globals
Standard gdalwarp and gdal_translate options are respected, e.g. -co compress=lzw, -of jp2kak, -t_srs +utm=11, -outsize 50% 50%, etc.