mirror of
https://github.com/dataforcanada/d4c-service-geo-assistant.git
synced 2026-06-14 23:11:01 +02:00
Render naip and summarize (#20)
* Intermediate * Fix naip geom handling * Fix imagery decoding for summary tool * Re enable xfail * Re enable xfail * Remove png references
This commit is contained in:
@@ -17,7 +17,7 @@ def geo_assistant_fixture():
|
||||
place=place_geojson,
|
||||
search_area=None,
|
||||
messages=[],
|
||||
naip_png_path="path/to/naip.png",
|
||||
naip_img_bytes=None,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
from types import NoneType
|
||||
|
||||
import pytest
|
||||
from geojson_pydantic import Feature
|
||||
from langchain_core.tools.base import ToolCall
|
||||
from shapely.geometry import box, mapping
|
||||
|
||||
from geo_assistant.agent.state import GeoAssistantState
|
||||
from geo_assistant.tools.naip import fetch_naip_img
|
||||
|
||||
|
||||
@@ -11,7 +13,7 @@ from geo_assistant.tools.naip import fetch_naip_img
|
||||
async def test_fetch_naip():
|
||||
"""
|
||||
Integration test: hit MPC STAC for NAIP around Union Market (DC),
|
||||
load imagery via odc-stac, and save an RGB PNG.
|
||||
load imagery via odc-stac, and save an RGB JPEG.
|
||||
|
||||
NOTE: This test requires:
|
||||
- Internet access (to reach Planetary Computer STAC + blobs)
|
||||
@@ -27,13 +29,13 @@ async def test_fetch_naip():
|
||||
# ~0.0001 degrees buffer in each direction
|
||||
aoi = box(lon - 0.0001, lat - 0.0001, lon + 0.0001, lat + 0.0001)
|
||||
aoi_geojson = mapping(aoi)
|
||||
|
||||
aoi_feature = Feature(type="Feature", geometry=aoi_geojson, properties={})
|
||||
tool_call = ToolCall(
|
||||
name="fetch_naip_img",
|
||||
args={
|
||||
"aoi_geojson": aoi_geojson,
|
||||
"start_date": "2021-01-01",
|
||||
"end_date": "2021-12-31",
|
||||
"state": GeoAssistantState(search_area=aoi_feature, messages=[]),
|
||||
},
|
||||
type="tool_call",
|
||||
id="test_tool_call_id",
|
||||
@@ -42,9 +44,9 @@ async def test_fetch_naip():
|
||||
# Call the actual tool - no STAC / odc-stac mocking
|
||||
result = await fetch_naip_img.ainvoke(tool_call)
|
||||
assert "naip_img_bytes" in result.update
|
||||
assert result.update["naip_img_bytes"] is not None, "Expected PNG bytes in result"
|
||||
assert isinstance(result.update["naip_img_bytes"], bytes)
|
||||
assert len(result.update["naip_img_bytes"]) > 1, "Expected non-empty PNG bytes"
|
||||
assert result.update["naip_img_bytes"] is not None, "Expected JPEG bytes in result"
|
||||
assert isinstance(result.update["naip_img_bytes"], str)
|
||||
assert len(result.update["naip_img_bytes"]) > 1, "Expected non-empty JPEG bytes"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -68,13 +70,13 @@ async def test_fetch_naip_too_large():
|
||||
# ~0.003 degrees buffer in each direction
|
||||
aoi = box(lon - 0.003, lat - 0.003, lon + 0.003, lat + 0.003)
|
||||
aoi_geojson = mapping(aoi)
|
||||
|
||||
aoi_feature = Feature(type="Feature", geometry=aoi_geojson, properties={})
|
||||
tool_call = ToolCall(
|
||||
name="fetch_naip_img",
|
||||
args={
|
||||
"aoi_geojson": aoi_geojson,
|
||||
"start_date": "2021-01-01",
|
||||
"end_date": "2021-12-31",
|
||||
"state": GeoAssistantState(search_area=aoi_feature, messages=[]),
|
||||
},
|
||||
type="tool_call",
|
||||
id="test_tool_call_id",
|
||||
@@ -83,5 +85,5 @@ async def test_fetch_naip_too_large():
|
||||
# Call the actual tool - no STAC / odc-stac mocking
|
||||
result = await fetch_naip_img.ainvoke(tool_call)
|
||||
assert "naip_img_bytes" in result.update
|
||||
assert result.update["naip_img_bytes"] is None, "Expected no PNG bytes in result"
|
||||
assert result.update["naip_img_bytes"] is None, "Expected no JPEG bytes in result"
|
||||
assert isinstance(result.update["naip_img_bytes"], NoneType)
|
||||
|
||||
@@ -1,29 +1,43 @@
|
||||
"""Tests for the satellite image summarization tool."""
|
||||
|
||||
import base64
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
from langchain_core.tools.base import ToolCall
|
||||
|
||||
from geo_assistant.agent.state import GeoAssistantState
|
||||
from geo_assistant.tools.summarize import summarize_sat_img
|
||||
|
||||
# Sample test data
|
||||
TEST_IMAGE_URL = "https://petapixel.com/assets/uploads/2022/08/French-Officials-Use-Satellite-Photos-and-AI-to-Spot-Unregistered-Pools-1536x806.jpg"
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"img_url,summary",
|
||||
[
|
||||
(TEST_IMAGE_URL, "building"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.xfail
|
||||
def test_summarize_sat_img(img_url, summary):
|
||||
command = summarize_sat_img.invoke(
|
||||
async def test_summarize_sat_img(img_url, summary):
|
||||
# Load the image from the supplied URL and encode it in base64
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||
}
|
||||
resp = requests.get(img_url, headers=headers)
|
||||
resp.raise_for_status()
|
||||
img_base64 = base64.b64encode(resp.content).decode("utf-8")
|
||||
command = await summarize_sat_img.ainvoke(
|
||||
ToolCall(
|
||||
name="summarize_sat_img",
|
||||
type="tool_call",
|
||||
args={"img_url": img_url},
|
||||
args={
|
||||
"state": GeoAssistantState(naip_img_bytes=img_base64, messages=[]),
|
||||
"tool_call_id": str(uuid.uuid4()),
|
||||
},
|
||||
id=str(uuid.uuid4()),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user