Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving geotiff dataset masks is not possible #80

Open
mraspaud opened this issue Mar 23, 2021 · 2 comments
Open

Saving geotiff dataset masks is not possible #80

mraspaud opened this issue Mar 23, 2021 · 2 comments

Comments

@mraspaud
Copy link
Member

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)
@djhoese
Copy link
Member

djhoese commented Mar 23, 2021

Have you looked at how rioxarray makes this available? Given that we probably want to adopt rioxarray as a replacement for trollimage's custom geotiff handling, we should probably adopt/transition to a similar interface.

@mraspaud
Copy link
Member Author

No I haven't checked (yet). But we will have to wait for the full rioxarray transition as they don't support GCPs yet for example.

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

No branches or pull requests

2 participants