This commit is contained in:
vcoppe
2024-05-23 12:57:24 +02:00
parent 51495e9bd1
commit 8b5d1f1fbf
7 changed files with 67 additions and 39 deletions

View File

@@ -1,7 +1,6 @@
import { distance, type Coordinates, TrackPoint, TrackSegment } from "gpx";
import { original } from "immer";
import { get, type Readable } from "svelte/store";
import { computeAnchorPoints } from "./Simplify";
import mapboxgl from "mapbox-gl";
import { route } from "./Routing";
@@ -66,22 +65,6 @@ export class RoutingControls {
for (let segmentIndex = 0; segmentIndex < segments.length; segmentIndex++) {
let segment = segments[segmentIndex];
if (!segment._data.anchors) { // New segment, compute anchor points for it
computeAnchorPoints(segment);
}
if (segment.trkpt.length > 0) {
if (!segment.trkpt[0]._data.anchor) { // First point is not an anchor, make it one
segment.trkpt[0]._data.anchor = true;
segment.trkpt[0]._data.zoom = 0;
}
if (!segment.trkpt[segment.trkpt.length - 1]._data.anchor) { // Last point is not an anchor, make it one
segment.trkpt[segment.trkpt.length - 1]._data.anchor = true;
segment.trkpt[segment.trkpt.length - 1]._data.zoom = 0;
}
}
for (let point of segment.trkpt) { // Update the existing anchors (could be improved by matching the existing anchors with the new ones?)
if (point._data.anchor) {
if (anchorIndex < this.anchors.length) {
@@ -433,17 +416,19 @@ export class RoutingControls {
}
if (anchors[0].point._data.index === 0) { // First anchor is the first point of the segment
anchors[0].point = response[0]; // Update the first anchor
anchors[0].point = response[0]; // Replace the first anchor
anchors[0].point._data.index = 0;
} else {
response.splice(0, 0, anchors[0].point); // Keep the original start point
anchors[0].point = anchors[0].point.clone(); // Clone the anchor to assign new properties
response.splice(0, 0, anchors[0].point); // Insert it in the response to keep it
}
if (anchors[anchors.length - 1].point._data.index === segment.trkpt.length - 1) { // Last anchor is the last point of the segment
anchors[anchors.length - 1].point = response[response.length - 1]; // Update the last anchor
anchors[anchors.length - 1].point = response[response.length - 1]; // Replace the last anchor
anchors[anchors.length - 1].point._data.index = segment.trkpt.length - 1;
} else {
response.push(anchors[anchors.length - 1].point); // Keep the original end point
anchors[anchors.length - 1].point = anchors[anchors.length - 1].point.clone(); // Clone the anchor to assign new properties
response.push(anchors[anchors.length - 1].point); // Insert it in the response to keep it
}
for (let i = 1; i < anchors.length - 1; i++) {

View File

@@ -1,4 +1,4 @@
import type { Coordinates, TrackPoint, TrackSegment } from "gpx";
import type { Coordinates, GPXFile, TrackPoint, TrackSegment } from "gpx";
type SimplifiedTrackPoint = { point: TrackPoint, distance?: number };
@@ -15,7 +15,32 @@ export function getZoomLevelForDistance(latitude: number, distance?: number): nu
return Math.min(20, Math.max(0, Math.floor(Math.log2((earthRadius * Math.cos(lat)) / distance))));
}
export function computeAnchorPoints(segment: TrackSegment) {
export function updateAnchorPoints(file: GPXFile) {
let segments = file.getSegments();
for (let segmentIndex = 0; segmentIndex < segments.length; segmentIndex++) {
let segment = segments[segmentIndex];
if (!segment._data.anchors) { // New segment, compute anchor points for it
computeAnchorPoints(segment);
continue;
}
if (segment.trkpt.length > 0) {
if (!segment.trkpt[0]._data.anchor) { // First point is not an anchor, make it one
segment.trkpt[0]._data.anchor = true;
segment.trkpt[0]._data.zoom = 0;
}
if (!segment.trkpt[segment.trkpt.length - 1]._data.anchor) { // Last point is not an anchor, make it one
segment.trkpt[segment.trkpt.length - 1]._data.anchor = true;
segment.trkpt[segment.trkpt.length - 1]._data.zoom = 0;
}
}
}
}
function computeAnchorPoints(segment: TrackSegment) {
let points = segment.trkpt;
let anchors = ramerDouglasPeucker(points);
anchors.forEach((anchor) => {