It is -dstnodata
you need, 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 the provided value (or one for each band), the nodata values from the input data set, or some default value. I don't know how GDAL determines the default nodata values, 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.