-
Notifications
You must be signed in to change notification settings - Fork 85
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
Improvements to band_tags #799
Comments
I wonder if it would be helpful to have an interface to writing tags to the Dataset/DataArray similar to rasterio's: https://rasterio.readthedocs.io/en/stable/topics/tags.html#writing-tags |
@neda-dtu, mind providing examples of what your failed attempts looked like? |
I am not sure that I follow how the Dataset/DataArray tags would be handled similar to rasterio. Would this imply making an additional attribute to hold the tags? Here are my successful and failed examples. I used the attached test file from the Geotiff.js repository. nt_20201024_f18_nrt_s.tif.zip The metadata from
My first attempt was to just roundtrip the file: import xarray as xr
import rioxarray
ds = xr.open_dataarray("nt_20201024_f18_nrt_s.tif")
ds.rio.to_raster("nt_roundtrip.tif", "COG") This resulted in the statistics metadata being not being assigned to the bands, but the nodata value was assigned and a new description band field was added.
My next attempt was to try to write the whole dataset to raster. To try and differentiate between the band tags and the dataset tags. Of course didn't work as only dataarrays can be written. I then tried adding the tiff tag for GDALMetadata 42112, with the XML added, as I saw when using original tiffinfo
roundtripped tiffinfo
This didn't work as I wasn't supposed to be adding tiff tags directly and the XML wasn't parsed correctly. Finally, I dug around in the rioxarray code to see why the description field was correctly added to the band, which was when I found that band tags worked. I updated my code to add the statistics I needed as band tags: import xarray as xr
import rioxarray
ds = xr.open_dataarray("nt_20201024_f18_nrt_s.tif")
tags = {}
tags["STATISTICS_MINIMUM"] = float(ds.min())
tags["STATISTICS_MAXIMUM"] = float(ds.max())
ds.rio.to_raster("nt_band_tags.tif", "COG", tags={"band_tags": [tags]}) Then I got the idea that I could add the import xarray as xr
import rioxarray
ds = xr.open_dataarray("nt_20201024_f18_nrt_s.tif")
attrs = ds.attrs
ds.attrs = {
"AREA_OR_POINT": attrs.pop("AREA_OR_POINT"),
"band_tags": [attrs],
}
ds.rio.to_raster("nt_band_attrs.tif", "COG") |
The idea would be to add |
I was working on adding GDAL Metadata to a GeoTiff file, as it is needed for getting the minimum and maximum values when using Geotiff.js. I tried adding the
STATISTICS_MINIMUM
andSTATISTICS_MAXIMUM
in a few ways, but wasn't able to get it to work until I found out about the undocumentedband_tags
key for thetags
argument ofto_raster
.It would great to have documentation of this features, perhaps with an example. I could add a mention of it in the docstring. Would there be other documentation that you think could be helpful?
As described in Envi header information is stripped on write #635 the tags are flattened when read into attributes on read, so band_tags do not round trip between read and write. It seems that XArray will allow for dictionary based attributes. Would this be a feature of interest? I guess it would need to have a toggle on read, as NetCDF doesn't allow dicts as attributes.
The text was updated successfully, but these errors were encountered: