I am trying to create a script that given a tiff file with islands of data (which can be disjoint) and black pixels of nodata, can generate polygons (or new tiffs) that only wrap around the islands of data. The issue I am facing is that my nodata value is automatically 0 (0 0 0 I think actually, or black), and some of my islands also contain the value 0 inside of them, what is the best way to accomplish this in gdal?

For example, if I run a command like:

gdalwarp -srcnodata 0 -dstnodata 0 orig-ignore-black.tif black-nodata.tif

my resulting tiff has pockets inside the data regions that have been stripped because they had a 0 value.


You could try running gdal_sieve.py to remove the zeros within your data islands; replacing them with nearby values. You will have to experiment with the parameters to get the result you want. The zero values in the resulting file become your nodata mask. You can then try stacking the mask and your original raster with gdal_merge.py -separate. Finally you can try gdal_translate. Pay close attention to the -mask and -b options in the docs.

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.