[gdal-dev] GDAL doesn't read GeoTiff .msk mask file

Even Rouault even.rouault at spatialys.com
Mon Dec 19 04:38:04 PST 2016


Nikolai,

> 
> I’ve got an 8-bit 3-band (RGB) GeoTiff file created in ArcGIS and it has a
> mask file (myfile.tif.msk) along with it, indicating which pixels have
> "nodata" value. When viewing it in ArcMap, I get “nodata” for masked
> pixels. But when viewing in QGIS, I get an unmasked raster and masked
> pixels return pixel values of R G and B bands. Gdalinfo doesn’t return a
> .tif.msk file in a list of dataset files (however it does return .aux.xml).

If you look at a valid .msk file generated by GDAL, you'll see INTERNAL_MASK_FLAGS_x
metadata item being reported. If there's no INTERNAL_MASK_FLAGS_x for band x,
then GDAL will ignore the .msk file for this band.

$ gdalinfo with_mask.tif.msk
Driver: GTiff/GeoTIFF
Files: with_mask.tif.msk
Size is 50, 50
Coordinate System is `'
Metadata:
  INTERNAL_MASK_FLAGS_1=2
  INTERNAL_MASK_FLAGS_2=2
  INTERNAL_MASK_FLAGS_3=2
Image Structure Metadata:
  COMPRESSION=DEFLATE
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,   50.0)
Upper Right (   50.0,    0.0)
Lower Right (   50.0,   50.0)
Center      (   25.0,   25.0)
Band 1 Block=50x50 Type=Byte, ColorInterp=Gray


The 2 here comes from the following GMF_ enumeration and so means that it
is a mask valid for all the bands of the dataset.

/** Flag returned by GDALGetMaskFlags() to indicate that all pixels are valid */
#define GMF_ALL_VALID     0x01
/** Flag returned by GDALGetMaskFlags() to indicate that the mask band is
 * valid for all bands */
#define GMF_PER_DATASET   0x02
/** Flag returned by GDALGetMaskFlags() to indicate that the mask band is
 * an alpha band */
#define GMF_ALPHA         0x04
/** Flag returned by GDALGetMaskFlags() to indicate that the mask band is
 * computed from nodata values */
#define GMF_NODATA        0x08

You can add this flag with gdal_translate :

gdal_translate my_invalid.tif.msk my_valid.tif.msk -co COMPRESS=DEFLATE \
 -mo INTERNAL_MASK_FLAGS_1=2 -mo INTERNAL_MASK_FLAGS_2=2 -mo INTERNAL_MASK_FLAGS_3=2


> 
> My goal is to add an internal mask band to GeoTiff files, so everything is
> packed within one file, and create a VRT mosaic afterwards. Another option
> is to use .tif.msk file as is. But I can’t find any options to
> gdal_translate, or gdalinfo to force gdal use the mask file. There isn’t
> much gdal documentation regarding .msk files. 

See:
https://trac.osgeo.org/gdal/wiki/rfc15_nodatabitmask
http://gdal.org/frmt_gtiff.html ("Internal nodata masks" section)

But it is true that the details of .msk files were not mentionned. I've just
improved that withhttps://trac.osgeo.org/gdal/changeset/36955 and
https://trac.osgeo.org/gdal/wiki/rfc15_nodatabitmask?action=diff&version=16&old_version=15

> How can I read mask file with
> gdal and convert my initial RGB GeoTiff to a GeoTiff with a mask band from
> it?

Once your mask is fixed to be recognized as explain above, you can create an 
internal mask with:

gdal_translate in_with_external_mask.tif out_with_internal_mask.tif --config GDAL_TIFF_INTERNAL_MASK YES

But note that QGIS does not handle GDAL mask band currently. You'll have to
transform the binary mask band as an alpha channel instead

gdal_translate rgb_with_mask.tif rgba.tif -b 1 -b 2 -b 3 -b mask

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20161219/3a2d377d/attachment.html>


More information about the gdal-dev mailing list