2024-04-24 19:32:55 +02:00
|
|
|
import type { Coordinates, GPXFile } from "gpx";
|
|
|
|
import { TrackPoint } from "gpx";
|
2024-04-23 14:11:05 +02:00
|
|
|
import mapboxgl from "mapbox-gl";
|
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
export function getMarker(coordinates: Coordinates, draggable: boolean = false): mapboxgl.Marker {
|
2024-04-23 18:36:16 +02:00
|
|
|
let element = document.createElement('div');
|
2024-04-23 19:33:11 +02:00
|
|
|
element.className = `h-3 w-3 rounded-full bg-background border-2 border-black cursor-pointer`;
|
2024-04-23 18:36:16 +02:00
|
|
|
return new mapboxgl.Marker({
|
|
|
|
draggable,
|
|
|
|
element
|
|
|
|
}).setLngLat(coordinates);
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
export type SimplifiedTrackPoint = { point: TrackPoint, index: number, distance?: number, segment?: number, zoom?: number };
|
2024-04-23 14:11:05 +02:00
|
|
|
|
|
|
|
export class AnchorPointHierarchy {
|
2024-04-24 19:32:55 +02:00
|
|
|
points: SimplifiedTrackPoint[];
|
2024-04-23 14:11:05 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
constructor() {
|
|
|
|
this.points = [];
|
|
|
|
}
|
2024-04-23 14:11:05 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
getMarkers(): mapboxgl.Marker[] {
|
|
|
|
let markers = [];
|
2024-04-24 19:32:55 +02:00
|
|
|
for (let point of this.points) {
|
|
|
|
let marker = getMarker(point.point.getCoordinates(), true);
|
|
|
|
Object.defineProperty(marker, '_simplified', { value: point });
|
|
|
|
markers.push(marker);
|
2024-04-23 14:11:05 +02:00
|
|
|
}
|
2024-04-23 16:20:47 +02:00
|
|
|
return markers;
|
2024-04-23 14:11:05 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
static create(file: GPXFile, epsilon: number = 50): AnchorPointHierarchy {
|
|
|
|
let hierarchy = new AnchorPointHierarchy();
|
|
|
|
|
|
|
|
let s = 0;
|
2024-04-23 14:11:05 +02:00
|
|
|
for (let track of file.getChildren()) {
|
|
|
|
for (let segment of track.getChildren()) {
|
|
|
|
let points = segment.trkpt;
|
2024-04-23 19:33:11 +02:00
|
|
|
let simplified = ramerDouglasPeucker(points, epsilon);
|
|
|
|
// Assign segment number to each point
|
|
|
|
simplified.forEach((point) => {
|
|
|
|
point.segment = s;
|
|
|
|
point.zoom = getZoomLevelForDistance(point.point.getLatitude(), point.distance);
|
2024-04-24 19:32:55 +02:00
|
|
|
hierarchy.points.push(point);
|
2024-04-23 19:33:11 +02:00
|
|
|
});
|
|
|
|
s++;
|
2024-04-23 14:11:05 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-23 19:33:11 +02:00
|
|
|
|
2024-04-23 14:11:05 +02:00
|
|
|
return hierarchy;
|
|
|
|
}
|
2024-04-23 19:33:11 +02:00
|
|
|
}
|
2024-04-23 14:11:05 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
function getZoomLevelForDistance(latitude: number, distance?: number): number {
|
|
|
|
if (distance === undefined) {
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-23 14:39:55 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
const rad = Math.PI / 180;
|
|
|
|
const lat = latitude * rad;
|
2024-04-23 14:11:05 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
return Math.min(20, Math.max(0, Math.floor(Math.log2((earthRadius * Math.cos(lat)) / distance))));
|
2024-04-23 14:11:05 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
function ramerDouglasPeucker(points: TrackPoint[], epsilon: number, start: number = 0, end: number = points.length - 1): SimplifiedTrackPoint[] {
|
2024-04-23 14:11:05 +02:00
|
|
|
let simplified = [{
|
|
|
|
point: points[start],
|
2024-04-23 19:33:11 +02:00
|
|
|
index: start,
|
|
|
|
|
2024-04-23 14:11:05 +02:00
|
|
|
}];
|
|
|
|
ramerDouglasPeuckerRecursive(points, epsilon, start, end, simplified);
|
|
|
|
simplified.push({
|
|
|
|
point: points[end],
|
|
|
|
index: end
|
|
|
|
});
|
|
|
|
return simplified;
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
function ramerDouglasPeuckerRecursive(points: TrackPoint[], epsilon: number, start: number, end: number, simplified: SimplifiedTrackPoint[]) {
|
2024-04-23 14:11:05 +02:00
|
|
|
let largest = {
|
|
|
|
index: 0,
|
|
|
|
distance: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = start + 1; i < end; i++) {
|
|
|
|
let distance = crossarc(points[start].getCoordinates(), points[end].getCoordinates(), points[i].getCoordinates());
|
|
|
|
if (distance > largest.distance) {
|
|
|
|
largest.index = i;
|
|
|
|
largest.distance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (largest.distance > epsilon) {
|
|
|
|
ramerDouglasPeuckerRecursive(points, epsilon, start, largest.index, simplified);
|
2024-04-23 19:33:11 +02:00
|
|
|
simplified.push({ point: points[largest.index], index: largest.index, distance: largest.distance });
|
2024-04-23 14:11:05 +02:00
|
|
|
ramerDouglasPeuckerRecursive(points, epsilon, largest.index, end, simplified);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const earthRadius = 6371008.8;
|
|
|
|
|
|
|
|
function crossarc(coord1: Coordinates, coord2: Coordinates, coord3: Coordinates): number {
|
|
|
|
// Calculates the shortest distance in meters
|
|
|
|
// between an arc (defined by p1 and p2) and a third point, p3.
|
|
|
|
// Input lat1,lon1,lat2,lon2,lat3,lon3 in degrees.
|
|
|
|
|
|
|
|
const rad = Math.PI / 180;
|
|
|
|
const lat1 = coord1.lat * rad;
|
|
|
|
const lat2 = coord2.lat * rad;
|
|
|
|
const lat3 = coord3.lat * rad;
|
|
|
|
|
|
|
|
const lon1 = coord1.lon * rad;
|
|
|
|
const lon2 = coord2.lon * rad;
|
|
|
|
const lon3 = coord3.lon * rad;
|
|
|
|
|
|
|
|
// Prerequisites for the formulas
|
|
|
|
const bear12 = bearing(lat1, lon1, lat2, lon2);
|
|
|
|
const bear13 = bearing(lat1, lon1, lat3, lon3);
|
|
|
|
let dis13 = distance(lat1, lon1, lat3, lon3);
|
|
|
|
|
|
|
|
let diff = Math.abs(bear13 - bear12);
|
|
|
|
if (diff > Math.PI) {
|
|
|
|
diff = 2 * Math.PI - diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is relative bearing obtuse?
|
|
|
|
if (diff > (Math.PI / 2)) {
|
|
|
|
return dis13;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the cross-track distance.
|
|
|
|
let dxt = Math.asin(Math.sin(dis13 / earthRadius) * Math.sin(bear13 - bear12)) * earthRadius;
|
|
|
|
|
|
|
|
// Is p4 beyond the arc?
|
|
|
|
let dis12 = distance(lat1, lon1, lat2, lon2);
|
|
|
|
let dis14 = Math.acos(Math.cos(dis13 / earthRadius) / Math.cos(dxt / earthRadius)) * earthRadius;
|
|
|
|
if (dis14 > dis12) {
|
|
|
|
return distance(lat2, lon2, lat3, lon3);
|
|
|
|
} else {
|
|
|
|
return Math.abs(dxt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function distance(latA: number, lonA: number, latB: number, lonB: number): number {
|
|
|
|
// Finds the distance between two lat / lon points.
|
|
|
|
return Math.acos(Math.sin(latA) * Math.sin(latB) + Math.cos(latA) * Math.cos(latB) * Math.cos(lonB - lonA)) * earthRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function bearing(latA: number, lonA: number, latB: number, lonB: number): number {
|
|
|
|
// Finds the bearing from one lat / lon point to another.
|
|
|
|
return Math.atan2(Math.sin(lonB - lonA) * Math.cos(latB),
|
|
|
|
Math.cos(latA) * Math.sin(latB) - Math.sin(latA) * Math.cos(latB) * Math.cos(lonB - lonA));
|
|
|
|
}
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
export function route(points: Coordinates[], brouterProfile: string, privateRoads: boolean, routing: boolean): Promise<TrackPoint[]> {
|
2024-04-23 18:36:16 +02:00
|
|
|
if (routing) {
|
2024-04-24 19:32:55 +02:00
|
|
|
return getRoute(points, brouterProfile, 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
|
|
|
}
|