opendatacube / de_hackathon_2024

For the joint Digital Earth Australia/Antarctica/Pacific ODC hackathon, September 2024.
Apache License 2.0
0 stars 1 forks source link

Support STAC documents that don't have top-level proj properties #2

Closed jmettes closed 2 months ago

jmettes commented 2 months ago

how to reproduce

datacube system init
datacube product add https://raw.githubusercontent.com/opendatacube/datacube-dataset-config/main/products/s2_l2a.odc-product.yaml
from pystac_client import Client
client = Client.open("https://earth-search.aws.element84.com/v1")
bbox = [
  176.13470010353373,
  -85.035941506574,
  180,
  -84.59129667426426
]
search_results = client.search(
    collections=["sentinel-2-c1-l2a"],
    bbox=bbox,
    datetime="2024-01-29/2024-01-31",
)
item = search_results.item_collection().items[0].to_dict()
from odc.apps.dc_tools import _stac
_stac.stac_transform(item)

error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 _stac.stac_transform(item)

File [~/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/apps/dc_tools/_stac.py:369](http://localhost:8888/home/jonathan/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/apps/dc_tools/_stac.py#line=368), in stac_transform(input_stac)
    367 proj_transform = properties.get("proj:transform")
    368 # TODO: handle old STAC that doesn't have grid information here...
--> 369 bands, grids, accessories = _get_stac_bands(
    370     input_stac,
    371     default_grid,
    372     proj_shape=proj_shape,
    373     proj_transform=proj_transform,
    374 )
    376 stac_properties, lineage = _get_stac_properties_lineage(input_stac)
    378 epsg = properties["proj:epsg"]

File [~/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/apps/dc_tools/_stac.py:234](http://localhost:8888/home/jonathan/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/apps/dc_tools/_stac.py#line=233), in _get_stac_bands(item, default_grid, proj_shape, proj_transform)
    231 # If transform specified here in the asset it should override
    232 # the properties-specified transform.
    233 transform = asset.get("proj:transform") or proj_transform
--> 234 grid = f"g{transform[0]:g}m"
    236 # As per transform, shape here overrides properties
    237 shape = asset.get("proj:shape") or proj_shape

TypeError: 'NoneType' object is not subscriptable

I fix it by getting one of the asset proj properties and setting them at top level

item['properties']['proj:transform'] = item['assets']['red']['proj:transform']
item['properties']['proj:shape'] = item['assets']['red']['proj:shape']
alexgleith commented 2 months ago

Using the odc.stac.eo3.stac2ds works fine in this case:

from pystac_client import Client

from odc.stac.eo3 import stac2ds

client = Client.open("https://earth-search.aws.element84.com/v1")
bbox = [
  176.13470010353373,
  -85.035941506574,
  180,
  -84.59129667426426
]

search = client.search(
    max_items=1,
    collections=["sentinel-2-c1-l2a"],
    bbox=bbox,
    datetime="2024-01-29/2024-01-31",
)

dataset = next(stac2ds(search.items()))