-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial STAC search tool for sentinel-2 (#98)
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import datetime | ||
|
||
from zeno.tools.stac.stac_tool import stac_tool | ||
|
||
|
||
def test_stac_tool(): | ||
result = stac_tool.invoke( | ||
input={ | ||
"bbox": (73.88168, 15.45949, 73.88268, 15.46049), | ||
"min_date": datetime.datetime(2024, 8, 1), | ||
"max_date": datetime.datetime(2024, 9, 1), | ||
} | ||
) | ||
assert len(result) == 7 | ||
assert result[0] == "S2A_43PCT_20240831_0_L2A" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import datetime | ||
from typing import Tuple | ||
|
||
from langchain_core.tools import tool | ||
from pydantic import BaseModel, Field | ||
from pystac_client import Client | ||
|
||
|
||
class StacInput(BaseModel): | ||
"""Input schema for STAC search tool""" | ||
|
||
catalog: str = Field( | ||
description="STAC catalog to use for search", | ||
default="https://earth-search.aws.element84.com/v1", | ||
) | ||
collection: str = Field( | ||
description="STAC Clollection to use", default="sentinel-2-l2a" | ||
) | ||
bbox: Tuple[float, float, float, float] = Field( | ||
description="Bounding box for STAC search." | ||
) | ||
min_date: datetime.datetime = Field( | ||
description="Earliest date for retrieving STAC items.", | ||
) | ||
max_date: datetime.datetime = Field( | ||
description="Latest date for retrieving STAC items", | ||
) | ||
|
||
|
||
@tool( | ||
"stac-tool", | ||
args_schema=StacInput, | ||
response_format="content_and_artifact", | ||
) | ||
def stac_tool( | ||
bbox: Tuple[float, float, float, float], | ||
min_date: datetime.datetime, | ||
max_date: datetime.datetime, | ||
catalog: str = "https://earth-search.aws.element84.com/v1", | ||
collection: str = "sentinel-2-l2a", | ||
) -> dict: | ||
"""Find locations and their administrative hierarchies given a place name. | ||
Returns a list of IDs with matches at different administrative levels | ||
""" | ||
print("---SENTINEL-TOOL---") | ||
|
||
catalog = Client.open(catalog) | ||
|
||
query = catalog.search( | ||
collections=[collection], | ||
datetime=[min_date, max_date], | ||
max_items=10, | ||
bbox=bbox, | ||
) | ||
|
||
items = list(query.items()) | ||
print(f"Found: {len(items):d} datasets") | ||
|
||
# Convert STAC items into a GeoJSON FeatureCollection | ||
stac_json = query.item_collection_as_dict() | ||
stac_ids = [item.id for item in items] | ||
|
||
return stac_ids, stac_json |