property-research/tools/pe_push_trails.py
acamilo 7d814c3413 willington 049-002-00: georeference the TH-22-4 harvest trails and crossings as splines with lidar z
Thin-plate-spline fit of the forester's 2022 map to the parcel, trails traced and checked against the 2023
0.25 ft ortho and 2 ft lidar; crossings snapped to DEM-derived channels. GeoJSON in WGS84 and EPSG:6433,
overlay images, method notes, and a payload/push script for the viewer's plan layer.
2026-10-08 17:09:17 -04:00

92 lines
4.7 KiB
Python
Executable file

#!/usr/bin/env python3
"""Push a trails GeoJSON (EPSG:6433 metres, x y z) into a Property Explorer plan layer.
The viewer's MCP server has no item-writing tool yet (only layer_create / orchard_layout), so this script
prepares the exact item payloads and sends them through whatever interface becomes available:
--dry-run print the items that would be sent (default when no transport is given)
--mcp URL --tool T call MCP tool T on the streamable-HTTP endpoint (e.g. http://host:8080/mcp) once per item,
with arguments {"property", "layer", "item"}; adjust map_item() if the tool's schema differs
--http URL POST the whole item list as JSON to URL (a REST endpoint, if one appears)
Nothing here embeds credentials: pass a bearer token through the PE_TOKEN environment variable if required.
Example:
tools/pe_push_trails.py properties/willington-ct-049-002-00/evidence/zoning/th-22-4/harvest-trails-6433.geojson \
--property willington-ct-049-002-00 --layer th-22-4-harvest-trails --dry-run
"""
import argparse, json, os, sys, urllib.request
def map_item(feature):
"""GeoJSON feature -> plan-layer item. Keep geometry in EPSG:6433 metres; z is advisory (viewer drapes)."""
p = feature["properties"]
g = feature["geometry"]
item = {
"id": feature.get("id"),
"kind": p.get("kind"), # skid-trail | stream-crossing | landing
"label": p.get("label"),
"geometry": {"type": g["type"], "coordinates": g["coordinates"], "crs": "EPSG:6433"},
"drape": True,
"style": {"skid-trail": {"stroke": "#d33", "width": 3, "dash": [8, 6]},
"stream-crossing": {"marker": "circle", "fill": "#0c0"},
"landing": {"marker": "square", "fill": "#fa0"}}.get(p.get("kind"), {}),
"note": p.get("basis"),
"source": p.get("source"),
}
if "control_vertices" in p:
item["spline"] = {"type": "catmull-rom", "control_vertices": p["control_vertices"], "sample_m": 5}
return item
def mcp_call(url, tool, arguments, token=None):
hdr = {"Content-Type": "application/json", "Accept": "application/json, text/event-stream"}
if token:
hdr["Authorization"] = f"Bearer {token}"
init = {"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {"protocolVersion": "2025-03-26", "capabilities": {}, "clientInfo": {"name": "pe-push-trails", "version": "0"}}}
req = urllib.request.Request(url, data=json.dumps(init).encode(), headers=hdr)
with urllib.request.urlopen(req, timeout=60) as r:
sid = r.headers.get("mcp-session-id")
hdr2 = dict(hdr, **({"Mcp-Session-Id": sid} if sid else {}))
call = {"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": tool, "arguments": arguments}}
req = urllib.request.Request(url, data=json.dumps(call).encode(), headers=hdr2)
with urllib.request.urlopen(req, timeout=120) as r:
return r.read().decode()
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("geojson")
ap.add_argument("--property", required=True)
ap.add_argument("--layer", required=True, help="plan layer slug (create it first with layer_create)")
ap.add_argument("--mcp", help="MCP streamable-HTTP endpoint")
ap.add_argument("--tool", help="MCP tool name that adds one item to a layer")
ap.add_argument("--http", help="REST endpoint that accepts {property, layer, items}")
ap.add_argument("--dry-run", action="store_true")
a = ap.parse_args()
fc = json.load(open(a.geojson))
if "6433" not in fc.get("crs_note", "") and "6433" not in a.geojson:
sys.exit("expected the EPSG:6433 file (harvest-trails-6433.geojson)")
items = [map_item(f) for f in fc["features"]]
token = os.environ.get("PE_TOKEN")
if a.dry_run or not (a.mcp or a.http):
print(json.dumps({"property": a.property, "layer": a.layer, "items": items}, indent=1)[:4000])
print(f"... {len(items)} items, {sum(len(i['geometry']['coordinates']) for i in items if i['geometry']['type']=='LineString')} line vertices")
return
if a.http:
req = urllib.request.Request(a.http, data=json.dumps({"property": a.property, "layer": a.layer, "items": items}).encode(),
headers={"Content-Type": "application/json", **({"Authorization": f"Bearer {token}"} if token else {})})
with urllib.request.urlopen(req, timeout=120) as r:
print(r.status, r.read().decode()[:2000])
return
if not a.tool:
sys.exit("--mcp needs --tool")
for it in items:
print(it["id"], mcp_call(a.mcp, a.tool, {"property": a.property, "layer": a.layer, "item": it}, token)[:300])
if __name__ == "__main__":
main()