Wednesday 14 June 2017

gdal - gdalwarp cutline along with shapefile



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




Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...