trails/packages/gpx/src/generate.ts
Ullrich Schäfer 2cfa5e54e7
Implement shared packages (tasks 2.1-2.6)
- @trails-cool/types: Route, Activity, Waypoint, RouteVersion, RouteMetadata
- @trails-cool/gpx: GPX parser (XML→waypoints/tracks/elevation) and generator
  with round-trip test, haversine distance, elevation gain/loss computation
- @trails-cool/map: MapView (Leaflet + OSM/OpenTopoMap/CyclOSM layer switcher),
  RouteLayer (GeoJSON polyline rendering)
- @trails-cool/ui: Button (primary/secondary/ghost), Input (with label/error),
  Card components with Tailwind styling
- @trails-cool/i18n: react-i18next config with English + German translations,
  browser language detection, fallback to English

All 13 unit tests pass. Both apps build successfully.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 13:09:35 +01:00

60 lines
1.6 KiB
TypeScript

import type { Waypoint } from "@trails-cool/types";
import type { TrackPoint } from "./types";
/**
* Generate a GPX XML string from waypoints and track points.
*/
export function generateGpx(options: {
name?: string;
waypoints?: Waypoint[];
tracks?: TrackPoint[][];
}): string {
const lines: string[] = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<gpx version="1.1" creator="trails.cool"',
' xmlns="http://www.topografix.com/GPX/1/1">',
];
if (options.name) {
lines.push(" <metadata>", ` <name>${escapeXml(options.name)}</name>`, " </metadata>");
}
if (options.waypoints) {
for (const wpt of options.waypoints) {
lines.push(` <wpt lat="${wpt.lat}" lon="${wpt.lon}">`);
if (wpt.name) {
lines.push(` <name>${escapeXml(wpt.name)}</name>`);
}
lines.push(" </wpt>");
}
}
if (options.tracks) {
for (const track of options.tracks) {
lines.push(" <trk>", " <trkseg>");
for (const pt of track) {
lines.push(` <trkpt lat="${pt.lat}" lon="${pt.lon}">`);
if (pt.ele !== undefined) {
lines.push(` <ele>${pt.ele}</ele>`);
}
if (pt.time) {
lines.push(` <time>${escapeXml(pt.time)}</time>`);
}
lines.push(" </trkpt>");
}
lines.push(" </trkseg>", " </trk>");
}
}
lines.push("</gpx>");
return lines.join("\n");
}
function escapeXml(str: string): string {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");
}