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: PySTACAPIItemLister to list STAC Items matching STAC API search #111

Closed weiji14 closed 1 year ago

weiji14 commented 1 year ago

An iterable-style DataPipe to list STAC Items matching a STAC API search query! Calls pystac_client.ItemSearch.items() to yield pystac.Item instances.

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

Usage:

import pystac_client

from torchdata.datapipes.iter import IterableWrapper
from zen3geo.datapipes import PySTACAPIItemLister

# List STAC Items from a STAC API query
query = dict(
    bbox=[57.2, -20.6, 57.9, -19.9],  # xmin, ymin, xmax, ymax
    datetime=["2023-01-01T00:00:00Z", "2023-01-31T00:00:00Z"],
    collections=["s2_l2a"],
)
dp = IterableWrapper(iterable=[query])
dp_pystac_client = dp.search_for_pystac_item(
    catalog_url="https://explorer.digitalearth.africa/stac/"
)
dp_pystac_item_list = dp_pystac_client.list_pystac_items_by_search()

# Loop or iterate over the DataPipe stream
it = iter(dp_pystac_item_list)
stac_item = next(it)

print(stac_item)
# <Item id=ec16dbf6-9729-5a8f-9d72-5e83a8b9f30d>

print(stac_item.properties)
# {'title': 'S2B_MSIL2A_20230103T062449_N0509_R091_T40KED_20230103T075000',
#  'gsd': 10,
#  'proj:epsg': 32740,
#  'platform': 'sentinel-2b',
#  'view:off_nadir': 0,
#  'instruments': ['msi'],
#  'eo:cloud_cover': 0.02,
#  'odc:file_format': 'GeoTIFF',
#  'odc:region_code': '40KED',
#  'constellation': 'sentinel-2',
#  'sentinel:sequence': '0',
#  'sentinel:utm_zone': 40,
#  'sentinel:product_id': 'S2B_MSIL2A_20230103T062449_N0509_R091_T40KED_20230103T075000',
#  'sentinel:grid_square': 'ED',
#  'sentinel:data_coverage': 28.61,
#  'sentinel:latitude_band': 'K',
#  'created': '2023-01-03T06:24:53Z',
#  'sentinel:valid_cloud_cover': True,
#  'sentinel:boa_offset_applied': True,
#  'sentinel:processing_baseline': '05.09',
#  'proj:shape': [10980, 10980],
#  'proj:transform': [10.0, 0.0, 499980.0, 0.0, -10.0, 7900000.0, 0.0, 0.0, 1.0],
#  'datetime': '2023-01-03T06:24:53Z',
#  'cubedash:region_code': '40KED'}

TODO:

Notes:

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