geo_file_exporters.js (4003B)
1 import utf8 from 'utf8'; 2 import escapeHtml from 'escape-html'; 3 import {saveNktk} from './parsers/nktk'; 4 5 function saveGpx(segments, name, points, withElevations = false) { 6 const gpx = []; 7 const fakeTime = '1970-01-01T00:00:01.000Z'; 8 const creationTime = new Date().toISOString(); 9 10 gpx.push('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>'); 11 gpx.push( 12 '<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="http://nakarte.me" ' + 13 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 ' + 14 'http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1">' 15 ); 16 17 gpx.push('\t<metadata>'); 18 gpx.push(`\t\t<time>${creationTime}</time>`); 19 gpx.push('\t</metadata>'); 20 21 points.forEach(function(marker) { 22 var label = marker.label; 23 label = escapeHtml(label); 24 label = utf8.encode(label); 25 gpx.push(`\t<wpt lat="${marker.latlng.lat.toFixed(6)}" lon="${marker.latlng.lng.toFixed(6)}">`); 26 if (withElevations && marker.latlng.alt !== null) { 27 gpx.push(`\t\t<ele>${marker.latlng.alt.toFixed(1)}</ele>`); 28 } 29 gpx.push(`\t\t<name>${label}</name>`); 30 gpx.push('\t</wpt>'); 31 } 32 ); 33 if (segments.length > 0) { 34 name = name || 'Track'; 35 name = escapeHtml(name); 36 name = utf8.encode(name); 37 gpx.push('\t<trk>'); 38 gpx.push('\t\t<name>' + name + '</name>'); 39 40 for (let segment of segments) { 41 gpx.push('\t\t<trkseg>'); 42 for (let point of segment) { 43 let x = point.lng.toFixed(6); 44 let y = point.lat.toFixed(6); 45 const elevation = (withElevations && point.alt !== null) 46 ? `<ele>${point.alt.toFixed(1)}</ele>` : ''; 47 // time element is not necessary, added for compatibility to Garmin Connect only 48 gpx.push(`\t\t\t<trkpt lat="${y}" lon="${x}">${elevation}<time>${fakeTime}</time></trkpt>`); 49 } 50 gpx.push('\t\t</trkseg>'); 51 } 52 gpx.push('\t</trk>'); 53 } 54 gpx.push('</gpx>'); 55 return gpx.join('\n'); 56 } 57 58 function saveGpxWithElevations(segments, name, points) { 59 return saveGpx(segments, name, points, true); 60 } 61 62 function saveKml(segments, name, points) { 63 const kml = []; 64 65 name = name || 'Track'; 66 name = escapeHtml(name); 67 name = utf8.encode(name); 68 69 kml.push('<?xml version="1.0" encoding="UTF-8"?>'); 70 kml.push('<kml xmlns="http://www.opengis.net/kml/2.2">'); 71 kml.push('\t<Document>'); 72 kml.push(`\t\t<name>${name}</name>`); 73 74 for (let [i, segment] of segments.entries()) { 75 kml.push('\t\t<Placemark>'); 76 kml.push(`\t\t\t<name>Line ${(i + 1)}</name>`); 77 kml.push('\t\t\t<LineString>'); 78 kml.push('\t\t\t\t<tessellate>1</tessellate>'); 79 kml.push('\t\t\t\t<coordinates>'); 80 81 for (let point of segment) { 82 let x = point.lng.toFixed(6); 83 let y = point.lat.toFixed(6); 84 kml.push(`\t\t\t\t\t${x},${y}`); 85 } 86 87 kml.push('\t\t\t\t</coordinates>'); 88 kml.push('\t\t\t</LineString>'); 89 kml.push('\t\t</Placemark>'); 90 } 91 92 points.forEach(function(marker) { 93 var label = marker.label; 94 label = escapeHtml(label); 95 label = utf8.encode(label); 96 var coordinates = marker.latlng.lng.toFixed(6) + ',' + marker.latlng.lat.toFixed(6) + ',0'; 97 98 kml.push('\t\t<Placemark>'); 99 kml.push('\t\t\t<name>' + label + '</name>'); 100 kml.push('\t\t\t<Point>'); 101 kml.push('\t\t\t\t<coordinates>' + coordinates + '</coordinates>'); 102 kml.push('\t\t\t</Point>'); 103 kml.push('\t\t</Placemark>'); 104 } 105 ); 106 107 kml.push('\t</Document>'); 108 kml.push('\t</kml>'); 109 110 return kml.join('\n'); 111 } 112 113 export {saveGpx, saveGpxWithElevations, saveKml, saveNktk as saveToString}; 114