more file formats && list add in edit

This commit is contained in:
Christian Beutel
2024-03-27 18:43:46 +01:00
parent ded0ea4f80
commit 340a3c7c05
15 changed files with 1498 additions and 13 deletions

View File

@@ -1,8 +1,10 @@
import { Trail } from "$lib/models/trail";
import { Waypoint } from "$lib/models/waypoint";
import { JSDOM } from "jsdom"
import { kml, tcx } from "$lib/vendor/toGeoJSON/toGeoJSON"
import GeoJsonToGpx from "$lib/vendor/geoJSONToGPX"
export function gpx2trail(gpx: string) {
export async function gpx2trail(gpx: string) {
const JSDOM = (await import("jsdom")).JSDOM
const xml = new JSDOM(gpx).window.document
const trail = new Trail("");
@@ -15,7 +17,7 @@ export function gpx2trail(gpx: string) {
trail.description = desc[0].textContent ?? "";
}
const el = xml.getElementsByTagName('wpt');
const el = xml.getElementsByTagName('wpt');
for (let i = 0; i < el.length; i++) {
const wp = new Waypoint(parseFloat(el[i].getAttribute('lat')!), parseFloat(el[i].getAttribute('lon')!));
@@ -29,10 +31,45 @@ export function gpx2trail(gpx: string) {
}
const startPoint = xml.getElementsByTagName("trk")[0].getElementsByTagName("trkseg")[0].getElementsByTagName("trkpt")[0];
if(startPoint) {
if (startPoint) {
trail.lat = parseFloat(startPoint.getAttribute('lat')!)
trail.lon = parseFloat(startPoint.getAttribute('lon')!)
}
}
return trail
}
export function fromKML(kmlData: string) {
const geojson = kml(new DOMParser().parseFromString(kmlData, "text/xml"))
const options = {
metadata: {
name: '',
author: {
name: '',
link: {
href: ''
}
}
}
}
const gpx = GeoJsonToGpx(geojson as any, options)
return new XMLSerializer().serializeToString(gpx)
}
export function fromTCX(tcxData: string) {
const geojson = tcx(new DOMParser().parseFromString(tcxData, "text/xml"))
const options = {
metadata: {
name: '',
author: {
name: '',
link: {
href: ''
}
}
}
}
const gpx = GeoJsonToGpx(geojson as any, options)
return new XMLSerializer().serializeToString(gpx)
}