2024-04-25 16:41:06 +02:00
|
|
|
import type { Coordinates } from "gpx";
|
2024-04-24 19:32:55 +02:00
|
|
|
import { TrackPoint } from "gpx";
|
2024-04-25 16:41:06 +02:00
|
|
|
import { get, writable } from "svelte/store";
|
|
|
|
import { _ } from "svelte-i18n";
|
|
|
|
|
|
|
|
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'
|
|
|
|
};
|
|
|
|
export const routingProfile = writable({
|
|
|
|
value: 'bike',
|
|
|
|
label: get(_)('toolbar.routing.activities.bike')
|
|
|
|
});
|
|
|
|
export const routing = writable(true);
|
|
|
|
export const privateRoads = writable(false);
|
|
|
|
|
|
|
|
export function route(points: Coordinates[]): Promise<TrackPoint[]> {
|
|
|
|
if (get(routing)) {
|
|
|
|
return getRoute(points, brouterProfiles[get(routingProfile).value], get(privateRoads));
|
2024-04-23 18:36:16 +02:00
|
|
|
} else {
|
|
|
|
return new Promise((resolve) => {
|
2024-04-24 19:32:55 +02:00
|
|
|
resolve(points.map(point => new TrackPoint({
|
|
|
|
attributes: {
|
|
|
|
lat: point.lat,
|
|
|
|
lon: point.lon
|
|
|
|
}
|
|
|
|
})));
|
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[]> {
|
|
|
|
let url = `https://routing.gpx.studio?lonlats=${points.map(point => `${point.lon},${point.lat}`).join('|')}&profile=${brouterProfile + (privateRoads ? '-private' : '')}&format=geojson&alternativeidx=0`;
|
|
|
|
|
|
|
|
let response = await fetch(url);
|
|
|
|
let geojson = await response.json();
|
|
|
|
|
|
|
|
let route: TrackPoint[] = [];
|
|
|
|
let coordinates = geojson.features[0].geometry.coordinates;
|
|
|
|
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
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return route;
|
2024-04-23 18:36:16 +02:00
|
|
|
}
|