I'm using gdalwarp to clip a polygon out of a map layer using --crop_to_cutline.

The original map layer has a lot of empty pixels (well, transparent / alpha=0, to be precise). As it stands, my output is a GeoTIFF, where the pixels that were originally transparent remain as such, and the areas of the image that would fall outside of the cutline are also transparent.

What I want is to be able to differentiate, within that output image, between the pixels that were transparent in the source and the pixels that fell outside of the cutline, say by giving them a specific value that wouldn't exist anywhere else in the image.

Is there an easy way to do that?


Sounds like you could use -srcnodata None and -dstnodata [other reserved value].

The original nodata pixels would maintain their value and be treated as "regular" pixels. The resulting nodata pixels (the dstnodata value now identified by the metadata) would be the ones outside of the clipping shape.

If you need to further clamp the input image beforehand to reserve the extra value, you could use gdal_translate -scale [src_min src_max dst_min dst_max], e.g. to shift the valid data range from 1-255 to 2-255.

| improve this answer | |
  • If I set -dstnodata 1, say, is that going to set the value of the outside-the-cutline pixels to 1 on all bands? The documentation isn't very clear on this, at least not to me... – DanM Aug 31 '18 at 16:53
  • That's correct. Looks like you could also specify a value per band, although I've never tried it – mikewatt Aug 31 '18 at 16:54
  • It's confusing... the docs for -dstnodata show it as -dstnodata value [value...], and states "different values can be supplied for each band". That sounds to me like I would write -dstnodata 0 0 0 255, for example, if I wanted the rgb bands to be set at 0 and the alpha band at 255, i.e. solid black. But when I do that I get "Error4: 0: No such file or directory", (the "0" is replaced by whatever I put in for the second number), so it appears to be only looking at the first value as an argument to that flag. – DanM Aug 31 '18 at 17:01
  • Does it work if you keep the arg together with quotes? i.e. -dstnodata "0 0 0 255" – mikewatt Aug 31 '18 at 18:23
  • Ah, yes, command works then. Doesn't do what I expected it to, though... still getting everything transparent. – DanM Aug 31 '18 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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