adds proper total calculation for routing

This commit is contained in:
Christian Beutel
2025-03-29 14:27:55 +01:00
parent 60be2f65e4
commit 1f3aabda37
5 changed files with 56 additions and 15 deletions

View File

@@ -1,16 +1,14 @@
<script lang="ts">
import type {
RoutingOptions,
ValhallaAutoCostingOptions,
ValhallaBicycleCostingOptions,
ValhallaPedestrianCostingOptions,
ValhallaBicycleCostingOptions
} from "$lib/models/valhalla";
import { formatSpeed } from "$lib/util/format_util";
import { _ } from "svelte-i18n";
import { slide } from "svelte/transition";
import Select, { type SelectItem } from "../base/select.svelte";
import Slider from "../base/slider.svelte";
import Toggle from "../base/toggle.svelte";
import { formatDistance, formatSpeed } from "$lib/util/format_util";
import { slide } from "svelte/transition";
interface Props {
options: RoutingOptions;
}
@@ -66,7 +64,7 @@
}
})
let showSettings = $state(true);
let showSettings = $state(false);
// svelte-ignore non_reactive_update
let cycleSpeedSlider: Slider;
@@ -209,7 +207,7 @@
}
></Slider>
<p class="text-sm text-end">
{options.bicycleOptions.avoid_bad_surfaces?.toFixed(0)}
{options.bicycleOptions.avoid_bad_surfaces?.toFixed(2)}
</p>
<hr class="border-input-border my-3" />
<Toggle

View File

@@ -83,11 +83,43 @@ export interface RoutingOptions {
}
interface ValhallaRouteResponse {
trip: {
legs: {
shape: string;
}[];
};
trip: Trip
}
export interface Trip {
locations: Location[]
legs: Leg[]
summary: Summary
status_message: string
status: number
units: string
language: string
}
export interface Location {
type: string
lat: number
lon: number
original_index: number
}
export interface Leg {
summary: Summary
shape: string
}
export interface Summary {
has_time_restrictions: boolean
has_toll: boolean
has_highway: boolean
has_ferry: boolean
min_lat: number
min_lon: number
max_lat: number
max_lon: number
time: number
length: number
cost: number
}
interface ValhallaHeightResponse {

View File

@@ -23,6 +23,7 @@ export function setRoute(newRoute: GPX) {
export async function calculateRouteBetween(startLat: number, startLon: number, endLat: number, endLon: number, options: RoutingOptions) {
let shape;
let duration: number;
if (options.autoRouting) {
let costingBody;
switch (options.modeOfTransport) {
@@ -52,8 +53,10 @@ export async function calculateRouteBetween(startLat: number, startLon: number,
const routeResponse: ValhallaRouteResponse = await r.json();
shape = routeResponse.trip.legs[0].shape
duration = routeResponse.trip.summary.time
} else {
shape = encodePolyline([[startLat, startLon], [endLat, endLon]])
duration = 0;
}
const r2 = await fetch("/api/v1/valhalla/height", { method: "POST", body: JSON.stringify({ encoded_polyline: shape }) })
@@ -65,7 +68,9 @@ export async function calculateRouteBetween(startLat: number, startLon: number,
const heightResponse: ValhallaHeightResponse = await r2.json()
const points = decodePolyline(shape);
const waypoints = points.map((p, i) => new Waypoint({ $: { lat: p[0], lon: p[1] }, ele: heightResponse.height[i] }))
const startTime = new Date().getTime();
const waypoints = points.map((p, i) => new Waypoint({ $: { lat: p[0], lon: p[1] }, ele: heightResponse.height[i], time: new Date(startTime + (((duration * 1000) / points.length) * i)) }))
return waypoints
}
@@ -78,6 +83,8 @@ export async function insertIntoRoute(waypoints: Waypoint[], index?: number) {
} else {
route.trk?.at(0)?.trkseg?.push(segment);
}
route.features = route.getTotals();
}
export async function editRoute(index: number, waypoints: Waypoint[]) {
@@ -85,8 +92,12 @@ export async function editRoute(index: number, waypoints: Waypoint[]) {
if (segment) {
segment.trkpt = waypoints
}
route.features = route.getTotals();
}
export function deleteFromRoute(index: number) {
route.trk?.at(0)?.trkseg?.splice(index, 1);
route.features = route.getTotals();
}

View File

@@ -831,7 +831,7 @@
overwriteGPX = true;
const totals = route.features;
$formData.distance = totals.distance;
$formData.duration = totals.duration;
$formData.duration = totals.duration / 1000 / 60;
$formData.elevation_gain = totals.elevationGain;
$formData.elevation_loss = totals.elevationLoss;
$formData.expand!.gpx_data = route.toString();