Skip to content

Saving geotiff dataset masks is not possible #80

Description

@mraspaud

Currently, trollimage has two behaviours when it comes to nodata: either a fill_value is provided and is used to fill the data (resulting in an image without alpha) or fill_value is None (default) in which case an alpha channel is used, and nodata value as set to full transparency.
It works very well in most cases.

However, the geotiff format supports the usage of dataset masks to flag out nodata pixels in a raster. They differ from alpha channels since they are purely binary. Benefits include lower disk space usage for storing the mask than an alpha channel (iiuc the mask is encoded on 1 bit per pixel), and allows more compression options, for example JPEG YCbCr (resulting in halving the size of the file saved to disk compared to JPEG alone, and YCbCr doesn't support alpha).

At the moment, the alpha channel generation is tightly coupled to the masking of the data, so we will have to refactor the code to be able to add support for a dataset mask.

A link to the relevant rasterio documentation:
https://rasterio.readthedocs.io/en/latest/topics/masks.html#dataset-masks
https://rasterio.readthedocs.io/en/latest/api/rasterio.io.html?highlight=write_mask#rasterio.io.DatasetWriter.write_mask

An example of writing such a mask with rasterio:

data = np.random.random((10, 10)).astype(rasterio.float64)
mask = np.random.choice([0, 255], (10, 10)).astype(np.uint8)

with rasterio.drivers(GDAL_TIFF_INTERNAL_MASK=True):
    with rasterio.open(tempfile.NamedTemporaryFile(dir=".", suffix=".tif").name, **kwargs) as dest:
        dest.write_band(1, data)
        dest.write_mask(mask)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions