gjoseph92 / stackstac

Turn a STAC catalog into a dask-based xarray
https://stackstac.readthedocs.io
MIT License
238 stars 49 forks source link

`TypeError: cannot unpack non-iterable NoneType object` on seemingly valid stac item list #187

Closed vlandau closed 1 year ago

vlandau commented 1 year ago

I'm currently working with some elevation data from Microsoft's Planetary Computer STAC, and I'm getting a stackstac error only for a specific area/tile (other AOIs seem to work).

To reproduce:

import planetary_computer as pc
from pystac_client import Client as stac_client
import stackstac

bbox = (-104.79075, 30.86168, -104.77074999999999, 30.881680000000003)

# Open the catalog with pystac_client  
catalog = stac_client.open("https://planetarycomputer.microsoft.com/api/stac/v1")

# Search the catalog
search = catalog.search(collections=["3dep-seamless"], bbox=bbox)
items = list(search.items())

# We only want the high res layers
items_high_res = [
    pc.sign(item).to_dict()
    for item in items
    if item.properties["gsd"] == 10
]

elevation = stackstac.stack(
    items_high_res,
    bounds=bbox,
    chunksize=64
)

Output:

>>> elevation = stackstac.stack(
...     items_high_res,
...     bounds=bbox,
...     chunksize=64
... )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vincent/miniconda3/envs/soil_moisture/lib/python3.10/site-packages/stackstac/stack.py", line 287, in stack
    asset_table, spec, asset_ids, plain_items = prepare_items(
  File "/home/vincent/miniconda3/envs/soil_moisture/lib/python3.10/site-packages/stackstac/prepare.py", line 333, in prepare_items
    out_bounds = geom_utils.snapped_bounds(out_bounds, out_resolutions_xy)
  File "/home/vincent/miniconda3/envs/soil_moisture/lib/python3.10/site-packages/stackstac/geom_utils.py", line 73, in snapped_bounds
    xres, yres = resolutions_xy
TypeError: cannot unpack non-iterable NoneType object

As you might expect based on the stacktrace, manually setting the resolution in the stack call avoids the issue. Notably, it is successful when the bbox is set to (-97.73474, 33.785180000000004, -97.71473999999999, 33.80518)

Could something be wrong with this particular item in the STAC?

Version/environment info:

Python: 3.10.6

Package versions:

stackstac==0.4.3 planetary-computer==0.4.9 pystac_client==0.5.1

vlandau commented 1 year ago

I'm now noticing that passing a resolution results in a DataArray that has two additional empty indices, each with length 0 (band and time). It would appear there is something wrong with the STAC items.

gjoseph92 commented 1 year ago

Thanks @vlandau, I can reproduce it. Because of the incorrect metadata, it looks to stackstac like the item doesn't overlap with the bounds you've specified. However, there's a bug in stackstac when 0 items overlap. I'll look into how to fix that.

vlandau commented 1 year ago

Thanks for looking into this @gjoseph92! Let me know if there's anything I can do to help out.

gjoseph92 commented 1 year ago

Just merged a fix. I confirmed it works for your example, so no need to test it, but if you need something that works, go ahead and install from main for now.

vlandau commented 1 year ago

Awesome, thanks for such a speedy resolution to this @gjoseph92, and that's for all your work on this software package!