adds route editing
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import GPX from "$lib/models/gpx/gpx";
|
||||
import { Trail } from "$lib/models/trail";
|
||||
import { Waypoint } from "$lib/models/waypoint";
|
||||
import { kml, tcx } from "$lib/vendor/toGeoJSON/toGeoJSON"
|
||||
import GeoJsonToGpx from "$lib/vendor/geoJSONToGPX"
|
||||
import { browser } from "$app/environment";
|
||||
import GeoJsonToGpx from "$lib/vendor/geoJSONToGPX";
|
||||
import { kml, tcx } from "$lib/vendor/toGeoJSON/toGeoJSON";
|
||||
|
||||
function calculateDistance(lat1: number, lon1: number, lat2: number, lon2: number): number {
|
||||
const R = 6371; // Radius of the Earth in km
|
||||
@@ -17,38 +17,23 @@ function calculateDistance(lat1: number, lon1: number, lat2: number, lon2: numbe
|
||||
return distance;
|
||||
}
|
||||
|
||||
export async function gpx2trail(gpx: string) {
|
||||
let xml;
|
||||
if (browser) {
|
||||
const parser = new DOMParser();
|
||||
xml = parser.parseFromString(gpx, "application/xml")
|
||||
} else {
|
||||
const JSDOM = (await import("jsdom")).JSDOM
|
||||
xml = new JSDOM(gpx).window.document
|
||||
export async function gpx2trail(gpxString: string) {
|
||||
const gpx = await GPX.parse(gpxString);
|
||||
|
||||
if (gpx instanceof Error) {
|
||||
throw gpx;
|
||||
}
|
||||
|
||||
const trail = new Trail("");
|
||||
|
||||
let name = xml.getElementsByTagName('name');
|
||||
if (name.length > 0) {
|
||||
trail.name = name[0].textContent ?? "";
|
||||
}
|
||||
let desc = xml.getElementsByTagName('desc');
|
||||
if (desc.length > 0) {
|
||||
trail.description = desc[0].textContent ?? "";
|
||||
}
|
||||
trail.name = gpx.metadata?.name ?? "";
|
||||
|
||||
const el = xml.getElementsByTagName('wpt');
|
||||
const elLength = el.length
|
||||
for (let i = 0; i < elLength; i++) {
|
||||
const wp = new Waypoint(parseFloat(el[i].getAttribute('lat')!), parseFloat(el[i].getAttribute('lon')!));
|
||||
|
||||
let nameEl = el[i].getElementsByTagName('name');
|
||||
wp.name = nameEl.length > 0 ? nameEl[0].textContent ?? "" : '';
|
||||
|
||||
let descEl = el[i].getElementsByTagName('desc');
|
||||
wp.description = descEl.length > 0 ? descEl[0].textContent ?? "" : '';
|
||||
trail.description = gpx.metadata?.desc;
|
||||
|
||||
for (const wpt of gpx.wpt ?? []) {
|
||||
const wp = new Waypoint(wpt.$.lat ?? 0, wpt.$.lon ?? 0);
|
||||
wp.name = wp.name
|
||||
wp.description = wp.description;
|
||||
trail.expand.waypoints.push(wp);
|
||||
}
|
||||
|
||||
@@ -56,17 +41,13 @@ export async function gpx2trail(gpx: string) {
|
||||
let totalDuration = 0;
|
||||
let totalDistance = 0;
|
||||
|
||||
const tracks = xml.getElementsByTagName("trk")
|
||||
for (const track of tracks) {
|
||||
const segments = track.getElementsByTagName("trkseg")
|
||||
for (const segment of segments) {
|
||||
const points = segment.getElementsByTagName("trkpt")
|
||||
for (const track of gpx.trk ?? []) {
|
||||
for (const segment of track.trkseg ?? []) {
|
||||
const points = segment.trkpt ?? [];
|
||||
|
||||
if (points.length >= 2) {
|
||||
const startTimeString = points[0].querySelector('time')?.textContent;
|
||||
const startTime = startTimeString ? new Date(startTimeString) : undefined;
|
||||
const endTimeString = points[points.length - 1].querySelector('time')?.textContent;
|
||||
const endTime = endTimeString ? new Date(endTimeString) : undefined;
|
||||
const startTime = points[0].time;
|
||||
const endTime = points[points.length - 1].time
|
||||
|
||||
if (startTime && endTime) {
|
||||
totalDuration += endTime.getTime() - startTime.getTime();
|
||||
@@ -79,30 +60,30 @@ export async function gpx2trail(gpx: string) {
|
||||
|
||||
const pointLength = points.length
|
||||
for (let i = 1; i < pointLength; i++) {
|
||||
const elevation = parseFloat(points[i].querySelector('ele')?.textContent || '0')
|
||||
const previousElevation = parseFloat(points[i - 1].querySelector('ele')?.textContent || '0')
|
||||
const prevPoint = points[i - 1];
|
||||
const point = points[i];
|
||||
const elevation = point.ele ?? 0
|
||||
const previousElevation = prevPoint.ele ?? 0
|
||||
const elevationDiff = elevation - previousElevation;
|
||||
if (elevationDiff > 0) {
|
||||
totalElevationGain += elevationDiff;
|
||||
}
|
||||
|
||||
const prevPoint = points[i - 1];
|
||||
const point = points[i];
|
||||
const distance = calculateDistance(
|
||||
parseFloat(prevPoint.getAttribute("lat") || '0'),
|
||||
parseFloat(prevPoint.getAttribute("lon") || '0'),
|
||||
parseFloat(point.getAttribute("lat") || '0'),
|
||||
parseFloat(point.getAttribute("lon") || '0')
|
||||
prevPoint.$.lat ?? 0,
|
||||
prevPoint.$.lon ?? 0,
|
||||
point.$.lat ?? 0,
|
||||
point.$.lon ?? 0,
|
||||
);
|
||||
totalDistance += distance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const startPoint = xml.getElementsByTagName("trk")[0].getElementsByTagName("trkseg")[0].getElementsByTagName("trkpt")[0];
|
||||
const startPoint = gpx.trk?.at(0)?.trkseg?.at(0)?.trkpt?.at(0);
|
||||
if (startPoint) {
|
||||
trail.lat = parseFloat(startPoint.getAttribute('lat')!)
|
||||
trail.lon = parseFloat(startPoint.getAttribute('lon')!)
|
||||
trail.lat = startPoint.$.lat
|
||||
trail.lon = startPoint.$.lon
|
||||
}
|
||||
|
||||
trail.duration = totalDuration / 1000 / 60
|
||||
|
||||
Reference in New Issue
Block a user