adds proper total calculation for routing
This commit is contained in:
@@ -1,16 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type {
|
import type {
|
||||||
RoutingOptions,
|
RoutingOptions,
|
||||||
ValhallaAutoCostingOptions,
|
ValhallaBicycleCostingOptions
|
||||||
ValhallaBicycleCostingOptions,
|
|
||||||
ValhallaPedestrianCostingOptions,
|
|
||||||
} from "$lib/models/valhalla";
|
} from "$lib/models/valhalla";
|
||||||
|
import { formatSpeed } from "$lib/util/format_util";
|
||||||
import { _ } from "svelte-i18n";
|
import { _ } from "svelte-i18n";
|
||||||
|
import { slide } from "svelte/transition";
|
||||||
import Select, { type SelectItem } from "../base/select.svelte";
|
import Select, { type SelectItem } from "../base/select.svelte";
|
||||||
import Slider from "../base/slider.svelte";
|
import Slider from "../base/slider.svelte";
|
||||||
import Toggle from "../base/toggle.svelte";
|
import Toggle from "../base/toggle.svelte";
|
||||||
import { formatDistance, formatSpeed } from "$lib/util/format_util";
|
|
||||||
import { slide } from "svelte/transition";
|
|
||||||
interface Props {
|
interface Props {
|
||||||
options: RoutingOptions;
|
options: RoutingOptions;
|
||||||
}
|
}
|
||||||
@@ -66,7 +64,7 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
let showSettings = $state(true);
|
let showSettings = $state(false);
|
||||||
|
|
||||||
// svelte-ignore non_reactive_update
|
// svelte-ignore non_reactive_update
|
||||||
let cycleSpeedSlider: Slider;
|
let cycleSpeedSlider: Slider;
|
||||||
@@ -209,7 +207,7 @@
|
|||||||
}
|
}
|
||||||
></Slider>
|
></Slider>
|
||||||
<p class="text-sm text-end">
|
<p class="text-sm text-end">
|
||||||
{options.bicycleOptions.avoid_bad_surfaces?.toFixed(0)}
|
{options.bicycleOptions.avoid_bad_surfaces?.toFixed(2)}
|
||||||
</p>
|
</p>
|
||||||
<hr class="border-input-border my-3" />
|
<hr class="border-input-border my-3" />
|
||||||
<Toggle
|
<Toggle
|
||||||
|
|||||||
@@ -83,11 +83,43 @@ export interface RoutingOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ValhallaRouteResponse {
|
interface ValhallaRouteResponse {
|
||||||
trip: {
|
trip: Trip
|
||||||
legs: {
|
}
|
||||||
shape: string;
|
|
||||||
}[];
|
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 {
|
interface ValhallaHeightResponse {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export function setRoute(newRoute: GPX) {
|
|||||||
export async function calculateRouteBetween(startLat: number, startLon: number, endLat: number, endLon: number, options: RoutingOptions) {
|
export async function calculateRouteBetween(startLat: number, startLon: number, endLat: number, endLon: number, options: RoutingOptions) {
|
||||||
|
|
||||||
let shape;
|
let shape;
|
||||||
|
let duration: number;
|
||||||
if (options.autoRouting) {
|
if (options.autoRouting) {
|
||||||
let costingBody;
|
let costingBody;
|
||||||
switch (options.modeOfTransport) {
|
switch (options.modeOfTransport) {
|
||||||
@@ -52,8 +53,10 @@ export async function calculateRouteBetween(startLat: number, startLon: number,
|
|||||||
|
|
||||||
const routeResponse: ValhallaRouteResponse = await r.json();
|
const routeResponse: ValhallaRouteResponse = await r.json();
|
||||||
shape = routeResponse.trip.legs[0].shape
|
shape = routeResponse.trip.legs[0].shape
|
||||||
|
duration = routeResponse.trip.summary.time
|
||||||
} else {
|
} else {
|
||||||
shape = encodePolyline([[startLat, startLon], [endLat, endLon]])
|
shape = encodePolyline([[startLat, startLon], [endLat, endLon]])
|
||||||
|
duration = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const r2 = await fetch("/api/v1/valhalla/height", { method: "POST", body: JSON.stringify({ encoded_polyline: shape }) })
|
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 heightResponse: ValhallaHeightResponse = await r2.json()
|
||||||
const points = decodePolyline(shape);
|
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
|
return waypoints
|
||||||
}
|
}
|
||||||
@@ -78,6 +83,8 @@ export async function insertIntoRoute(waypoints: Waypoint[], index?: number) {
|
|||||||
} else {
|
} else {
|
||||||
route.trk?.at(0)?.trkseg?.push(segment);
|
route.trk?.at(0)?.trkseg?.push(segment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
route.features = route.getTotals();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function editRoute(index: number, waypoints: Waypoint[]) {
|
export async function editRoute(index: number, waypoints: Waypoint[]) {
|
||||||
@@ -85,8 +92,12 @@ export async function editRoute(index: number, waypoints: Waypoint[]) {
|
|||||||
if (segment) {
|
if (segment) {
|
||||||
segment.trkpt = waypoints
|
segment.trkpt = waypoints
|
||||||
}
|
}
|
||||||
|
route.features = route.getTotals();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteFromRoute(index: number) {
|
export function deleteFromRoute(index: number) {
|
||||||
route.trk?.at(0)?.trkseg?.splice(index, 1);
|
route.trk?.at(0)?.trkseg?.splice(index, 1);
|
||||||
|
route.features = route.getTotals();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -831,7 +831,7 @@
|
|||||||
overwriteGPX = true;
|
overwriteGPX = true;
|
||||||
const totals = route.features;
|
const totals = route.features;
|
||||||
$formData.distance = totals.distance;
|
$formData.distance = totals.distance;
|
||||||
$formData.duration = totals.duration;
|
$formData.duration = totals.duration / 1000 / 60;
|
||||||
$formData.elevation_gain = totals.elevationGain;
|
$formData.elevation_gain = totals.elevationGain;
|
||||||
$formData.elevation_loss = totals.elevationLoss;
|
$formData.elevation_loss = totals.elevationLoss;
|
||||||
$formData.expand!.gpx_data = route.toString();
|
$formData.expand!.gpx_data = route.toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user