2024-04-25 16:41:06 +02:00
|
|
|
import type { Coordinates } from "gpx";
|
2024-05-09 00:02:27 +02:00
|
|
|
import { TrackPoint, distance } from "gpx";
|
2024-04-25 16:41:06 +02:00
|
|
|
import { get, writable } from "svelte/store";
|
2024-05-04 15:10:30 +02:00
|
|
|
import { settings } from "$lib/db";
|
2024-04-25 16:41:06 +02:00
|
|
|
import { _ } from "svelte-i18n";
|
2024-05-09 00:02:27 +02:00
|
|
|
import { map } from "$lib/stores";
|
2024-04-25 16:41:06 +02:00
|
|
|
|
2024-05-04 15:10:30 +02:00
|
|
|
const { routing, routingProfile, privateRoads } = settings;
|
|
|
|
|
2024-04-25 16:41:06 +02:00
|
|
|
export const brouterProfiles: { [key: string]: string } = {
|
|
|
|
bike: 'Trekking-dry',
|
|
|
|
racing_bike: 'fastbike',
|
|
|
|
mountain_bike: 'MTB',
|
|
|
|
foot: 'Hiking-Alpine-SAC6',
|
|
|
|
motorcycle: 'Car-FastEco',
|
|
|
|
water: 'river',
|
|
|
|
railway: 'rail'
|
|
|
|
};
|
2024-05-04 15:10:30 +02:00
|
|
|
export const routingProfileSelectItem = writable({
|
2024-04-25 16:41:06 +02:00
|
|
|
value: 'bike',
|
|
|
|
label: get(_)('toolbar.routing.activities.bike')
|
|
|
|
});
|
2024-05-04 15:10:30 +02:00
|
|
|
routingProfile.subscribe((value) => {
|
|
|
|
if (value !== get(routingProfileSelectItem).value) {
|
|
|
|
routingProfileSelectItem.update((item) => {
|
|
|
|
item.value = value;
|
|
|
|
item.label = get(_)(`toolbar.routing.activities.${value}`);
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
routingProfileSelectItem.subscribe((item) => {
|
|
|
|
if (item.value !== get(routingProfile)) {
|
|
|
|
routingProfile.set(item.value);
|
|
|
|
}
|
|
|
|
});
|
2024-04-25 16:41:06 +02:00
|
|
|
|
|
|
|
export function route(points: Coordinates[]): Promise<TrackPoint[]> {
|
|
|
|
if (get(routing)) {
|
2024-05-04 15:17:44 +02:00
|
|
|
return getRoute(points, brouterProfiles[get(routingProfile)], get(privateRoads));
|
2024-04-23 18:36:16 +02:00
|
|
|
} else {
|
2024-05-09 00:02:27 +02:00
|
|
|
return getIntermediatePoints(points);
|
2024-04-23 18:36:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
async function getRoute(points: Coordinates[], brouterProfile: string, privateRoads: boolean): Promise<TrackPoint[]> {
|
2024-04-25 19:02:34 +02:00
|
|
|
let url = `https://routing.gpx.studio?lonlats=${points.map(point => `${point.lon.toFixed(8)},${point.lat.toFixed(8)}`).join('|')}&profile=${brouterProfile + (privateRoads ? '-private' : '')}&format=geojson&alternativeidx=0`;
|
2024-04-24 19:32:55 +02:00
|
|
|
|
|
|
|
let response = await fetch(url);
|
2024-04-27 11:16:59 +02:00
|
|
|
|
|
|
|
// Check if the response is ok
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`${await response.text()}`);
|
|
|
|
}
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
let geojson = await response.json();
|
|
|
|
|
|
|
|
let route: TrackPoint[] = [];
|
|
|
|
let coordinates = geojson.features[0].geometry.coordinates;
|
2024-04-26 22:35:42 +02:00
|
|
|
let messages = geojson.features[0].properties.messages;
|
|
|
|
|
|
|
|
const lngIdx = messages[0].indexOf("Longitude");
|
|
|
|
const latIdx = messages[0].indexOf("Latitude");
|
|
|
|
const tagIdx = messages[0].indexOf("WayTags");
|
|
|
|
let messageIdx = 1;
|
|
|
|
let surface = getSurface(messages[messageIdx][tagIdx]);
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
for (let i = 0; i < coordinates.length; i++) {
|
|
|
|
let coord = coordinates[i];
|
|
|
|
route.push(new TrackPoint({
|
|
|
|
attributes: {
|
|
|
|
lat: coord[1],
|
|
|
|
lon: coord[0]
|
|
|
|
},
|
|
|
|
ele: coord[2] ?? undefined
|
|
|
|
}));
|
2024-04-26 22:35:42 +02:00
|
|
|
route[route.length - 1].setSurface(surface)
|
|
|
|
|
|
|
|
if (messageIdx < messages.length &&
|
|
|
|
coordinates[i][0] == Number(messages[messageIdx][lngIdx]) / 1000000 &&
|
|
|
|
coordinates[i][1] == Number(messages[messageIdx][latIdx]) / 1000000) {
|
|
|
|
messageIdx++;
|
|
|
|
|
2024-04-27 11:16:59 +02:00
|
|
|
if (messageIdx == messages.length) surface = "unknown";
|
2024-04-26 22:35:42 +02:00
|
|
|
else surface = getSurface(messages[messageIdx][tagIdx]);
|
|
|
|
}
|
2024-04-24 19:32:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return route;
|
2024-04-26 22:35:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSurface(message: string): string {
|
|
|
|
const fields = message.split(" ");
|
|
|
|
for (let i = 0; i < fields.length; i++) if (fields[i].startsWith("surface=")) {
|
|
|
|
return fields[i].substring(8);
|
|
|
|
}
|
|
|
|
return "unknown";
|
2024-05-09 00:02:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function getIntermediatePoints(points: Coordinates[]): Promise<TrackPoint[]> {
|
|
|
|
let route: TrackPoint[] = [];
|
|
|
|
let step = 0.05;
|
|
|
|
|
|
|
|
for (let i = 0; i < points.length - 1; i++) { // Add intermediate points between each pair of points
|
|
|
|
let dist = distance(points[i], points[i + 1]) / 1000;
|
|
|
|
for (let d = 0; d < dist; d += step) {
|
|
|
|
let lat = points[i].lat + d / dist * (points[i + 1].lat - points[i].lat);
|
|
|
|
let lon = points[i].lon + d / dist * (points[i + 1].lon - points[i].lon);
|
|
|
|
route.push(new TrackPoint({
|
|
|
|
attributes: {
|
|
|
|
lat: lat,
|
|
|
|
lon: lon
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
route.push(new TrackPoint({
|
|
|
|
attributes: {
|
|
|
|
lat: points[points.length - 1].lat,
|
|
|
|
lon: points[points.length - 1].lon
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
route.forEach((point) => {
|
|
|
|
point.setSurface("unknown");
|
|
|
|
point.ele = get(map)?.queryTerrainElevation(point.getCoordinates()) ?? undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise((resolve) => resolve(route));
|
|
|
|
}
|