adds trail export

This commit is contained in:
Christian Beutel
2024-05-05 15:46:17 +02:00
parent 930feda4d6
commit 756a9fa79f
18 changed files with 310 additions and 31 deletions

View File

@@ -17,3 +17,14 @@ export function readAsDataURLAsync(file: File) {
fr.readAsDataURL(file);
});
}
export function saveAs(data: Blob, fileName: string) {
var a = document.createElement("a") as HTMLAnchorElement;
a.setAttribute("style", "display: none");
document.body.appendChild(a);
const url = window.URL.createObjectURL(data);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};

View File

@@ -1,9 +1,11 @@
import GPX from "$lib/models/gpx/gpx";
import { Trail } from "$lib/models/trail";
import { Waypoint } from "$lib/models/waypoint";
import { currentUser } from "$lib/stores/user_store";
import GeoJsonToGpx from "$lib/vendor/geoJSONToGPX";
import { kml, tcx } from "$lib/vendor/toGeoJSON/toGeoJSON";
import cryptoRandomString from "crypto-random-string";
import { get } from "svelte/store";
export async function gpx2trail(gpxString: string) {
const gpx = await GPX.parse(gpxString);
@@ -18,7 +20,7 @@ export async function gpx2trail(gpxString: string) {
trail.description = gpx.metadata?.desc;
for (const wpt of gpx.wpt ?? []) {
for (const wpt of gpx.wpt ?? []) {
const wp = new Waypoint(wpt.$.lat ?? 0, wpt.$.lon ?? 0);
wp.id = cryptoRandomString({ length: 15 });
wp.name = wpt.name
@@ -29,13 +31,13 @@ export async function gpx2trail(gpxString: string) {
const totals = gpx.getTotals()
const trackPoints = gpx.trk?.at(0)?.trkseg?.at(0)?.trkpt
const routePoints = gpx.rte?.at(0)?.rtept;
const routePoints = gpx.rte?.at(0)?.rtept;
const startPoint = trackPoints?.at(0) ?? routePoints?.at(0);
const startPoint = trackPoints?.at(0) ?? routePoints?.at(0);
if (startPoint) {
trail.lat = startPoint.$.lat
trail.lon = startPoint.$.lon
}
}
const startTime = trackPoints?.at(0)?.time;
const endTime = trackPoints?.at((trackPoints?.length ?? 1) - 1)?.time
@@ -49,7 +51,44 @@ export async function gpx2trail(gpxString: string) {
trail.elevation_gain = totals.elevationGain;
trail.distance = totals.distance
return {gpx: gpx, trail: trail}
return { gpx: gpx, trail: trail }
}
export async function trail2gpx(trail: Trail) {
if (!trail.expand.gpx_data) {
throw Error("Trail has no GPX data")
}
const gpx = await GPX.parse(trail.expand.gpx_data) as GPX;
if (gpx instanceof Error) {
throw gpx;
}
gpx.metadata = {
name: trail.name,
desc: trail.description ?? "",
time: trail.date ? new Date(trail.date) : new Date(),
keywords: `${trail.category ?? ""}, ${trail.location ?? ""}`,
author: { name: trail.author ?? "", email: get(currentUser)?.email ?? "" }
}
if(!gpx.wpt) {
gpx.wpt = [];
}
for (const wp of trail.expand.waypoints) {
const gpxWpt = gpx.wpt.find((w) => w.$.lat == wp.lat && w.$.lon == wp.lon)
if(!gpxWpt) {
gpx.wpt.push({
$: {
lat: wp.lat,
lon: wp.lon
}
})
}
}
return gpx.toString();
}
export function fromKML(kmlData: string) {