39

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

3 Answers 3

49

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.

3
  • 2
    without the -srcnodata <in> -dstnodata <out> hint by Curlew it did not work for me.
    – Jens
    Jun 4, 2014 at 19:33
  • 4
    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, 2018 at 7:37
  • gdalwarp -cutline Eastcoast.shp -crop_to_cutline -dstalpha "myrasterfinal77.tif" "demUTM12.tif" Getting invalid syntax Oct 20, 2020 at 11:45
29

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
3
  • 1
    Can You elaborate little bit more? What is used as <in> and <out> ? Jan 2, 2013 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, 2013 at 23:15
  • I am getting invalid syntax, can u help me with this gdalwarp -srcnodata "myrasterfinal77.tif" -dstnodata "demUTM12.tif" -crop_to_cutline -cutline "Eastcoast.shp" "myrasterfinal77.tif" "demUTM12.tif" Oct 20, 2020 at 11:43
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

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

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