|
| 1 | +function getName(xml) { |
| 2 | + return xml.querySelector('name')?.textContent |
| 3 | +} |
| 4 | + |
| 5 | +function getDescription(xml) { |
| 6 | + return xml.querySelector('desc')?.textContent |
| 7 | +} |
| 8 | + |
| 9 | +function getColor(xml) { |
| 10 | + return xml.querySelector('color')?.textContent |
| 11 | +} |
| 12 | + |
| 13 | +function getGpsTrack(xml) { |
| 14 | + const getChild = (obj, name) => { |
| 15 | + return obj.querySelector(name); |
| 16 | + } |
| 17 | + const queryFunc = (trkpt) => { |
| 18 | + return { |
| 19 | + timestamp: getChild(trkpt, 'time')?.textContent, |
| 20 | + value: [ |
| 21 | + trkpt.getAttribute('lat'), |
| 22 | + trkpt.getAttribute('lon'), |
| 23 | + getChild(trkpt, 'ele')?.textContent, |
| 24 | + ], |
| 25 | + } |
| 26 | + }; |
| 27 | + let track = xml.querySelector('trkseg'); |
| 28 | + console.debug('trkseg', track); |
| 29 | + if (!track) { return null } |
| 30 | + const trackArr = Object.values(track.children).map(queryFunc); |
| 31 | + console.debug('trackArr', trackArr); |
| 32 | + return trackArr; |
| 33 | +} |
| 34 | + |
| 35 | +function getWaypoint(xml) { |
| 36 | + const waypoint = xml.querySelector('wpt'); |
| 37 | + const timestamp = xml.querySelector('time'); |
| 38 | + if (!waypoint) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + return { |
| 42 | + timestamp: timestamp.textContent, |
| 43 | + value: [waypoint.getAttribute('lat'), waypoint.getAttribute('lon')], |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export async function parseXml(file) { |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + const rawFile = new XMLHttpRequest(); |
| 50 | + rawFile.onreadystatechange = () => { |
| 51 | + if (rawFile.readyState === 4 && (rawFile.status === 200 || rawFile.status === 0)) { |
| 52 | + const parser = new DOMParser(); |
| 53 | + const xml = parser.parseFromString(rawFile.response, 'text/xml'); |
| 54 | + |
| 55 | + const data = {}; |
| 56 | + data.name = getName(xml); |
| 57 | + data.description = getDescription(xml); |
| 58 | + data.color = getColor(xml); |
| 59 | + data.track = getGpsTrack(xml); |
| 60 | + data.waypoint = getWaypoint(xml); |
| 61 | + |
| 62 | + resolve(Object.fromEntries( |
| 63 | + Object.entries(data).filter(([_, v]) => !!v) |
| 64 | + )); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + rawFile.open('GET', file, false); |
| 69 | + rawFile.send(); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +function toGeoJSONBase(geometry, props) { |
| 75 | + const feature = {}; |
| 76 | + feature.type = 'Feature'; |
| 77 | + feature.geometry = geometry; |
| 78 | + feature.properties = props; |
| 79 | + return { |
| 80 | + type: 'FeatureCollection', |
| 81 | + features: [feature], |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export function toGeoJSONWaypoint(item, props = {}) { |
| 86 | + const geo = { |
| 87 | + type: 'Point', |
| 88 | + coordinates: [item.value[1], item.value[0]], |
| 89 | + }; |
| 90 | + return toGeoJSONBase(geo, props) |
| 91 | +} |
| 92 | + |
| 93 | +export function toGeoJSONTrack(item, props = {}) { |
| 94 | + const lineCoords = item.map((v) => [v.value[1], v.value[0]]); |
| 95 | + const geo = { |
| 96 | + type: 'LineString', |
| 97 | + coordinates: lineCoords, |
| 98 | + }; |
| 99 | + return toGeoJSONBase(geo, props) |
| 100 | +} |
0 commit comments