24

I am trying to clip my Raster file according to a shapefile geometry. I am using the following code

gdalwarp -cutline INPUT.shp INPUT.tif OUTPUT.tif

But it is resulting a black color outside the shapefile geometry extent. I am giving the example here. In the first Image that I want to clip. The second image is the resultant raster but I want the 3rd Raster where the outside of the geometry will be null.

1st Image: INPUT Raster

1st Image: INPUT Raster

2nd Image: OUTPUT Raster

2nd Image: OUTPUT Raster

enter image description here

3rd Image: I want to get output like this

29

You need to use the -dstalpha option to gdalwarp e.g.:

gdalwarp -cutline INPUT.shp -crop_to_cutline -dstalpha INPUT.tif OUTPUT.tif

This will add an alpha band to the output tiff which masks out the area falling outside the cutline.

A late answer, but hopefully it will help someone else with the same problem.

  • 2
    without the -srcnodata <in> -dstnodata <out> hint by Curlew it did not work for me. – Jens Jun 4 '14 at 19:33
  • Well, -dstalpha is OK to use with imagery (it basically adds an alpha Band in GeoTIFF and mask the area). However, data values remains in the source Band (in this case I guess, that it is value 0). If you want to work with this as raster values (not imagery), -dstalpha is WRONG approach.@Curlew answer is much preferable option. Also, I found -crop_to_cutline option very dangerous, if you need to keep exact overlay (pixel-size and spacing) with the input raster. I prefer to set -te options (bounding box) to keep coordinates under full control. – jurajb Oct 2 '18 at 7:37
18

Try to specify the nodata-value from your input raster and set it for the output as well. Furthermore add the option -crop_to_cutline to make exact crops. More about the options here.

gdalwarp -srcnodata <in> -dstnodata <out> -crop_to_cutline -cutline INPUT.shp INPUT.tif OUTPUT.tif
  • 1
    Can You elaborate little bit more? What is used as <in> and <out> ? – Devils Dream Jan 2 '13 at 19:02
  • <in> is your input rasters nodata-value and <out> for your output-raster. Please have a look at the provided link where you can find all information and options (output format, source, extent, ... ) – Curlew Jan 2 '13 at 23:15
2

You can use raster clip option in QGIS which runs gdalwarp in the background and configures the right options for you.

http://www.qgistutorials.com/en/docs/raster_mosaicing_and_clipping.html

  • I want a ***.tif format – Devils Dream Jan 2 '13 at 19:18
  • The steps outlined in the link above should give you a .tif output. – spatialthoughts Jan 2 '13 at 20:01
  • 1
    The link seems dead. – Vadim Ovchinnikov Oct 9 '17 at 17:10
  • 1
    @VadimOvchinnikov browsed a bit and found where it was moved, fixed it – DarkCygnus Feb 2 '18 at 16:51

protected by Aaron Nov 26 '14 at 12:40

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.