2025-02-02 11:17:22 +01:00
|
|
|
import { get, type Readable } from 'svelte/store';
|
|
|
|
|
import mapboxgl from 'mapbox-gl';
|
2025-10-17 23:54:45 +02:00
|
|
|
import { map } from '$lib/components/map/map';
|
|
|
|
|
import { waypointPopup, trackpointPopup } from './gpx-layer-popup';
|
2025-02-02 11:17:22 +01:00
|
|
|
import {
|
|
|
|
|
ListTrackSegmentItem,
|
|
|
|
|
ListWaypointItem,
|
|
|
|
|
ListWaypointsItem,
|
|
|
|
|
ListTrackItem,
|
|
|
|
|
ListFileItem,
|
|
|
|
|
ListRootItem,
|
2025-10-05 19:34:05 +02:00
|
|
|
} from '$lib/components/file-list/file-list';
|
2025-10-18 16:10:08 +02:00
|
|
|
import { getClosestLinePoint, getElevation } from '$lib/utils';
|
2025-10-17 23:54:45 +02:00
|
|
|
import { selectedWaypoint } from '$lib/components/toolbar/tools/waypoint/waypoint';
|
2025-02-02 11:17:22 +01:00
|
|
|
import { MapPin, Square } from 'lucide-static';
|
|
|
|
|
import { getSymbolKey, symbols } from '$lib/assets/symbols';
|
2025-10-18 00:31:14 +02:00
|
|
|
import type { GPXFileWithStatistics } from '$lib/logic/statistics-tree';
|
2025-10-17 23:54:45 +02:00
|
|
|
import { selection } from '$lib/logic/selection';
|
|
|
|
|
import { settings } from '$lib/logic/settings';
|
|
|
|
|
import { currentTool, Tool } from '$lib/components/toolbar/tools';
|
|
|
|
|
import { fileActionManager } from '$lib/logic/file-action-manager';
|
|
|
|
|
import { fileActions } from '$lib/logic/file-actions';
|
|
|
|
|
import { splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
2025-10-18 16:10:08 +02:00
|
|
|
import { mapCursor, MapCursorState } from '$lib/logic/map-cursor';
|
2024-04-25 13:48:31 +02:00
|
|
|
|
|
|
|
|
const colors = [
|
|
|
|
|
'#ff0000',
|
|
|
|
|
'#0000ff',
|
|
|
|
|
'#46e646',
|
|
|
|
|
'#00ccff',
|
|
|
|
|
'#ff9900',
|
|
|
|
|
'#ff00ff',
|
|
|
|
|
'#ffff32',
|
|
|
|
|
'#288228',
|
|
|
|
|
'#9933ff',
|
|
|
|
|
'#50f0be',
|
2025-02-02 11:17:22 +01:00
|
|
|
'#8c645a',
|
2024-04-25 13:48:31 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const colorCount: { [key: string]: number } = {};
|
|
|
|
|
for (let color of colors) {
|
|
|
|
|
colorCount[color] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the color with the least amount of uses
|
|
|
|
|
function getColor() {
|
|
|
|
|
let color = colors.reduce((a, b) => (colorCount[a] <= colorCount[b] ? a : b));
|
|
|
|
|
colorCount[color]++;
|
|
|
|
|
return color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decrementColor(color: string) {
|
2024-06-19 16:15:21 +02:00
|
|
|
if (colorCount.hasOwnProperty(color)) {
|
|
|
|
|
colorCount[color]--;
|
|
|
|
|
}
|
2024-04-25 13:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 23:20:09 +02:00
|
|
|
function getMarkerForSymbol(symbol: string | undefined, layerColor: string) {
|
|
|
|
|
let symbolSvg = symbol ? symbols[symbol]?.iconSvg : undefined;
|
|
|
|
|
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
2025-02-02 11:17:22 +01:00
|
|
|
${Square.replace('width="24"', 'width="12"')
|
|
|
|
|
.replace('height="24"', 'height="12"')
|
|
|
|
|
.replace('stroke="currentColor"', 'stroke="SteelBlue"')
|
|
|
|
|
.replace('stroke-width="2"', 'stroke-width="1.5" x="9.6" y="0.4"')
|
|
|
|
|
.replace('fill="none"', `fill="${layerColor}"`)}
|
|
|
|
|
${MapPin.replace('width="24"', '')
|
|
|
|
|
.replace('height="24"', '')
|
|
|
|
|
.replace('stroke="currentColor"', '')
|
|
|
|
|
.replace('path', `path fill="#3fb1ce" stroke="SteelBlue" stroke-width="1"`)
|
|
|
|
|
.replace(
|
|
|
|
|
'circle',
|
|
|
|
|
`circle fill="${symbolSvg ? 'none' : 'white'}" stroke="${symbolSvg ? 'none' : 'white'}" stroke-width="2"`
|
|
|
|
|
)}
|
|
|
|
|
${
|
|
|
|
|
symbolSvg
|
|
|
|
|
?.replace('width="24"', 'width="10"')
|
2024-08-09 23:50:47 +02:00
|
|
|
.replace('height="24"', 'height="10"')
|
|
|
|
|
.replace('stroke="currentColor"', 'stroke="white"')
|
2025-02-02 11:17:22 +01:00
|
|
|
.replace('stroke-width="2"', 'stroke-width="2.5" x="7" y="5"') ?? ''
|
|
|
|
|
}
|
2024-08-09 23:50:47 +02:00
|
|
|
</svg>`;
|
2024-08-08 23:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-17 23:54:45 +02:00
|
|
|
const { directionMarkers, treeFileView, defaultOpacity, defaultWidth } = settings;
|
2024-05-08 14:53:28 +02:00
|
|
|
|
2024-04-25 14:55:35 +02:00
|
|
|
export class GPXLayer {
|
2024-04-30 20:55:47 +02:00
|
|
|
fileId: string;
|
2024-05-08 21:31:54 +02:00
|
|
|
file: Readable<GPXFileWithStatistics | undefined>;
|
2024-04-25 13:48:31 +02:00
|
|
|
layerColor: string;
|
2024-04-28 18:59:31 +02:00
|
|
|
markers: mapboxgl.Marker[] = [];
|
2024-05-24 20:23:49 +02:00
|
|
|
selected: boolean = false;
|
|
|
|
|
draggable: boolean;
|
2024-05-08 14:53:28 +02:00
|
|
|
unsubscribe: Function[] = [];
|
2024-04-25 13:48:31 +02:00
|
|
|
|
2024-05-03 15:59:34 +02:00
|
|
|
updateBinded: () => void = this.update.bind(this);
|
2024-06-13 09:44:27 +02:00
|
|
|
layerOnMouseEnterBinded: (e: any) => void = this.layerOnMouseEnter.bind(this);
|
2024-06-10 20:03:57 +02:00
|
|
|
layerOnMouseLeaveBinded: () => void = this.layerOnMouseLeave.bind(this);
|
2024-10-08 15:49:14 +02:00
|
|
|
layerOnMouseMoveBinded: (e: any) => void = this.layerOnMouseMove.bind(this);
|
2024-06-10 20:03:57 +02:00
|
|
|
layerOnClickBinded: (e: any) => void = this.layerOnClick.bind(this);
|
2024-09-12 10:13:03 +02:00
|
|
|
layerOnContextMenuBinded: (e: any) => void = this.layerOnContextMenu.bind(this);
|
2024-04-25 16:41:06 +02:00
|
|
|
|
2025-10-17 23:54:45 +02:00
|
|
|
constructor(fileId: string, file: Readable<GPXFileWithStatistics | undefined>) {
|
2024-05-03 15:59:34 +02:00
|
|
|
this.fileId = fileId;
|
2024-05-08 21:31:54 +02:00
|
|
|
this.file = file;
|
2024-04-25 13:48:31 +02:00
|
|
|
this.layerColor = getColor();
|
2025-10-17 23:54:45 +02:00
|
|
|
this.unsubscribe.push(
|
|
|
|
|
map.subscribe(($map) => {
|
|
|
|
|
if ($map) {
|
|
|
|
|
$map.on('style.import.load', this.updateBinded);
|
|
|
|
|
this.update();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-05-08 14:53:28 +02:00
|
|
|
this.unsubscribe.push(file.subscribe(this.updateBinded));
|
2025-02-02 11:17:22 +01:00
|
|
|
this.unsubscribe.push(
|
|
|
|
|
selection.subscribe(($selection) => {
|
|
|
|
|
let newSelected = $selection.hasAnyChildren(new ListFileItem(this.fileId));
|
|
|
|
|
if (this.selected || newSelected) {
|
|
|
|
|
this.selected = newSelected;
|
|
|
|
|
this.update();
|
|
|
|
|
}
|
|
|
|
|
if (newSelected) {
|
|
|
|
|
this.moveToFront();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-05-23 11:21:57 +02:00
|
|
|
this.unsubscribe.push(directionMarkers.subscribe(this.updateBinded));
|
2025-02-02 11:17:22 +01:00
|
|
|
this.unsubscribe.push(
|
|
|
|
|
currentTool.subscribe((tool) => {
|
|
|
|
|
if (tool === Tool.WAYPOINT && !this.draggable) {
|
|
|
|
|
this.draggable = true;
|
|
|
|
|
this.markers.forEach((marker) => marker.setDraggable(true));
|
|
|
|
|
} else if (tool !== Tool.WAYPOINT && this.draggable) {
|
|
|
|
|
this.draggable = false;
|
|
|
|
|
this.markers.forEach((marker) => marker.setDraggable(false));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-05-24 20:23:49 +02:00
|
|
|
this.draggable = get(currentTool) === Tool.WAYPOINT;
|
2024-04-25 13:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-03 15:59:34 +02:00
|
|
|
update() {
|
2025-10-17 23:54:45 +02:00
|
|
|
const _map = get(map);
|
2024-05-08 21:31:54 +02:00
|
|
|
let file = get(this.file)?.file;
|
2025-10-17 23:54:45 +02:00
|
|
|
if (!_map || !file) {
|
2024-05-03 15:59:34 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-02 11:17:22 +01:00
|
|
|
if (
|
|
|
|
|
file._data.style &&
|
|
|
|
|
file._data.style.color &&
|
|
|
|
|
this.layerColor !== `#${file._data.style.color}`
|
|
|
|
|
) {
|
2024-06-19 16:15:21 +02:00
|
|
|
decrementColor(this.layerColor);
|
2024-07-22 18:27:53 +02:00
|
|
|
this.layerColor = `#${file._data.style.color}`;
|
2024-06-19 16:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-04 23:50:27 +02:00
|
|
|
try {
|
2025-11-10 13:11:44 +01:00
|
|
|
let source = _map.getSource(this.fileId) as mapboxgl.GeoJSONSource | undefined;
|
2024-05-08 21:31:54 +02:00
|
|
|
if (source) {
|
|
|
|
|
source.setData(this.getGeoJSON());
|
|
|
|
|
} else {
|
2025-10-17 23:54:45 +02:00
|
|
|
_map.addSource(this.fileId, {
|
2024-05-04 23:50:27 +02:00
|
|
|
type: 'geojson',
|
2025-02-02 11:17:22 +01:00
|
|
|
data: this.getGeoJSON(),
|
2024-05-04 23:50:27 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-17 23:54:45 +02:00
|
|
|
if (!_map.getLayer(this.fileId)) {
|
|
|
|
|
_map.addLayer({
|
2024-05-04 23:50:27 +02:00
|
|
|
id: this.fileId,
|
|
|
|
|
type: 'line',
|
|
|
|
|
source: this.fileId,
|
|
|
|
|
layout: {
|
|
|
|
|
'line-join': 'round',
|
2025-02-02 11:17:22 +01:00
|
|
|
'line-cap': 'round',
|
2024-05-04 23:50:27 +02:00
|
|
|
},
|
|
|
|
|
paint: {
|
|
|
|
|
'line-color': ['get', 'color'],
|
2025-01-01 14:40:28 +01:00
|
|
|
'line-width': ['get', 'width'],
|
2025-02-02 11:17:22 +01:00
|
|
|
'line-opacity': ['get', 'opacity'],
|
|
|
|
|
},
|
2024-05-04 23:50:27 +02:00
|
|
|
});
|
2024-04-25 13:48:31 +02:00
|
|
|
|
2025-10-17 23:54:45 +02:00
|
|
|
_map.on('click', this.fileId, this.layerOnClickBinded);
|
|
|
|
|
_map.on('contextmenu', this.fileId, this.layerOnContextMenuBinded);
|
|
|
|
|
_map.on('mouseenter', this.fileId, this.layerOnMouseEnterBinded);
|
|
|
|
|
_map.on('mouseleave', this.fileId, this.layerOnMouseLeaveBinded);
|
|
|
|
|
_map.on('mousemove', this.fileId, this.layerOnMouseMoveBinded);
|
2024-05-04 23:50:27 +02:00
|
|
|
}
|
2024-05-08 14:53:28 +02:00
|
|
|
|
|
|
|
|
if (get(directionMarkers)) {
|
2025-10-17 23:54:45 +02:00
|
|
|
if (!_map.getLayer(this.fileId + '-direction')) {
|
|
|
|
|
_map.addLayer(
|
2025-02-02 11:17:22 +01:00
|
|
|
{
|
|
|
|
|
id: this.fileId + '-direction',
|
|
|
|
|
type: 'symbol',
|
|
|
|
|
source: this.fileId,
|
|
|
|
|
layout: {
|
|
|
|
|
'text-field': '»',
|
|
|
|
|
'text-offset': [0, -0.1],
|
|
|
|
|
'text-keep-upright': false,
|
|
|
|
|
'text-max-angle': 361,
|
|
|
|
|
'text-allow-overlap': true,
|
|
|
|
|
'text-font': ['Open Sans Bold'],
|
|
|
|
|
'symbol-placement': 'line',
|
|
|
|
|
'symbol-spacing': 20,
|
|
|
|
|
},
|
|
|
|
|
paint: {
|
|
|
|
|
'text-color': 'white',
|
|
|
|
|
'text-opacity': 0.7,
|
|
|
|
|
'text-halo-width': 0.2,
|
|
|
|
|
'text-halo-color': 'white',
|
|
|
|
|
},
|
2024-05-08 14:53:28 +02:00
|
|
|
},
|
2025-10-24 20:06:54 +02:00
|
|
|
_map.getLayer('distance-markers-100') ? 'distance-markers-100' : undefined
|
2025-02-02 11:17:22 +01:00
|
|
|
);
|
2024-05-08 14:53:28 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-10-17 23:54:45 +02:00
|
|
|
if (_map.getLayer(this.fileId + '-direction')) {
|
|
|
|
|
_map.removeLayer(this.fileId + '-direction');
|
2024-05-08 14:53:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-02 20:04:17 +02:00
|
|
|
|
|
|
|
|
let visibleItems: [number, number][] = [];
|
|
|
|
|
file.forEachSegment((segment, trackIndex, segmentIndex) => {
|
|
|
|
|
if (!segment._data.hidden) {
|
|
|
|
|
visibleItems.push([trackIndex, segmentIndex]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-17 23:54:45 +02:00
|
|
|
_map.setFilter(
|
2025-02-02 11:17:22 +01:00
|
|
|
this.fileId,
|
|
|
|
|
[
|
|
|
|
|
'any',
|
|
|
|
|
...visibleItems.map(([trackIndex, segmentIndex]) => [
|
|
|
|
|
'all',
|
|
|
|
|
['==', 'trackIndex', trackIndex],
|
|
|
|
|
['==', 'segmentIndex', segmentIndex],
|
|
|
|
|
]),
|
|
|
|
|
],
|
|
|
|
|
{ validate: false }
|
|
|
|
|
);
|
2025-10-17 23:54:45 +02:00
|
|
|
if (_map.getLayer(this.fileId + '-direction')) {
|
|
|
|
|
_map.setFilter(
|
2025-02-02 11:17:22 +01:00
|
|
|
this.fileId + '-direction',
|
|
|
|
|
[
|
|
|
|
|
'any',
|
|
|
|
|
...visibleItems.map(([trackIndex, segmentIndex]) => [
|
|
|
|
|
'all',
|
|
|
|
|
['==', 'trackIndex', trackIndex],
|
|
|
|
|
['==', 'segmentIndex', segmentIndex],
|
|
|
|
|
]),
|
|
|
|
|
],
|
|
|
|
|
{ validate: false }
|
|
|
|
|
);
|
2024-07-18 16:15:37 +02:00
|
|
|
}
|
2025-02-02 11:17:22 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
// No reliable way to check if the map is ready to add sources and layers
|
2024-05-08 21:31:54 +02:00
|
|
|
return;
|
2024-04-30 15:57:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let markerIndex = 0;
|
2024-05-24 20:23:49 +02:00
|
|
|
|
|
|
|
|
if (get(selection).hasAnyChildren(new ListFileItem(this.fileId))) {
|
2025-02-02 11:17:22 +01:00
|
|
|
file.wpt.forEach((waypoint) => {
|
|
|
|
|
// Update markers
|
2024-08-08 23:20:09 +02:00
|
|
|
let symbolKey = getSymbolKey(waypoint.sym);
|
2024-05-24 20:23:49 +02:00
|
|
|
if (markerIndex < this.markers.length) {
|
2025-02-02 11:17:22 +01:00
|
|
|
this.markers[markerIndex].getElement().innerHTML = getMarkerForSymbol(
|
|
|
|
|
symbolKey,
|
|
|
|
|
this.layerColor
|
|
|
|
|
);
|
2024-05-24 20:23:49 +02:00
|
|
|
this.markers[markerIndex].setLngLat(waypoint.getCoordinates());
|
2025-02-02 11:17:22 +01:00
|
|
|
Object.defineProperty(this.markers[markerIndex], '_waypoint', {
|
|
|
|
|
value: waypoint,
|
|
|
|
|
writable: true,
|
|
|
|
|
});
|
2024-05-24 20:23:49 +02:00
|
|
|
} else {
|
2024-07-18 17:52:50 +02:00
|
|
|
let element = document.createElement('div');
|
|
|
|
|
element.classList.add('w-8', 'h-8', 'drop-shadow-xl');
|
2024-08-08 23:20:09 +02:00
|
|
|
element.innerHTML = getMarkerForSymbol(symbolKey, this.layerColor);
|
2024-05-24 20:23:49 +02:00
|
|
|
let marker = new mapboxgl.Marker({
|
2024-06-20 18:13:46 +02:00
|
|
|
draggable: this.draggable,
|
2024-07-18 17:52:50 +02:00
|
|
|
element,
|
2025-02-02 11:17:22 +01:00
|
|
|
anchor: 'bottom',
|
2024-05-24 20:23:49 +02:00
|
|
|
}).setLngLat(waypoint.getCoordinates());
|
|
|
|
|
Object.defineProperty(marker, '_waypoint', { value: waypoint, writable: true });
|
|
|
|
|
let dragEndTimestamp = 0;
|
2024-10-08 15:49:14 +02:00
|
|
|
marker.getElement().addEventListener('mousemove', (e) => {
|
2024-05-24 20:23:49 +02:00
|
|
|
if (marker._isDragging) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-08 15:49:14 +02:00
|
|
|
waypointPopup?.setItem({ item: marker._waypoint, fileId: this.fileId });
|
2024-05-24 13:16:41 +02:00
|
|
|
e.stopPropagation();
|
2024-05-24 20:23:49 +02:00
|
|
|
});
|
|
|
|
|
marker.getElement().addEventListener('click', (e) => {
|
|
|
|
|
if (dragEndTimestamp && Date.now() - dragEndTimestamp < 1000) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 09:44:27 +02:00
|
|
|
if (get(currentTool) === Tool.WAYPOINT && e.shiftKey) {
|
2025-10-17 23:54:45 +02:00
|
|
|
fileActions.deleteWaypoint(this.fileId, marker._waypoint._data.index);
|
2024-06-13 09:44:27 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-28 15:52:29 +01:00
|
|
|
if (get(treeFileView)) {
|
2025-02-02 11:17:22 +01:00
|
|
|
if (
|
|
|
|
|
(e.ctrlKey || e.metaKey) &&
|
|
|
|
|
get(selection).hasAnyChildren(
|
|
|
|
|
new ListWaypointsItem(this.fileId),
|
|
|
|
|
false
|
|
|
|
|
)
|
|
|
|
|
) {
|
2025-10-17 23:54:45 +02:00
|
|
|
selection.addSelectItem(
|
2025-02-02 11:17:22 +01:00
|
|
|
new ListWaypointItem(this.fileId, marker._waypoint._data.index)
|
|
|
|
|
);
|
2024-06-12 18:48:03 +02:00
|
|
|
} else {
|
2025-10-17 23:54:45 +02:00
|
|
|
selection.selectItem(
|
2025-02-02 11:17:22 +01:00
|
|
|
new ListWaypointItem(this.fileId, marker._waypoint._data.index)
|
|
|
|
|
);
|
2024-06-12 18:48:03 +02:00
|
|
|
}
|
2024-06-27 15:50:15 +02:00
|
|
|
} else if (get(currentTool) === Tool.WAYPOINT) {
|
2024-06-12 18:48:03 +02:00
|
|
|
selectedWaypoint.set([marker._waypoint, this.fileId]);
|
2024-06-27 15:50:15 +02:00
|
|
|
} else {
|
2024-10-08 15:49:14 +02:00
|
|
|
waypointPopup?.setItem({ item: marker._waypoint, fileId: this.fileId });
|
2024-05-24 20:23:49 +02:00
|
|
|
}
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
});
|
|
|
|
|
marker.on('dragstart', () => {
|
2025-10-18 16:10:08 +02:00
|
|
|
mapCursor.notify(MapCursorState.WAYPOINT_DRAGGING, true);
|
2024-05-24 20:23:49 +02:00
|
|
|
marker.getElement().style.cursor = 'grabbing';
|
2024-10-08 15:49:14 +02:00
|
|
|
waypointPopup?.hide();
|
2024-05-24 20:23:49 +02:00
|
|
|
});
|
|
|
|
|
marker.on('dragend', (e) => {
|
2025-10-18 16:10:08 +02:00
|
|
|
mapCursor.notify(MapCursorState.WAYPOINT_DRAGGING, false);
|
2024-05-24 20:23:49 +02:00
|
|
|
marker.getElement().style.cursor = '';
|
2024-09-04 19:11:56 +02:00
|
|
|
getElevation([marker._waypoint]).then((ele) => {
|
2025-10-17 23:54:45 +02:00
|
|
|
fileActionManager.applyToFile(this.fileId, (file) => {
|
2024-09-04 19:11:56 +02:00
|
|
|
let latLng = marker.getLngLat();
|
|
|
|
|
let wpt = file.wpt[marker._waypoint._data.index];
|
|
|
|
|
wpt.setCoordinates({
|
|
|
|
|
lat: latLng.lat,
|
2025-02-02 11:17:22 +01:00
|
|
|
lon: latLng.lng,
|
2024-09-04 19:11:56 +02:00
|
|
|
});
|
|
|
|
|
wpt.ele = ele[0];
|
2024-05-24 20:23:49 +02:00
|
|
|
});
|
|
|
|
|
});
|
2025-02-02 11:17:22 +01:00
|
|
|
dragEndTimestamp = Date.now();
|
2024-05-24 20:23:49 +02:00
|
|
|
});
|
|
|
|
|
this.markers.push(marker);
|
|
|
|
|
}
|
|
|
|
|
markerIndex++;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-30 15:57:47 +02:00
|
|
|
|
2025-02-02 11:17:22 +01:00
|
|
|
while (markerIndex < this.markers.length) {
|
|
|
|
|
// Remove extra markers
|
2024-04-30 15:57:47 +02:00
|
|
|
this.markers.pop()?.remove();
|
2024-04-28 18:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.markers.forEach((marker) => {
|
2024-07-02 20:04:17 +02:00
|
|
|
if (!marker._waypoint._data.hidden) {
|
2025-10-17 23:54:45 +02:00
|
|
|
marker.addTo(_map);
|
2024-07-02 20:04:17 +02:00
|
|
|
} else {
|
|
|
|
|
marker.remove();
|
|
|
|
|
}
|
2024-04-28 18:59:31 +02:00
|
|
|
});
|
2024-04-25 13:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove() {
|
2025-10-17 23:54:45 +02:00
|
|
|
const _map = get(map);
|
|
|
|
|
if (_map) {
|
|
|
|
|
_map.off('click', this.fileId, this.layerOnClickBinded);
|
|
|
|
|
_map.off('contextmenu', this.fileId, this.layerOnContextMenuBinded);
|
|
|
|
|
_map.off('mouseenter', this.fileId, this.layerOnMouseEnterBinded);
|
|
|
|
|
_map.off('mouseleave', this.fileId, this.layerOnMouseLeaveBinded);
|
|
|
|
|
_map.off('mousemove', this.fileId, this.layerOnMouseMoveBinded);
|
|
|
|
|
_map.off('style.import.load', this.updateBinded);
|
|
|
|
|
|
|
|
|
|
if (_map.getLayer(this.fileId + '-direction')) {
|
|
|
|
|
_map.removeLayer(this.fileId + '-direction');
|
2024-07-13 11:42:21 +02:00
|
|
|
}
|
2025-10-17 23:54:45 +02:00
|
|
|
if (_map.getLayer(this.fileId)) {
|
|
|
|
|
_map.removeLayer(this.fileId);
|
2024-07-13 11:42:21 +02:00
|
|
|
}
|
2025-10-17 23:54:45 +02:00
|
|
|
if (_map.getSource(this.fileId)) {
|
|
|
|
|
_map.removeSource(this.fileId);
|
2024-07-13 11:42:21 +02:00
|
|
|
}
|
2024-05-04 23:50:27 +02:00
|
|
|
}
|
2024-04-25 13:48:31 +02:00
|
|
|
|
2024-04-28 18:59:31 +02:00
|
|
|
this.markers.forEach((marker) => {
|
|
|
|
|
marker.remove();
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-08 14:53:28 +02:00
|
|
|
this.unsubscribe.forEach((unsubscribe) => unsubscribe());
|
2024-04-25 13:48:31 +02:00
|
|
|
|
|
|
|
|
decrementColor(this.layerColor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
moveToFront() {
|
2025-10-17 23:54:45 +02:00
|
|
|
const _map = get(map);
|
|
|
|
|
if (!_map) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_map.getLayer(this.fileId)) {
|
|
|
|
|
_map.moveLayer(this.fileId);
|
2024-05-03 15:59:34 +02:00
|
|
|
}
|
2025-10-17 23:54:45 +02:00
|
|
|
if (_map.getLayer(this.fileId + '-direction')) {
|
2025-10-24 20:06:54 +02:00
|
|
|
_map.moveLayer(this.fileId + '-direction');
|
2024-05-08 14:53:28 +02:00
|
|
|
}
|
2024-04-25 13:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-10 20:03:57 +02:00
|
|
|
layerOnMouseEnter(e: any) {
|
|
|
|
|
let trackIndex = e.features[0].properties.trackIndex;
|
|
|
|
|
let segmentIndex = e.features[0].properties.segmentIndex;
|
|
|
|
|
|
2025-02-02 11:17:22 +01:00
|
|
|
if (
|
|
|
|
|
get(currentTool) === Tool.SCISSORS &&
|
|
|
|
|
get(selection).hasAnyParent(
|
|
|
|
|
new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex)
|
|
|
|
|
)
|
|
|
|
|
) {
|
2025-10-18 16:10:08 +02:00
|
|
|
mapCursor.notify(MapCursorState.SCISSORS, true);
|
2024-06-10 20:03:57 +02:00
|
|
|
} else {
|
2025-10-18 16:10:08 +02:00
|
|
|
mapCursor.notify(MapCursorState.LAYER_HOVER, true);
|
2024-06-10 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layerOnMouseLeave() {
|
2025-10-18 16:10:08 +02:00
|
|
|
mapCursor.notify(MapCursorState.SCISSORS, false);
|
|
|
|
|
mapCursor.notify(MapCursorState.LAYER_HOVER, false);
|
2024-06-10 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-08 15:49:14 +02:00
|
|
|
layerOnMouseMove(e: any) {
|
2025-06-04 18:41:28 +02:00
|
|
|
if (e.originalEvent.shiftKey) {
|
2024-10-08 15:49:14 +02:00
|
|
|
let trackIndex = e.features[0].properties.trackIndex;
|
|
|
|
|
let segmentIndex = e.features[0].properties.segmentIndex;
|
|
|
|
|
|
|
|
|
|
const file = get(this.file)?.file;
|
|
|
|
|
if (file) {
|
2025-02-02 11:17:22 +01:00
|
|
|
const closest = getClosestLinePoint(
|
|
|
|
|
file.trk[trackIndex].trkseg[segmentIndex].trkpt,
|
|
|
|
|
{ lat: e.lngLat.lat, lon: e.lngLat.lng }
|
|
|
|
|
);
|
2024-10-08 15:49:14 +02:00
|
|
|
trackpointPopup?.setItem({ item: closest, fileId: this.fileId });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 20:03:57 +02:00
|
|
|
layerOnClick(e: any) {
|
2025-02-02 11:17:22 +01:00
|
|
|
if (
|
|
|
|
|
get(currentTool) === Tool.ROUTING &&
|
|
|
|
|
get(selection).hasAnyChildren(new ListRootItem(), true, ['waypoints'])
|
|
|
|
|
) {
|
2024-04-25 13:56:07 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2024-05-24 13:16:41 +02:00
|
|
|
|
2024-06-10 20:03:57 +02:00
|
|
|
let trackIndex = e.features[0].properties.trackIndex;
|
|
|
|
|
let segmentIndex = e.features[0].properties.segmentIndex;
|
|
|
|
|
|
2025-02-02 11:17:22 +01:00
|
|
|
if (
|
|
|
|
|
get(currentTool) === Tool.SCISSORS &&
|
|
|
|
|
get(selection).hasAnyParent(
|
|
|
|
|
new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex)
|
|
|
|
|
)
|
|
|
|
|
) {
|
2025-10-17 23:54:45 +02:00
|
|
|
fileActions.split(get(splitAs), this.fileId, trackIndex, segmentIndex, {
|
2025-02-02 11:17:22 +01:00
|
|
|
lat: e.lngLat.lat,
|
|
|
|
|
lon: e.lngLat.lng,
|
|
|
|
|
});
|
2024-06-10 20:03:57 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 13:16:41 +02:00
|
|
|
let file = get(this.file)?.file;
|
|
|
|
|
if (!file) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let item = undefined;
|
2025-02-02 11:17:22 +01:00
|
|
|
if (get(treeFileView) && file.getSegments().length > 1) {
|
|
|
|
|
// Select inner item
|
|
|
|
|
item =
|
|
|
|
|
file.children[trackIndex].children.length > 1
|
|
|
|
|
? new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex)
|
|
|
|
|
: new ListTrackItem(this.fileId, trackIndex);
|
2024-05-24 13:16:41 +02:00
|
|
|
} else {
|
|
|
|
|
item = new ListFileItem(this.fileId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 09:44:27 +02:00
|
|
|
if (e.originalEvent.ctrlKey || e.originalEvent.metaKey) {
|
2025-10-17 23:54:45 +02:00
|
|
|
selection.addSelectItem(item);
|
2024-04-25 13:48:31 +02:00
|
|
|
} else {
|
2025-10-17 23:54:45 +02:00
|
|
|
selection.selectItem(item);
|
2024-04-25 13:48:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 10:13:03 +02:00
|
|
|
layerOnContextMenu(e: any) {
|
|
|
|
|
if (e.originalEvent.ctrlKey) {
|
|
|
|
|
this.layerOnClick(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 13:48:31 +02:00
|
|
|
getGeoJSON(): GeoJSON.FeatureCollection {
|
2024-05-08 21:31:54 +02:00
|
|
|
let file = get(this.file)?.file;
|
2024-05-03 15:59:34 +02:00
|
|
|
if (!file) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'FeatureCollection',
|
2025-02-02 11:17:22 +01:00
|
|
|
features: [],
|
2024-05-03 15:59:34 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let data = file.toGeoJSON();
|
2024-05-23 11:21:57 +02:00
|
|
|
|
2025-02-02 11:17:22 +01:00
|
|
|
let trackIndex = 0,
|
|
|
|
|
segmentIndex = 0;
|
2024-04-25 13:48:31 +02:00
|
|
|
for (let feature of data.features) {
|
|
|
|
|
if (!feature.properties) {
|
|
|
|
|
feature.properties = {};
|
|
|
|
|
}
|
|
|
|
|
if (!feature.properties.color) {
|
|
|
|
|
feature.properties.color = this.layerColor;
|
|
|
|
|
}
|
|
|
|
|
if (!feature.properties.opacity) {
|
2024-06-19 16:15:21 +02:00
|
|
|
feature.properties.opacity = get(defaultOpacity);
|
2024-04-25 13:48:31 +02:00
|
|
|
}
|
2025-01-01 20:01:46 +01:00
|
|
|
if (!feature.properties.width) {
|
|
|
|
|
feature.properties.width = get(defaultWidth);
|
|
|
|
|
}
|
2025-02-02 11:17:22 +01:00
|
|
|
if (
|
|
|
|
|
get(selection).hasAnyParent(
|
|
|
|
|
new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex)
|
|
|
|
|
) ||
|
|
|
|
|
get(selection).hasAnyChildren(new ListWaypointsItem(this.fileId), true)
|
|
|
|
|
) {
|
2025-01-01 14:40:28 +01:00
|
|
|
feature.properties.width = feature.properties.width + 2;
|
2024-06-19 16:15:21 +02:00
|
|
|
feature.properties.opacity = Math.min(1, feature.properties.opacity + 0.1);
|
2024-05-23 11:21:57 +02:00
|
|
|
}
|
2024-05-24 13:16:41 +02:00
|
|
|
feature.properties.trackIndex = trackIndex;
|
|
|
|
|
feature.properties.segmentIndex = segmentIndex;
|
2024-05-08 21:31:54 +02:00
|
|
|
|
2024-05-23 11:21:57 +02:00
|
|
|
segmentIndex++;
|
|
|
|
|
if (segmentIndex >= file.trk[trackIndex].trkseg.length) {
|
|
|
|
|
segmentIndex = 0;
|
|
|
|
|
trackIndex++;
|
2024-05-08 21:31:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-23 11:21:57 +02:00
|
|
|
return data;
|
2024-05-08 21:31:54 +02:00
|
|
|
}
|
2025-02-02 11:17:22 +01:00
|
|
|
}
|