Ticket #1599 (new enhancement)

Opened 1 year ago

Last modified 3 months ago

Clip By Polygon, Clip By Mask, Clip By File

Reported by: maphew Assigned to: warmerdam
Priority: normal Milestone:
Component: GDAL_Raster Version: unspecified
Severity: major Keywords: rasterize clip polygon
Cc:

Description

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:

  1. Bug Tracking
  2. User Wiki
  3. Developer Wiki
  4. GDAL/OGR In Other Languages
  5. 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.

Change History

04/27/07 15:05:07 changed by maphew

whups, the internal table of contents is wrong. It's supposed to refer to each of the subheadings below. I can't remove it though, sorry.

07/31/07 06:14:01 changed by laurent

Do this feature have a chance to be released ? Maybe it's just a dream ?

07/31/07 10:43:42 changed by warmerdam

  • keywords set to rasterize clip polygon.
  • component changed from default to GDAL_Raster.

Laurent,

This is currently a request enhancement but no one has indicated a willingness to implement it. So effectively, yes, it is a dream.

Note that gdal_rasterize can already do some of this (especially with the new -i flag - similar to -reverse above). But gdal_rasterize requires an output file to pre-exist. I think the best approach to this might be further work on gdal_rasterize.

05/06/08 14:42:19 changed by maphew

...I implemented part of what is asked in that ticket in the just released Mirone 1.3.0 We can now clip an image by an arbitrary polygon and create masks from the ensemble of plotted polygons (just right click on polygon and select what you want).

Joaquim Luis -- http://www.nabble.com/updates-on-masking-raster-data-tp17028508p17028831.html

Mirone's homepage: http://w3.ualg.pt/~jluis/mirone/

added to this ticket on the chance there may be an opportunity to share code between the projects for this feature.