import type { Waypoint } from "@trails-cool/types"; import type { TrackPoint } from "./types.ts"; /** * Generate a GPX XML string from waypoints and track points. */ export interface NoGoArea { points: Array<{ lat: number; lon: number }>; } export function generateGpx(options: { name?: string; waypoints?: Waypoint[]; tracks?: TrackPoint[][]; noGoAreas?: NoGoArea[]; }): string { const hasExtensions = options.noGoAreas && options.noGoAreas.length > 0; const lines: string[] = [ '', '", ]; if (options.name) { lines.push(" ", ` ${escapeXml(options.name)}`, " "); } if (options.waypoints) { for (const wpt of options.waypoints) { lines.push(` `); if (wpt.name) { lines.push(` ${escapeXml(wpt.name)}`); } lines.push(" "); } } if (options.tracks) { for (const track of options.tracks) { lines.push(" ", " "); for (const pt of track) { lines.push(` `); if (pt.ele !== undefined) { lines.push(` ${pt.ele}`); } if (pt.time) { lines.push(` `); } lines.push(" "); } lines.push(" ", " "); } } if (options.noGoAreas && options.noGoAreas.length > 0) { lines.push(" ", " "); for (const area of options.noGoAreas) { lines.push(" "); for (const pt of area.points) { lines.push(` `); } lines.push(" "); } lines.push(" ", " "); } lines.push(""); return lines.join("\n"); } function escapeXml(str: string): string { return str .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }