Skip to content

Commit

Permalink
Replace stackstac with odc-stac
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell committed Jul 18, 2023
1 parent beef8c0 commit 2438e95
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import xarray as xr


catalog = pystac_client.Client.open(
"https://earth-search.aws.element84.com/v0"
"https://earth-search.aws.element84.com/v1",
)

search = catalog.search(
intersects=dict(type="Point", coordinates=[-105.78, 35.79]),
collections=['sentinel-s2-l2a-cogs'],
datetime="2020-04-01/2020-05-01",
collections=['sentinel-2-l2a'],
datetime="2022-04-01/2022-05-01",
)

xr.open_dataset(search, engine="stac")
Expand Down Expand Up @@ -71,7 +71,7 @@ pip install git+https://github.com/stac-utils/xpystac
## How it works

When you call ``xarray.open_dataset(object, engine="stac")`` this library maps that open call to the correct library.
Depending on the ``type`` of ``object`` that might be [stackstac](https://github.com/gjoseph92/stackstac)
Depending on the ``type`` of ``object`` that might be [odc-stac](https://github.com/opendatacube/odc-stac)
or back to ``xarray.open_dataset`` itself but with the engine and other options pulled from the pystac object.

## Prior Art
Expand Down
2 changes: 1 addition & 1 deletion environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ dependencies:
- adlfs
- aiohttp
- fsspec
- odc-stac
- planetary-computer
- pystac-client
- requests
- rioxarray
- stackstac
- urllib3<2 # temporary pin https://github.com/stac-utils/pystac-client/issues/509
- zarr
# testing
Expand Down
15 changes: 14 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dask.array
import pytest

from xpystac.core import to_xarray
Expand All @@ -9,10 +10,22 @@ def test_to_xarray_with_cog_asset(simple_cog):


def test_to_xarray_with_pystac_client_search(simple_search):
ds = to_xarray(simple_search, assets=["blue", "green", "red"])
ds = to_xarray(simple_search)
assert ds


def test_to_xarray_returns_dask_backed_object(simple_search):
ds = to_xarray(simple_search)
assert isinstance(ds.blue.data, dask.array.Array)
assert ds.blue.data.npartitions > 1


def test_to_xarray_with_pystac_client_search_passes_kwargs_through(simple_search):
ds = to_xarray(simple_search, assets=["red", "green", "blue"], chunks={})
assert list(ds.data_vars) == ["red", "green", "blue"]
assert ds.blue.data.npartitions == 1


def test_to_xarray_with_drop_variables_raises(simple_search):
with pytest.raises(KeyError, match="not implemented for pystac items"):
to_xarray(simple_search, drop_variables=["blue"])
Expand Down
9 changes: 7 additions & 2 deletions xpystac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ def _(
drop_variables: Union[str, List[str], None] = None,
**kwargs,
) -> xarray.Dataset:
stackstac = _import_optional_dependency("stackstac")
odc_stac = _import_optional_dependency("odc.stac")
default_kwargs: Mapping = {"chunks": {"x": 1024, "y": 1024}}
if drop_variables is not None:
raise KeyError("``drop_variables`` not implemented for pystac items")
return stackstac.stack(obj, **kwargs).to_dataset(dim="band", promote_attrs=True)
if isinstance(obj, pystac.Item):
items = [obj]
else:
items = [i for i in obj]
return odc_stac.load(items, **{**default_kwargs, **kwargs})


@to_xarray.register
Expand Down

0 comments on commit 2438e95

Please sign in to comment.