[gdal-dev] Create dummy internal mask

Even Rouault even.rouault at spatialys.com
Mon Sep 3 11:09:42 PDT 2018


On dimanche 2 septembre 2018 21:43:51 CEST Denis Rykov wrote:
> Hi,
> 
> I need to add dummy internal mask to 1-band GeoTIFF for further processing.
> The drawback of the following approach is that it mask legitimate 0 pixels:
> 
> gdal_translate --config GDAL_TIFF_INTERNAL_MASK YES -mask 1 nomask.tif
> 
> > mask.tif
> 
> The workaround to come over this issue looks like:
> 
> gdal_calc.py -A nomask.tif --calc="1" --outfile=calc_mask.tif
> 
> > gdalbuildvrt -separate result.vrt nomask.tif calc_mask.tif
> > gdal_translate --config GDAL_TIFF_INTERNAL_MASK YES -b 1 -mask 2
> > result.vrt mask.tif
> 
> Is there an easier way to do the same?

Not necessarily shorter, but less convoluted way that should be faster on 
large datasets:

$ python

from osgeo import gdal
ds = gdal.Open('my.tif', gdal.GA_Update)
gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES')
ds.CreateMaskBand(gdal.GMF_PER_DATASET)
band = ds.GetRasterBand(1).GetMaskBand()
band.Fill(255)


Other possibility, assuming that there is a pixel value that never appears in 
the band:

nearblack -color VALUE_THAT_DOES_NOT_APPEAR -near 0 -nb 0 -setmask \
	-o out.tif in.tif --config GDAL_TIFF_INTERNAL_MASK YES

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list