2024-04-22 19:36:31 +02:00
|
|
|
<script lang="ts">
|
2024-04-23 14:11:05 +02:00
|
|
|
import ToolbarItemMenu from '../ToolbarItemMenu.svelte';
|
2024-04-22 19:36:31 +02:00
|
|
|
import * as Card from '$lib/components/ui/card';
|
|
|
|
import * as Select from '$lib/components/ui/select';
|
|
|
|
import { Switch } from '$lib/components/ui/switch';
|
|
|
|
import { Label } from '$lib/components/ui/label/index.js';
|
|
|
|
import * as Alert from '$lib/components/ui/alert';
|
|
|
|
import { CircleHelp } from 'lucide-svelte';
|
|
|
|
|
2024-04-24 22:35:53 +02:00
|
|
|
import { map, selectedFiles, getFileStore, applyToFile } from '$lib/stores';
|
2024-04-24 19:32:55 +02:00
|
|
|
import { AnchorPointHierarchy, getMarker, route } from './routing';
|
2024-04-23 16:20:47 +02:00
|
|
|
import { onDestroy } from 'svelte';
|
2024-04-23 18:36:16 +02:00
|
|
|
import mapboxgl from 'mapbox-gl';
|
|
|
|
import KDBush from 'kdbush';
|
|
|
|
|
|
|
|
import type { GPXFile } from 'gpx';
|
2024-04-23 14:11:05 +02:00
|
|
|
|
2024-04-24 18:13:01 +02:00
|
|
|
import { _ } from 'svelte-i18n';
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
let brouterProfiles: { [key: string]: string } = {
|
2024-04-22 19:36:31 +02:00
|
|
|
bike: 'Trekking-dry',
|
2024-04-24 19:32:55 +02:00
|
|
|
racing_bike: 'fastbike',
|
|
|
|
mountain_bike: 'MTB',
|
2024-04-22 19:36:31 +02:00
|
|
|
foot: 'Hiking-Alpine-SAC6',
|
|
|
|
motorcycle: 'Car-FastEco',
|
|
|
|
water: 'river',
|
|
|
|
railway: 'rail'
|
|
|
|
};
|
2024-04-24 19:32:55 +02:00
|
|
|
let routingProfile = {
|
|
|
|
value: 'bike',
|
|
|
|
label: $_('toolbar.routing.activities.bike')
|
|
|
|
};
|
2024-04-22 19:36:31 +02:00
|
|
|
let routing = true;
|
|
|
|
let privateRoads = false;
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
let anchorPointHierarchy: AnchorPointHierarchy | null = null;
|
2024-04-23 16:20:47 +02:00
|
|
|
let markers: mapboxgl.Marker[] = [];
|
2024-04-24 22:35:53 +02:00
|
|
|
let file: GPXFile | null = null;
|
2024-04-23 18:36:16 +02:00
|
|
|
let kdbush: KDBush | null = null;
|
2024-04-23 16:20:47 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
function toggleMarkersForZoomLevelAndBounds() {
|
2024-04-23 16:20:47 +02:00
|
|
|
if ($map) {
|
|
|
|
let zoom = $map.getZoom();
|
|
|
|
markers.forEach((marker) => {
|
2024-04-23 19:33:11 +02:00
|
|
|
if (marker._simplified.zoom <= zoom && $map.getBounds().contains(marker.getLngLat())) {
|
|
|
|
marker.addTo($map);
|
2024-04-23 16:20:47 +02:00
|
|
|
} else {
|
2024-04-23 19:33:11 +02:00
|
|
|
marker.remove();
|
2024-04-23 16:20:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
async function extendFile(e: mapboxgl.MapMouseEvent) {
|
|
|
|
if (file && anchorPointHierarchy && anchorPointHierarchy.points.length > 0) {
|
|
|
|
let lastPoint = anchorPointHierarchy.points[anchorPointHierarchy.points.length - 1];
|
|
|
|
let newPoint = {
|
|
|
|
lon: e.lngLat.lng,
|
|
|
|
lat: e.lngLat.lat
|
|
|
|
};
|
|
|
|
let response = await route(
|
|
|
|
[lastPoint.point.getCoordinates(), newPoint],
|
|
|
|
brouterProfiles[routingProfile.value],
|
|
|
|
privateRoads,
|
|
|
|
routing
|
|
|
|
);
|
2024-04-24 22:35:53 +02:00
|
|
|
applyToFile(file, (f) => f.append(response));
|
2024-04-24 19:32:55 +02:00
|
|
|
}
|
2024-04-23 18:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let insertableMarker: mapboxgl.Marker | null = null;
|
|
|
|
function moveInsertableMarker(e: mapboxgl.MapMouseEvent) {
|
|
|
|
if (insertableMarker && kdbush && $map) {
|
|
|
|
let bounds = $map.getBounds();
|
|
|
|
let latLngDistance = Math.max(
|
|
|
|
Math.abs(bounds.getNorth() - bounds.getSouth()),
|
|
|
|
Math.abs(bounds.getEast() - bounds.getWest())
|
|
|
|
);
|
|
|
|
if (kdbush.within(e.lngLat.lng, e.lngLat.lat, latLngDistance / 200).length > 0) {
|
|
|
|
insertableMarker.setLngLat(e.lngLat);
|
|
|
|
} else {
|
|
|
|
insertableMarker.remove();
|
|
|
|
insertableMarker = null;
|
|
|
|
$map.off('mousemove', moveInsertableMarker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function showInsertableMarker(e: mapboxgl.MapMouseEvent) {
|
|
|
|
if ($map && !insertableMarker) {
|
|
|
|
insertableMarker = getMarker({
|
|
|
|
lon: e.lngLat.lng,
|
|
|
|
lat: e.lngLat.lat
|
|
|
|
});
|
|
|
|
insertableMarker.addTo($map);
|
|
|
|
$map.on('mousemove', moveInsertableMarker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:20:47 +02:00
|
|
|
function clean() {
|
|
|
|
markers.forEach((marker) => {
|
|
|
|
marker.remove();
|
|
|
|
});
|
|
|
|
markers = [];
|
|
|
|
if ($map) {
|
2024-04-23 19:33:11 +02:00
|
|
|
$map.off('zoom', toggleMarkersForZoomLevelAndBounds);
|
|
|
|
$map.off('move', toggleMarkersForZoomLevelAndBounds);
|
2024-04-23 18:36:16 +02:00
|
|
|
$map.off('click', extendFile);
|
|
|
|
if (file) {
|
2024-04-24 22:35:53 +02:00
|
|
|
$map.off('mouseover', file.layerId, showInsertableMarker);
|
2024-04-23 18:36:16 +02:00
|
|
|
}
|
|
|
|
if (insertableMarker) {
|
|
|
|
insertableMarker.remove();
|
|
|
|
}
|
2024-04-23 16:20:47 +02:00
|
|
|
}
|
2024-04-23 18:36:16 +02:00
|
|
|
kdbush = null;
|
2024-04-23 16:20:47 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 14:11:05 +02:00
|
|
|
$: if ($selectedFiles.size == 1 && $map) {
|
2024-04-23 18:36:16 +02:00
|
|
|
clean();
|
|
|
|
|
|
|
|
file = $selectedFiles.values().next().value;
|
2024-04-24 20:13:42 +02:00
|
|
|
|
2024-04-23 18:36:16 +02:00
|
|
|
// record time
|
|
|
|
let start = performance.now();
|
2024-04-24 22:35:53 +02:00
|
|
|
anchorPointHierarchy = AnchorPointHierarchy.create(file);
|
2024-04-23 18:36:16 +02:00
|
|
|
// record time
|
|
|
|
let end = performance.now();
|
|
|
|
console.log('Time to create anchor points: ' + (end - start) + 'ms');
|
|
|
|
|
2024-04-24 19:32:55 +02:00
|
|
|
markers = anchorPointHierarchy.getMarkers();
|
2024-04-23 16:20:47 +02:00
|
|
|
|
2024-04-23 19:33:11 +02:00
|
|
|
toggleMarkersForZoomLevelAndBounds();
|
|
|
|
$map.on('zoom', toggleMarkersForZoomLevelAndBounds);
|
|
|
|
$map.on('move', toggleMarkersForZoomLevelAndBounds);
|
2024-04-23 18:36:16 +02:00
|
|
|
$map.on('click', extendFile);
|
2024-04-24 22:35:53 +02:00
|
|
|
$map.on('mouseover', file.layerId, showInsertableMarker);
|
2024-04-23 18:36:16 +02:00
|
|
|
|
2024-04-24 22:35:53 +02:00
|
|
|
let points = file.getTrackPoints();
|
2024-04-23 18:36:16 +02:00
|
|
|
|
|
|
|
start = performance.now();
|
|
|
|
kdbush = new KDBush(points.length);
|
|
|
|
for (let i = 0; i < points.length; i++) {
|
|
|
|
kdbush.add(points[i].getLongitude(), points[i].getLatitude());
|
|
|
|
}
|
|
|
|
kdbush.finish();
|
|
|
|
end = performance.now();
|
|
|
|
console.log('Time to create kdbush: ' + (end - start) + 'ms');
|
2024-04-23 16:20:47 +02:00
|
|
|
} else {
|
|
|
|
clean();
|
2024-04-22 19:36:31 +02:00
|
|
|
}
|
2024-04-23 16:20:47 +02:00
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
clean();
|
|
|
|
});
|
2024-04-22 19:36:31 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<ToolbarItemMenu>
|
|
|
|
<Card.Root>
|
2024-04-24 18:13:01 +02:00
|
|
|
<Card.Content class="p-4 flex flex-col gap-4">
|
2024-04-22 19:36:31 +02:00
|
|
|
<div class="w-full flex flex-row justify-between items-center gap-2">
|
2024-04-24 18:13:01 +02:00
|
|
|
<Label>{$_('toolbar.routing.activity')}</Label>
|
2024-04-22 19:36:31 +02:00
|
|
|
<Select.Root bind:selected={routingProfile}>
|
|
|
|
<Select.Trigger class="h-8 w-40">
|
|
|
|
<Select.Value />
|
|
|
|
</Select.Trigger>
|
|
|
|
<Select.Content>
|
|
|
|
{#each Object.keys(brouterProfiles) as profile}
|
2024-04-24 19:32:55 +02:00
|
|
|
<Select.Item value={profile}
|
|
|
|
>{$_(`toolbar.routing.activities.${profile}`)}</Select.Item
|
|
|
|
>
|
2024-04-22 19:36:31 +02:00
|
|
|
{/each}
|
|
|
|
</Select.Content>
|
|
|
|
</Select.Root>
|
|
|
|
</div>
|
|
|
|
<div class="w-full flex flex-row justify-between items-center gap-2">
|
2024-04-24 18:13:01 +02:00
|
|
|
<Label for="routing">{$_('toolbar.routing.use_routing')}</Label>
|
2024-04-22 19:36:31 +02:00
|
|
|
<Switch id="routing" class="scale-90" bind:checked={routing} />
|
|
|
|
</div>
|
|
|
|
<div class="w-full flex flex-row justify-between items-center gap-2">
|
2024-04-24 18:13:01 +02:00
|
|
|
<Label for="private">{$_('toolbar.routing.allow_private')}</Label>
|
2024-04-22 19:36:31 +02:00
|
|
|
<Switch id="private" class="scale-90" bind:checked={privateRoads} />
|
|
|
|
</div>
|
|
|
|
<Alert.Root class="max-w-64">
|
|
|
|
<CircleHelp size="16" />
|
|
|
|
<!-- <Alert.Title>Heads up!</Alert.Title> -->
|
|
|
|
<Alert.Description>
|
|
|
|
{#if $selectedFiles.size > 1}
|
2024-04-24 18:13:01 +02:00
|
|
|
<div>{$_('toolbar.routing.help_multiple_files')}</div>
|
2024-04-22 19:36:31 +02:00
|
|
|
{:else if $selectedFiles.size == 0}
|
2024-04-24 18:13:01 +02:00
|
|
|
<div>{$_('toolbar.routing.help_no_file')}</div>
|
2024-04-22 19:36:31 +02:00
|
|
|
{:else}
|
2024-04-24 18:13:01 +02:00
|
|
|
<div>{$_('toolbar.routing.help')}</div>
|
2024-04-22 19:36:31 +02:00
|
|
|
{/if}
|
|
|
|
</Alert.Description>
|
|
|
|
</Alert.Root>
|
|
|
|
</Card.Content>
|
|
|
|
</Card.Root>
|
|
|
|
</ToolbarItemMenu>
|