import type { Waypoint } from "@trails-cool/types"; import type { TrackPoint } from "./types.ts"; /** * Generate a GPX XML string from waypoints and track points. */ export function generateGpx(options: { name?: string; waypoints?: Waypoint[]; tracks?: TrackPoint[][]; }): string { 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(" ", " "); } } lines.push(""); return lines.join("\n"); } function escapeXml(str: string): string { return str .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }