import Foundation enum GPXExporter { /// Writes the route to a temporary `.gpx` file and returns its URL. static func write(detail: RouteDetail) -> URL? { let xml = generate(detail: detail) let safeName = detail.name .replacingOccurrences(of: "/", with: "-") .trimmingCharacters(in: .whitespacesAndNewlines) let filename = safeName.isEmpty ? "tour" : safeName let url = FileManager.default.temporaryDirectory .appendingPathComponent("\(filename).gpx") do { try xml.write(to: url, atomically: true, encoding: .utf8) return url } catch { return nil } } static func generate(detail: RouteDetail) -> String { let trkpts = detail.gpsTrack.map { p -> String in if let alt = p.alt { return " \(alt)" } else { return " " } }.joined(separator: "\n") let safeName = detail.name .replacingOccurrences(of: "&", with: "&") .replacingOccurrences(of: "<", with: "<") .replacingOccurrences(of: ">", with: ">") return """ \(safeName) \(trkpts) """ } }