change trackpoint format to match server expectation
Sonarqube Scanner / Build and analyze (push) Successful in 1m9s

This commit is contained in:
Florian Zumpe
2026-09-27 19:25:04 +02:00
parent b032940681
commit 3cdad9caa5
6 changed files with 258 additions and 58 deletions
+56 -16
View File
@@ -153,33 +153,64 @@ globalThis.Wegwichtel = globalThis.Wegwichtel || {};
nameElement.textContent = name;
}
function syncGpx(gpx, route, pois) {
const document = parseXml(gpx || emptyGpx(route?.name));
function ensureTrackSegment(document, routeName) {
const root = document.documentElement;
updateMetadataName(document, route?.name || 'Neue Strecke');
Array.from(document.getElementsByTagNameNS('*', 'wpt')).forEach(node => node.remove());
let track = directChildrenByLocalName(root, 'trk')[0];
if (!track) {
track = createElement(document, 'trk');
root.appendChild(track);
}
let trackName = directChildrenByLocalName(track, 'name')[0];
if (!trackName) {
trackName = createElement(document, 'name');
track.insertBefore(trackName, track.firstChild);
}
trackName.textContent = routeName;
let segment = directChildrenByLocalName(track, 'trkseg')[0];
if (!segment) {
segment = createElement(document, 'trkseg');
track.appendChild(segment);
}
return segment;
}
function isManagedTrackPoint(node) {
return Boolean(firstDescendantByLocalName(node, 'poiId'));
}
function syncGpx(gpx, route, pois) {
const routeName = route?.name || 'Neue Strecke';
const document = parseXml(gpx || emptyGpx(routeName));
updateMetadataName(document, routeName);
// Wegwichtel verwaltet neue Streckenpunkte direkt als GPX-Trackpunkte.
// Fremde/importierte Trackpunkte ohne Wegwichtel-ID bleiben erhalten.
Array.from(document.getElementsByTagNameNS('*', 'wpt')).forEach(node => node.remove());
Array.from(document.getElementsByTagNameNS('*', 'trkpt'))
.filter(isManagedTrackPoint)
.forEach(node => node.remove());
const segment = ensureTrackSegment(document, routeName);
const orderedPois = [...(pois || [])].sort((left, right) => Number(left.sequence) - Number(right.sequence));
orderedPois.forEach((poi, index) => {
const title = poi.title || `Station ${index + 1}`;
const title = poi.title || `Trackpunkt ${index + 1}`;
if (!Number.isFinite(Number(poi.lat)) || !Number.isFinite(Number(poi.lon))) {
throw new TypeError(`Für ${title} fehlen gültige Koordinaten.`);
}
const waypoint = createElement(document, 'wpt');
waypoint.setAttribute('lat', String(Number(poi.lat)));
waypoint.setAttribute('lon', String(Number(poi.lon)));
waypoint.appendChild(createElement(document, 'name', title));
if (poi.description) waypoint.appendChild(createElement(document, 'desc', poi.description));
const trackPoint = createElement(document, 'trkpt');
trackPoint.setAttribute('lat', String(Number(poi.lat)));
trackPoint.setAttribute('lon', String(Number(poi.lon)));
trackPoint.appendChild(createElement(document, 'name', title));
if (poi.description) trackPoint.appendChild(createElement(document, 'desc', poi.description));
const extensions = createElement(document, 'extensions');
const poiId = document.createElementNS(POI_NAMESPACE, 'wegwichtel:poiId');
poiId.textContent = String(poi.id);
extensions.appendChild(poiId);
waypoint.appendChild(extensions);
const routeOrTrack = directChildrenByLocalName(root, 'rte')[0]
|| directChildrenByLocalName(root, 'trk')[0]
|| null;
if (routeOrTrack) routeOrTrack.before(waypoint);
else root.appendChild(waypoint);
trackPoint.appendChild(extensions);
segment.appendChild(trackPoint);
});
return new XMLSerializer().serializeToString(document);
@@ -292,6 +323,14 @@ globalThis.Wegwichtel = globalThis.Wegwichtel || {};
}).filter(point => point.lat != null && point.lon != null);
}
function gpxForUpload(routePackage) {
if (!routePackage?.gpx) throw new Error('Für die lokale Strecke ist keine GPX-Datei vorhanden.');
if (!routePoints(routePackage.gpx).length) {
throw new Error('Die lokale Strecke enthält noch keine GPX-Trackpunkte.');
}
return routePackage.gpx;
}
function radians(value) {
return value * (Math.PI / 180);
}
@@ -437,6 +476,7 @@ globalThis.Wegwichtel = globalThis.Wegwichtel || {};
fileToDataUrl,
get: readPackage,
getAppRoute,
gpxForUpload,
list: listPackages,
listAppRoutes,
parseWaypoints,