Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pystac 1.12 search results cause ValueError when using odc.stac.load that does not happen with pystac 1.11 #1507

Closed
jkibele opened this issue Jan 26, 2025 · 2 comments

Comments

@jkibele
Copy link

jkibele commented Jan 26, 2025

I am not completely sure this is actually a problem in pystac (vs maybe the projection extension or another associated package), but I do know that downgrading from pystac==1.12 to pystac==1.11 was a successful workaround. In a conda environment created with this: mamba create -n pystac_prob_min python=3.11 pystac-client odc-stac shapely, running the following code will cause a ValueError.

import pystac_client
from odc import stac
from datetime import datetime as dt
from shapely.geometry import Point


def get_stac_items(x_coord: float, y_coord: float) -> pystac_client.item_search.ItemSearch:
    location_point = Point(x_coord, y_coord)
    collection = 'sentinel-2-l2a'
    start_date = None  # earliest available
    end_date = dt.today().strftime("%Y-%m-%d")

    # get a pystac client going to search the images
    client = pystac_client.Client.open("https://earth-search.aws.element84.com/v1")
    # From all days available with less than 5% cloud cover, get the 5
    # least cloudy days.
    search_results = client.search(
        collections=[collection],
        intersects=location_point,
        datetime=[start_date, end_date],
        query=["eo:cloud_cover<5"],
        sortby=["+properties.eo:cloud_cover"],
        max_items=5,
    )
    return search_results


if __name__ == '__main__':
    # Jalama beach point
    x, y = (-120.510536, 34.510536)
    # Get some items
    search_results = get_stac_items(x, y)
    ds = stac.load(
        search_results.items(),
        bands=['swir16'],
        chunks=dict(time=1, x=5490, y=5490),
    )
    print(ds)

Here's the traceback:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1570, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/jkibele/repos/land-mask-lambda/pystac_bug_min_example.py", line 35, in <module>
    ds = stac.load(
         ^^^^^^^^^^
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/odc/stac/_stac_load.py", line 360, in load
    _parsed = list(parse_items(items, cfg=stac_cfg, md_plugin=md_plugin))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/odc/stac/_mdtools.py", line 745, in parse_items
    proc.update(item)
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/odc/stac/_mdtools.py", line 574, in update
    self._bootstrap(item)
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/odc/stac/_mdtools.py", line 557, in _bootstrap
    _, band2grid = compute_eo3_grids(data_bands)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/odc/stac/_mdtools.py", line 317, in compute_eo3_grids
    geoboxes = dicttoolz.valmap(asset_geobox, assets)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/toolz/dicttoolz.py", line 85, in valmap
    rv.update(zip(d.keys(), map(func, d.values())))
  File "/Users/jkibele/miniforge3/envs/pystac_prob_min/lib/python3.11/site-packages/odc/stac/_mdtools.py", line 277, in asset_geobox
    raise ValueError(
ValueError: The asset must have the following fields (from the projection extension): shape, transform, and one of an epsg, wkt2, or projjson
python-BaseException

If I force pystac==1.11 (instead of version 1.12) by creating another conda env with this command: mamba create -n pystac_prob_fix python=3.11 pystac=1.11 pystac-client odc-stac shapely and run the same code, then I get the expected result which is an xarray Dataset containing the retrieved items.

The problem seems to have something to do with how the projection extension is working (the proj crs_string is coming back as None), but I haven't had time to dig any further at this point.

It seems that at least one other person was having the same problem: https://www.reddit.com/r/pythonhelp/comments/1i8uvgq/problem_in_odcstac_when_reading_geospacial/. (I'm the one that commented there, not the original poster).

@jkibele
Copy link
Author

jkibele commented Jan 26, 2025

After poking around a bit more, I found #1503. It looks very likely that was the cause of my problem. It seems that you fine folks have already found and fixed this issue, and I missed it when searching the issues earlier. Sorry to raise a false alarm, and thanks for all your work.

@gadomski
Copy link
Member

No worries at all, and thanks for the detailed report. I'm hoping to release the bugfix (and probably an implementation for #1504, which should also help) early this week.

@gadomski gadomski reopened this Jan 27, 2025
@gadomski gadomski reopened this Jan 27, 2025
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

No branches or pull requests

2 participants