import type { AeroflyFlight, AeroflyNavRouteBase } from "@fboes/aerofly-custom-missions"; import { AeroflyFlightToStringConverter, type ExportFileConverterWaypointType, } from "./AeroflyFlightToStringConverter.js"; type KeyholeMarkupLanguageRouteStyle = { id: ExportFileConverterWaypointType | "aircraft"; iconHref: string; }; export class AeroflyFlightToKmlConverter extends AeroflyFlightToStringConverter { static readonly fileName = "Keyhole Markup Language"; static readonly fileExtension = "kml"; convert(flightplan: AeroflyFlight): string { const routeColor = "9314ff"; const styles: KeyholeMarkupLanguageRouteStyle[] = [ { id: "airport", iconHref: "https://maps.google.com/mapfiles/kml/shapes/airports.png" }, { id: "runway", iconHref: "https://maps.google.com/mapfiles/kml/shapes/target.png" }, { id: "waypoint", iconHref: "http://maps.google.com/mapfiles/kml/shapes/triangle.png" }, { id: "navaid", iconHref: "https://maps.google.com/mapfiles/kml/shapes/polygon.png" }, { id: "aircraft", iconHref: "https://maps.google.com/mapfiles/kml/shapes/airports.png" }, ]; return `\ ${this.xml(this.getFlightplanTitle(flightplan))} ${this.xml(this.getMissionBriefing(flightplan))} ${styles .map((style) => { return `\ `; }) .join("\n")} ${this.xml(this.getFlightplanTitle(flightplan))} #flightplan 1 1 absolute ${flightplan.navigation.waypoints.map((wp) => this.coordinatesToString(wp)).join("\n")} ${flightplan.navigation.waypoints .map((wp) => { const alt = this.getWaypointAltitude(wp); return `\ ${this.xml(wp.identifier)} #${this.getWaypointSimplifiedType(wp)} ${wp.longitude.toString()} ${wp.latitude.toString()} ${(alt ?? 0).toString()} 10000 70 0 ${alt ? "absolute" : "relativeToGround"} ${alt !== null ? "absolute" : "relativeToGround"} ${this.coordinatesToString(wp)} `; }) .join("\n")} `; } private xml(str: string): string { return str .replace(/&/g, "&") // must be first! .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } private coordinatesToString(wp: AeroflyNavRouteBase) { return [wp.longitude, wp.latitude, this.getWaypointAltitude(wp) ?? 0].join(","); } }