|
| 1 | +import type { DetourPaintPiece, IntegratedRoute, LatLng } from "./pinkLineRoute"; |
| 2 | + |
| 3 | +/** GeoJSON position order in exported JSON (RFC 7946). Internal `LatLng` remains [lat, lng]. */ |
| 4 | +export type LngLat = [number, number]; |
| 5 | + |
| 6 | +export const COLAB_ROUTE_GEOMETRY_EXPORT_VERSION = 1 as const; |
| 7 | + |
| 8 | +export interface ColabRouteGeometryBundle { |
| 9 | + detour_export_version: typeof COLAB_ROUTE_GEOMETRY_EXPORT_VERSION; |
| 10 | + integrated_route: { |
| 11 | + solid: Array<{ coordinates: LngLat[] }>; |
| 12 | + removed: Array<{ coordinates: LngLat[] }>; |
| 13 | + }; |
| 14 | + detour_paint: { |
| 15 | + road: Array<{ coordinates: LngLat[] }>; |
| 16 | + offroad: Array<{ road_end: LngLat; target: LngLat }>; |
| 17 | + junctions: LngLat[]; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +function toLngLat([lat, lng]: LatLng): LngLat { |
| 22 | + return [lng, lat]; |
| 23 | +} |
| 24 | + |
| 25 | +function ringLngLat(ring: LatLng[]): LngLat[] { |
| 26 | + return ring.map(toLngLat); |
| 27 | +} |
| 28 | + |
| 29 | +function collectJunctionsFromPaint(pieces: DetourPaintPiece[]): LngLat[] { |
| 30 | + const out: LngLat[] = []; |
| 31 | + for (const p of pieces) { |
| 32 | + if (p.kind === "offroad") { |
| 33 | + out.push(toLngLat(p.roadEnd)); |
| 34 | + } |
| 35 | + } |
| 36 | + return out; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Serializes the same `IntegratedRoute` snapshot used for map painting after routing. |
| 41 | + * When `detourPaint` is absent, proposed geometry is taken from `dashed` as road polylines. |
| 42 | + */ |
| 43 | +export function serializeIntegratedRouteToColabBundle( |
| 44 | + route: IntegratedRoute |
| 45 | +): ColabRouteGeometryBundle { |
| 46 | + const solid = route.solid.map((ring) => ({ coordinates: ringLngLat(ring) })); |
| 47 | + const removed = route.removed.map((ring) => ({ coordinates: ringLngLat(ring) })); |
| 48 | + |
| 49 | + const road: Array<{ coordinates: LngLat[] }> = []; |
| 50 | + const offroad: Array<{ road_end: LngLat; target: LngLat }> = []; |
| 51 | + |
| 52 | + if (route.detourPaint && route.detourPaint.length > 0) { |
| 53 | + for (const piece of route.detourPaint) { |
| 54 | + if (piece.kind === "road") { |
| 55 | + road.push({ coordinates: ringLngLat(piece.points) }); |
| 56 | + } else { |
| 57 | + offroad.push({ |
| 58 | + road_end: toLngLat(piece.roadEnd), |
| 59 | + target: toLngLat(piece.target), |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + } else { |
| 64 | + for (const seg of route.dashed) { |
| 65 | + if (seg.length >= 2) road.push({ coordinates: ringLngLat(seg) }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const junctions = |
| 70 | + route.detourPaint && route.detourPaint.length > 0 |
| 71 | + ? collectJunctionsFromPaint(route.detourPaint) |
| 72 | + : []; |
| 73 | + |
| 74 | + return { |
| 75 | + detour_export_version: COLAB_ROUTE_GEOMETRY_EXPORT_VERSION, |
| 76 | + integrated_route: { solid, removed }, |
| 77 | + detour_paint: { road, offroad, junctions }, |
| 78 | + }; |
| 79 | +} |
0 commit comments