Skip to content

Item handler: fall back to Zarr asset or raise a clear error (#68) - #69

Open
ghostiee-11 wants to merge 1 commit into
stac-utils:mainfrom
ghostiee-11:fix/item-handler-no-openable-asset
Open

Item handler: fall back to Zarr asset or raise a clear error (#68)#69
ghostiee-11 wants to merge 1 commit into
stac-utils:mainfrom
ghostiee-11:fix/item-handler-no-openable-asset

Conversation

@ghostiee-11

Copy link
Copy Markdown

What

When xpystac.to_xarray(item) is called on a pystac.Item that has no kerchunk reference, the existing handler runs off the end of the function and returns None. That None then propagates up to xarray's backend, which dereferences dataset.variables and raises an opaque AttributeError: 'NoneType' object has no attribute 'variables' from deep inside _protect_dataset_variables_inplace.

Reproduced on the live Earth Search NAIP catalog and in the Lumen STACSource path (holoviz/lumen#1867):

import pystac_client, xarray as xr, xpystac

client = pystac_client.Client.open('https://earth-search.aws.element84.com/v1')
item = next(client.search(collections=['naip'], max_items=1).items())
xr.open_dataset(item, engine='stac')
# AttributeError: 'NoneType' object has no attribute 'variables'

Fix

Add an explicit fallback in the Item handler:

  • If the Item has any Zarr / icechunk asset, dispatch to the registered Asset handler with that asset (so an item that ships a dataset-level asset alongside other formats now opens cleanly).
  • Otherwise, raise a ValueError naming the observed media_types and, if the item has a COG asset (the common case), pointing the user at stackstac / odc-stac, which are the right tools for COG-only items.

After the fix, the same snippet returns a clean error:

ValueError: Item 'me_m_4506963_se_19_030_20231115' has no Zarr or kerchunk
asset that xpystac can open as a dataset; observed asset media_types:
['application/xml', 'image/tiff; application=geotiff; profile=cloud-optimized'].
For COG / image assets, use stackstac or odc-stac to stack across items.

Tests

Added two regression tests against the existing simple_item fixture (its only assets are COG + thumbnail, mirroring NAIP's shape). All existing core tests stay green; test_to_xarray_reference_file was already failing on main in my env (unrecognized engine 'kerchunk') and is unrelated.

Fixes #68.

When `xpystac.to_xarray(item)` is called on a `pystac.Item` that has no
kerchunk reference, the existing handler runs off the end of the
function and returns None. That None then propagates up to xarray's
backend, which dereferences `dataset.variables` and raises an opaque
`AttributeError: 'NoneType' object has no attribute 'variables'` from
deep inside `_protect_dataset_variables_inplace`.

Tested live against Earth Search NAIP (https://earth-search.aws.element84.com/v1)
and reproduced in the lumen STACSource path (holoviz/lumen#1867):

  client = pystac_client.Client.open('https://earth-search.aws.element84.com/v1')
  item = next(client.search(collections=['naip'], max_items=1).items())
  xr.open_dataset(item, engine='stac')
  # AttributeError: 'NoneType' object has no attribute 'variables'

Add an explicit fallback in the Item handler:

* If the Item has any Zarr / icechunk asset, dispatch to the registered
  Asset handler with that asset. This means an item that ships a
  dataset-level asset alongside other formats now opens cleanly.

* Otherwise, raise a `ValueError` naming the observed media_types and,
  if the item has a COG asset (the common case), pointing the user at
  stackstac / odc-stac which are the right tools for COG-only items.

Fixes stac-utils#68.

Added two regression tests against the existing `simple_item` fixture
(its only assets are COG + thumbnail, mirroring NAIP's shape).
@ghostiee-11

Copy link
Copy Markdown
Author

Hey @jsignell, please check into this!!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pre-validate asset media_type before xr.open_dataset to fail clearly on non-Zarr formats

1 participant