It is -dstnodata
you needare asking for, not -dstalpha
:
gdalwarp -cutline -dstnodata -9999 INPUT.shp INPUT.tif OUTPUT.tif
gdalwarp
sets the area outside the -cutline
polygon to -dstnodata
, which can be a specified value (or one for each band), the nodata values from the input data set, or, if none of the two is available, some default value (in order of priority), which however is not set as nodata value on the output dataset. I don't know how GDAL determines thethat default nodata value, but for Byte
data, it is zero.
An alternative solution that was proposed here "is to use gdal_translate
with -projwin
to extract to shape envelope and then gdal_rasterize
with -i
to burn nodata where shape is missing." This solution also circumvents raster shifting or resampling.
But all of these are using GDAL command line tools.