weiji14 / zen3geo

The 🌏 data science library you've been waiting for~
http://zen3geo.rtfd.io/en/latest/walkthrough.html
GNU Lesser General Public License v3.0
77 stars 3 forks source link

:sparkles: PySTACAPISearchIterDataPipe to query dynamic STAC Catalogs #59

Closed weiji14 closed 2 years ago

weiji14 commented 2 years ago

An iterable-style DataPipe to make STAC API queries to dynamic STAC Catalogs! Uses pystac-client to perform the STAC API Item Search on the /search endpoint.

Preview at https://zen3geo--59.org.readthedocs.build/en/59/api.html#zen3geo.datapipes.PySTACAPISearch

Usage (requires planetary-computer>=0.4.7 and pystac-client>=0.5.0):

import planetary_computer
from torchdata.datapipes.iter import IterableWrapper
from zen3geo.datapipes import PySTACAPISearch

# Peform STAC API query using DataPipe
query = dict(
    bbox=[174.5, -41.37, 174.9, -41.19],
    datetime=["2012-02-20T00:00:00Z", "2022-12-22T00:00:00Z"],
    collections=["cop-dem-glo-30"],
)
dp = IterableWrapper(iterable=[query])
dp_pystac_client = dp.search_for_pystac_item(
    catalog_url="https://planetarycomputer.microsoft.com/api/stac/v1",
    modifier=planetary_computer.sign_inplace,
)
# Loop or iterate over the DataPipe stream
it = iter(dp_pystac_client)
stac_item_search = next(it)
stac_items = list(stac_item_search.items())

print(stac_items)
# [<Item id=Copernicus_DSM_COG_10_S42_00_E174_00_DEM>]

print(stac_items[0].properties)
# {'gsd': 30,
#  'datetime': '2021-04-22T00:00:00Z',
#  'platform': 'TanDEM-X',
#  'proj:epsg': 4326,
#  'proj:shape': [3600, 3600],
#  'proj:transform': [0.0002777777777777778,
#   0.0,
#   173.9998611111111,
#   0.0,
#   -0.0002777777777777778,
#   -40.99986111111111]}

TODO:

Part of https://github.com/weiji14/zen3geo/discussions/48.

References: