opendatacube / odc-geo

GeoBox and geometry utilities extracted from datacube-core
https://odc-geo.readthedocs.io/en/latest/
Apache License 2.0
83 stars 15 forks source link

Feature request: have save_cog_with_dask write band descriptions #138

Closed alexjohnson-kobold closed 7 months ago

alexjohnson-kobold commented 7 months ago

Using write_cog we get band descriptions, but these don't currently show up if we use save_cog_with_dask. Could this be added? For example if I open a COG created with write_cog in a text editor I see a block like:

<GDALMetadata>
  <Item name="OVR_RESAMPLING_ALG">NEAREST</Item>
  <Item name="DESCRIPTION" sample="0" role="description">green</Item>
  <Item name="DESCRIPTION" sample="1" role="description">red</Item>
  <Item name="DESCRIPTION" sample="2" role="description">nir</Item>
  <Item name="DESCRIPTION" sample="3" role="description">swir1</Item>
  <Item name="DESCRIPTION" sample="4" role="description">swir2</Item>
  <Item name="DESCRIPTION" sample="5" role="description">swir3</Item>
  <Item name="DESCRIPTION" sample="6" role="description">swir4</Item>
  <Item name="DESCRIPTION" sample="7" role="description">swir5</Item>
  <Item name="DESCRIPTION" sample="8" role="description">swir6</Item>
  <Item name="DESCRIPTION" sample="9" role="description">tir1</Item>
  <Item name="DESCRIPTION" sample="10" role="description">tir2</Item>
  <Item name="DESCRIPTION" sample="11" role="description">tir3</Item>
  <Item name="DESCRIPTION" sample="12" role="description">tir4</Item>
  <Item name="DESCRIPTION" sample="13" role="description">tir5</Item>
</GDALMetadata>

Which means in QGIS I see band labels like "Band 04: swir1" whereas for a COG made by save_cog_with_dask I just see "Band 04"

cc @mplough-kobold @Kirill888

alexjohnson-kobold commented 7 months ago

Actually looks as though the band descriptions I'm seeing aren't coming directly from write_cog but from some custom code we have on top of it - regardless, that code won't (directly at least) work with save_cog_with_dask so the request remains.

Kirill888 commented 7 months ago

I would approach this by implementing an optional gdal_metadata_extra: str|List[str]|None = None, for example

save_cog_with_dask(
  xx, ...,
  gdal_metadata_extra=[
    '<Item name="DESCRIPTION" sample="1" role="description">green</Item>',
    '<Item name="DESCRIPTION" sample="2" role="description">red</Item>',
])

This would require updating these two internal methods with an extra argument:

https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/cog/_tifffile.py#L467-L472

https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/cog/_tifffile.py#L41-L46

then passing on extra args in save_cog_with_dask:

https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/cog/_tifffile.py#L697-L698 https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/cog/_tifffile.py#L712-L713

Kirill888 commented 7 months ago

Then we can use that machinery to automatically add band names, overview resampling settings and whatever other relevant metadata other than stats to COG.

mplough-kobold commented 7 months ago

@Kirill888 thanks for the guidance on this. I modified save_cog_with_dask to write band names by default using the dataset attribute long_name by default. That way the user doesn't need to figure out how to deal with either XML escaping or -- even more fun -- GDAL's XML double escaping.

RichardScottOZ commented 7 months ago

Sensible way to do it Matthew, build it in.