stac-utils / pystac-client

Python client for searching STAC APIs
https://pystac-client.readthedocs.io
Other
163 stars 48 forks source link

Feature request: Search catalog for latest datetime #751

Open ben-epoch-blue opened 4 weeks ago

ben-epoch-blue commented 4 weeks ago

I am trying to search for multiple datasets:

catalog = pystac_client.Client.open("https://planetarycomputer.microsoft.com/api/stac/v1", modifier=planetary_computer.sign_inplace)
search = catalog.search(collections=['nasadem', 'esa-worldcover', 'jrc-gsw'], datetime='2021', bbox=bbox).item_collection()

However, each dataset is derived from a different time period. I would like to get the latest dataset for each collection instead of all available datasets

gadomski commented 4 weeks ago

This isn't too hard to implement now:

from pystac_client import Client

client = Client.open("https://planetarycomputer.microsoft.com/api/stac/v1")
items = []
for collection in ["nasadem", "esa-worldcover", "jrc-gsw"]:
    items.append(
        next(
            client.search(
                collections=[collection], max_items=1, sortby="-properties.datetime"
            ).items()
        )
    )
# items == [<Item id=NASADEM_HGT_s56w072>, <Item id=ESA_WorldCover_10m_2021_v200_S60W030>, <Item id=90W_80Nv1_3_2020>]

I don't personally think this is a common-enough pattern to be worth building functionality around, but I'll leave this issue open to collect comments if others think differently.

ben-epoch-blue commented 4 weeks ago

Thank you for taking the time to write up the code!

I think it's a common use case for non-temporal analysis (in my case, creating cost layers) as there's no need for older datasets

jsignell commented 2 weeks ago

This is a good example of the type of thing that might be documented in a "How to..." section of the docs. Something like this: https://docs.xarray.dev/en/stable/howdoi.html