|
| 1 | +"""OGC API – Moving Features read routes generated over the MEOS dispatcher. |
| 2 | +
|
| 3 | +The "pure MEOS round-trip" resources — the temporal-geometry sequence and its |
| 4 | +derived velocity / distance queries, and a stored temporal property — are |
| 5 | +served by dispatching the matching MEOS function from the vendored catalog |
| 6 | +through the ``Dispatcher`` + ``WireCodec``, then shaping the MF-JSON result as |
| 7 | +the OGC envelope. The collection / feature lifecycle, persistence and the |
| 8 | +GeoJSON envelope have no MEOS equivalent and stay hand-written. |
| 9 | +
|
| 10 | +OGC resource → MEOS catalog function (the alignment map): |
| 11 | +
|
| 12 | + tgsequence (export) → temporal_as_mfjson |
| 13 | + tgsequence/{tg}/velocity → tpoint_speed |
| 14 | + tgsequence/{tg}/distance → tpoint_cumulative_length |
| 15 | + tproperties/{name} → temporal_as_mfjson |
| 16 | +
|
| 17 | +``acceleration`` returns 501: with linearly interpolated position the speed is |
| 18 | +piecewise-constant, so its derivative is zero within each segment and undefined |
| 19 | +at the vertices; the value is not approximated. |
| 20 | +
|
| 21 | +A ``FeatureStore`` port abstracts the database read so the routes are testable |
| 22 | +with stubs; production wires it to PyMEOS + psycopg2. Writes (sub-trajectory |
| 23 | +append, temporal-property creation) remain on the hand-written path until the |
| 24 | +catalog's input-function shapes are confirmed against a live MEOS. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +from typing import Any, Protocol, runtime_checkable |
| 30 | + |
| 31 | +from fastapi import APIRouter, HTTPException, Request |
| 32 | + |
| 33 | +#: OGC derived measure → the MEOS catalog function that computes it. |
| 34 | +DERIVED_FN = {"velocity": "tpoint_speed", "distance": "tpoint_cumulative_length"} |
| 35 | +#: MEOS serialiser used to export a temporal value as MF-JSON. |
| 36 | +EXPORT_FN = "temporal_as_mfjson" |
| 37 | + |
| 38 | + |
| 39 | +@runtime_checkable |
| 40 | +class FeatureStore(Protocol): |
| 41 | + """Database port. Values are MF-JSON (the wire shape the codec decodes). |
| 42 | +
|
| 43 | + Production wires this to PyMEOS + psycopg2; tests pass a stub. |
| 44 | + """ |
| 45 | + |
| 46 | + def get_trajectory(self, cid: str, fid: str) -> Any | None: ... |
| 47 | + def get_property(self, cid: str, fid: str, name: str) -> Any | None: ... |
| 48 | + |
| 49 | + |
| 50 | +router = APIRouter() |
| 51 | + |
| 52 | + |
| 53 | +def _ctx(request: Request): |
| 54 | + state = request.app.state |
| 55 | + store = getattr(state, "feature_store", None) |
| 56 | + if store is None: |
| 57 | + raise HTTPException(501, "no FeatureStore is wired into the app") |
| 58 | + return state.dispatcher, state.codec, store |
| 59 | + |
| 60 | + |
| 61 | +def _dispatch_unary(dispatcher, codec, fn: str, value_mfjson: Any) -> Any: |
| 62 | + """Decode an MF-JSON temporal value, dispatch a single-temporal-argument |
| 63 | + MEOS function, and re-encode the result as MF-JSON.""" |
| 64 | + if not dispatcher.has(fn): |
| 65 | + raise HTTPException(501, f"MEOS function `{fn}` is not in the vendored catalog") |
| 66 | + sig = dispatcher.signature(fn) |
| 67 | + if not sig.params: |
| 68 | + raise HTTPException(500, f"MEOS function `{fn}` has no parameters to dispatch") |
| 69 | + pname = sig.params[0]["name"] |
| 70 | + arg = codec.decode("mfjson", value_mfjson) |
| 71 | + result = dispatcher.dispatch(fn, {pname: arg}) |
| 72 | + return codec.encode("mfjson", result) |
| 73 | + |
| 74 | + |
| 75 | +def _temporal_property(name: str, type_token: str, mfjson: Any, self_href: str) -> dict: |
| 76 | + """Shape a MEOS MF-JSON temporal value as an OGC ``temporalProperty``.""" |
| 77 | + seq = mfjson if isinstance(mfjson, list) else [mfjson] |
| 78 | + return { |
| 79 | + "name": name, |
| 80 | + "type": type_token, |
| 81 | + "valueSequence": seq, |
| 82 | + "links": [{"rel": "self", "href": self_href}], |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +@router.get("/collections/{cid}/items/{fid}/tgsequence", summary="Temporal geometry (MF-JSON)") |
| 87 | +def get_tgsequence(cid: str, fid: str, request: Request) -> Any: |
| 88 | + dispatcher, codec, store = _ctx(request) |
| 89 | + trip = store.get_trajectory(cid, fid) |
| 90 | + if trip is None: |
| 91 | + raise HTTPException(404, "feature not found") |
| 92 | + return _dispatch_unary(dispatcher, codec, EXPORT_FN, trip) |
| 93 | + |
| 94 | + |
| 95 | +@router.get( |
| 96 | + "/collections/{cid}/items/{fid}/tgsequence/{tg}/{measure}", |
| 97 | + summary="Derived temporal-geometry query: velocity | distance (acceleration → 501)", |
| 98 | +) |
| 99 | +def get_derived(cid: str, fid: str, tg: str, measure: str, request: Request) -> dict: |
| 100 | + if measure == "acceleration": |
| 101 | + raise HTTPException( |
| 102 | + 501, |
| 103 | + "acceleration is not derivable: linearly interpolated position gives a " |
| 104 | + "piecewise-constant speed, whose derivative is zero within each segment " |
| 105 | + "and undefined at the vertices", |
| 106 | + ) |
| 107 | + fn = DERIVED_FN.get(measure) |
| 108 | + if fn is None: |
| 109 | + raise HTTPException(404, f"unknown temporal-geometry query: {measure}") |
| 110 | + dispatcher, codec, store = _ctx(request) |
| 111 | + trip = store.get_trajectory(cid, fid) |
| 112 | + if trip is None: |
| 113 | + raise HTTPException(404, "feature not found") |
| 114 | + out = _dispatch_unary(dispatcher, codec, fn, trip) |
| 115 | + return _temporal_property(measure, "TReal", out, request.url.path) |
| 116 | + |
| 117 | + |
| 118 | +@router.get( |
| 119 | + "/collections/{cid}/items/{fid}/tproperties/{name}", |
| 120 | + summary="A stored temporal property as an OGC temporalProperty", |
| 121 | +) |
| 122 | +def get_tproperty(cid: str, fid: str, name: str, request: Request) -> dict: |
| 123 | + dispatcher, codec, store = _ctx(request) |
| 124 | + value = store.get_property(cid, fid, name) |
| 125 | + if value is None: |
| 126 | + raise HTTPException(404, f"unknown temporal property: {name}") |
| 127 | + out = _dispatch_unary(dispatcher, codec, EXPORT_FN, value) |
| 128 | + return _temporal_property(name, "TReal", out, request.url.path) |
0 commit comments