Skip to main content

qualia_mcp/tools/
geospatial.rs

1//! MCP tools for Geospatial Coordinate Framing, Hilbert Spatial Indexing, and Geological WFS Resolvers.
2//!
3//! Powered by `qualia-geospatial`.
4//! Designed according to `AGENTS.md` ยง0-B (<500 lines).
5
6use crate::types::{parse_tool_args, McpSystemError};
7use qualia_geospatial::{
8    hilbert_to_xy, xy_to_hilbert, GeodeticCoord, GeodeticFrame,
9};
10use serde_json::{json, Value};
11
12/// `geospatial_project_coord` tool โ€” projects geodetic WGS84/GDA2020 coordinates to millimeter local frame and DGGS hash.
13pub fn project_coord_tool(args: &[u8]) -> Result<String, McpSystemError> {
14    let v = parse_tool_args(args)?;
15    let lat = v.get("latitude_deg").and_then(Value::as_f64).unwrap_or(-35.795);
16    let lon = v.get("longitude_deg").and_then(Value::as_f64).unwrap_or(148.315);
17    let elev = v.get("elevation_ahd_m").and_then(Value::as_f64).unwrap_or(520.0);
18
19    let frame = GeodeticFrame::default();
20    let target = GeodeticCoord {
21        latitude_deg: lat,
22        longitude_deg: lon,
23        elevation_ahd_mm: (elev * 1000.0) as i32,
24    };
25
26    let (x_mm, y_mm, z_mm) = frame.project_to_local_mm(&target);
27    let dggs_hash = frame.to_dggs_cell_hash(&target, 12);
28
29    let payload = json!({
30        "latitudeDeg": lat,
31        "longitudeDeg": lon,
32        "elevationAhdM": elev,
33        "localXMillimeters": x_mm,
34        "localYMillimeters": y_mm,
35        "localZMillimeters": z_mm,
36        "localXMeters": x_mm as f64 / 1000.0,
37        "localYMeters": y_mm as f64 / 1000.0,
38        "localZMeters": z_mm as f64 / 1000.0,
39        "dggsCellHash": format!("0x{:016x}", dggs_hash),
40    });
41    serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError)
42}
43
44/// `geospatial_hilbert_curve` tool โ€” computes Hilbert space-filling curve 1D spatial addresses and 2D tile unproject.
45pub fn hilbert_curve_tool(args: &[u8]) -> Result<String, McpSystemError> {
46    let v = parse_tool_args(args)?;
47    let order = v.get("order").and_then(Value::as_u64).unwrap_or(16) as usize;
48
49    if let (Some(x), Some(y)) = (
50        v.get("x").and_then(Value::as_u64),
51        v.get("y").and_then(Value::as_u64),
52    ) {
53        let d = xy_to_hilbert(x as u32, y as u32, order);
54        let payload = json!({
55            "mode": "xy_to_d",
56            "x": x,
57            "y": y,
58            "order": order,
59            "hilbertDistance": d,
60            "hilbertHex": format!("0x{:016x}", d),
61        });
62        return serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError);
63    } else if let Some(d) = v.get("hilbert_distance").and_then(Value::as_u64) {
64        let (x, y) = hilbert_to_xy(d, order);
65        let payload = json!({
66            "mode": "d_to_xy",
67            "hilbertDistance": d,
68            "order": order,
69            "x": x,
70            "y": y,
71        });
72        return serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError);
73    }
74
75    Err(McpSystemError::InvalidParameters)
76}
77
78/// `geospatial_resolve_geologic_unit` tool โ€” resolves Australian/regional WFS lithological units to physical recipes.
79pub fn resolve_geologic_unit_tool(args: &[u8]) -> Result<String, McpSystemError> {
80    let v = parse_tool_args(args)?;
81    let unit_code = v.get("unit_code").and_then(Value::as_str).unwrap_or("Gda");
82
83    let layer = qualia_geospatial::GeologicVectorLayer::new_snowy_monaro_baseline();
84    let record = layer
85        .units
86        .iter()
87        .find(|u| u.unit_code.eq_ignore_ascii_case(unit_code))
88        .cloned()
89        .unwrap_or_else(|| qualia_geospatial::GeologicUnitRecord {
90            unit_code: unit_code.to_string(),
91            lithology: "Unknown Lithology".to_string(),
92            stratigraphic_age_ma: 0.0,
93            lut_index: 255,
94        });
95
96    let payload = json!({
97        "unitCode": record.unit_code,
98        "lithology": record.lithology,
99        "stratigraphicAgeMa": record.stratigraphic_age_ma,
100        "lutIndex": record.lut_index,
101        "knownBaselineUnits": layer.units.iter().map(|u| &u.unit_code).collect::<Vec<_>>(),
102    });
103    serde_json::to_string(&payload).map_err(|_| McpSystemError::ParseError)
104}
105
106pub use project_coord_tool as geospatial_transform;
107pub use hilbert_curve_tool as geospatial_hilbert;
108pub use resolve_geologic_unit_tool as geospatial_strata;