Open-EO / openeo-python-client

Python client API for OpenEO
https://open-eo.github.io/openeo-python-client/
Apache License 2.0
156 stars 42 forks source link

No band name metadata after aggregate_spatial #612

Closed soxofaan closed 2 months ago

soxofaan commented 2 months ago

user has something like this (very simplified):

input = connection.load_collection("SENTINEL2_L2A", bands=["B02"])
avg_vec = input.aggregate_spatial(geometries="meh", reducer="mean")
avg_raster = (
    avg_vec
    .vector_to_raster(input)
    .rename_labels(dimension="bands", target=["B02_avg"])
)

merged = input.merge_cubes(avg_raster)

stuff = merged.band("B02_avg")

the last step fails with

ValueError: Invalid band name/index 'B02_avg'. Valid names: ['B02']

while client should be able to figure out that the available band names for merged are "B02" and "B02_avg"

soxofaan commented 2 months ago

cc @llanduyt

soxofaan commented 2 months ago

reason is that aggregate_spatial does not set metadata

https://github.com/Open-EO/openeo-python-client/blob/35b5fc7b0cc844bf5f02b0e437f683f501ffe5ff/openeo/rest/datacube.py#L1011

so merge_cubes does not know what band names to add

workaround is to manually define the band dimension metadata on avg_vec, before using it further, e.g. very crudely

avg_vec.metadata = openeo.metadata.CubeMetadata(
    dimensions=[
        input.metadata.band_dimension
    ]
)
soxofaan commented 2 months ago

fixed by 2bac71979d0fdef87c959fdc2f32f78be5005759