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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Nikolai Bezdna
Hello gdal devs!

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).

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. How can I read mask file with gdal and convert my initial RGB GeoTiff to a GeoTiff with a mask band from it?

Cheers,
Nick
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GDAL doesn't read GeoTiff .msk mask file

Even Rouault-2

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


_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GDAL doesn't read GeoTiff .msk mask file

Even Rouault-2

On lundi 19 décembre 2016 13:38:04 CET Even Rouault wrote:

> 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)

 

Actually looking at the code and experimenting, that's super weird. The .msk file should always be listed in the file list, even if there are no INTERNAL_MASK_FLAGS_x metadata item. Does gdalinfo on the .msk file succeed ?

 

--

Spatialys - Geospatial professional services

http://www.spatialys.com


_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GDAL doesn't read GeoTiff .msk mask file

Nikolai Bezdna
In reply to this post by Even Rouault-2
Even, thanks a lot for replying.

No, the strange thing is neither gdalinfo, nor gdal_translate work on my .msk file.
Gdalinfo returns "not recognized as a supported file format”. Though this .msk works ok in ArcGIS.
I can even export raster from ArcGIS with baked-in mask (like setting output to 16-bit and nodata value to 256 (anything bigger than 255)).
But then I don’t know how to get gdal translate it back to 8 bit with adding a mask band from pixels with value of 256. Is that possible?

Cheers,
Nick

>
> On lundi 19 décembre 2016 13:38:04 CET Even Rouault wrote:
>> 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)
>
> Actually looking at the code and experimenting, that's super weird. The .msk file
> should always be listed in the file list, even if there are no INTERNAL_MASK_FLAGS_x
> metadata item. Does gdalinfo on the .msk file succeed ?
>
> --
> 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/6b1c110f/attachment.html>
>

_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GDAL doesn't read GeoTiff .msk mask file

Even Rouault-2

On mardi 20 décembre 2016 04:34:55 CET Nikolai Bezdna wrote:

> No, the strange thing is neither gdalinfo, nor gdal_translate work on my

> .msk file.

> Gdalinfo returns "not recognized as a supported file format”.

 

Oh, then that's really an ArcGIS specific thing. Not even a regular TIFF

 

> Though this .msk works ok in ArcGIS. I can even export raster from ArcGIS

> with baked-in mask (like setting output to 16-bit and nodata value to 256

> (anything bigger than 255)). But then I don’t know how to get gdal

> translate it back to 8 bit with adding a mask band from pixels with value

> of 256. Is that possible?

 

Use gdal_translate -a_nodata 256 in.tif out.tif

 

--

Spatialys - Geospatial professional services

http://www.spatialys.com


_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Loading...