[gdal-dev] crop an image

Frank Warmerdam warmerdam at pobox.com
Thu Jul 16 16:24:51 EDT 2009


Steve.Toutant at inspq.qc.ca wrote:
> 
> Hello,
> I need to crop an image based on a polygon from a shapefile.
> Could not figure how to do it so I used gdal_translate and I specified 
> the extent of the shapefile like this
> gdal_translate -projwin -428734.998712 20337 1.497532 -387578.449250 
> 172036.180727 EstMtl.img rssMtl.img
> 
> But the polygon is irregular. So the new image contains the region I 
> need with some extra.  From this new image Is there a way to assign 
> "nodata" to pixels outside the irregular polygon?
> 
> Or maybe there is a way to crop an image from an irregular polygon?
> thanks

Steve,

You can use the gdal_rasterize command to burn a nodata value to all
pixels outside a target polygon.

eg.
   gdal_rasterize -burn 0 -i -l polygon polygon.shp rssMtl.img

Note the -i flag meets to invert - that is assign all pixels outside
the polygon instead of all the pixels inside the polygon.

Docs at:
   http://www.gdal.org/gdal_rasterize.html

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the gdal-dev mailing list