Files
gpx.studio/website/src/lib/logic/bounds.ts
T

149 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-10-19 16:14:05 +02:00
import { get } from 'svelte/store';
2026-01-30 21:01:24 +01:00
import maplibregl from 'maplibre-gl';
2025-10-19 16:14:05 +02:00
import { map } from '$lib/components/map/map';
import { page } from '$app/state';
2026-10-04 13:18:18 +02:00
import { engine, FileStateCollectionObserver, type FileState } from '$lib/engine';
const { statistics } = engine;
2025-10-19 16:14:05 +02:00
export class BoundsManager {
2026-01-30 21:01:24 +01:00
private _bounds: maplibregl.LngLatBounds = new maplibregl.LngLatBounds();
2025-10-19 16:14:05 +02:00
private _files: Set<string> = new Set();
2026-10-04 13:18:18 +02:00
private _fileStateCollectionObserver: FileStateCollectionObserver | null = null;
2025-10-19 16:14:05 +02:00
private _unsubscribes: (() => void)[] = [];
constructor() {
2026-10-04 13:18:18 +02:00
this._fileStateCollectionObserver = new FileStateCollectionObserver(
2025-10-19 16:14:05 +02:00
(newFiles) => {
if (page.url.hash.length == 0) {
this.fitBoundsOnLoad(Array.from(newFiles.keys()));
}
},
(fileId) => {},
() => {}
);
}
fitBoundsOnLoad(files: string[]) {
this.reset();
this._files = new Set(files);
2026-10-04 13:18:18 +02:00
this._fileStateCollectionObserver = new FileStateCollectionObserver(
2025-10-19 16:14:05 +02:00
(newFiles) => {
newFiles.forEach((fileState, fileId) => {
if (this._files.has(fileId)) {
this._unsubscribes.push(
fileState.subscribe((state) => {
this.addBoundsFromFile(fileId, state);
})
);
}
});
},
(fileId) => {},
() => {}
);
}
2026-10-04 13:18:18 +02:00
addBoundsFromFile(fileId: string, file: FileState) {
2025-10-19 16:14:05 +02:00
if (!file || !this._files.has(fileId)) return;
this._files.delete(fileId);
2026-10-04 13:18:18 +02:00
if (file.statistics.bounds) {
let bounds = new maplibregl.LngLatBounds([
file.statistics.bounds.west,
file.statistics.bounds.south,
file.statistics.bounds.east,
file.statistics.bounds.north,
]);
if (!this.validBounds(bounds)) return;
this._bounds.extend(bounds);
}
2025-10-19 16:14:05 +02:00
if (this._files.size === 0) {
this.finalizeFitBounds();
}
}
finalizeFitBounds() {
if (
2025-11-17 22:53:48 +01:00
this._bounds.getSouth() >= this._bounds.getNorth() &&
this._bounds.getWest() >= this._bounds.getEast()
2025-10-19 16:14:05 +02:00
) {
return;
}
this._unsubscribes.push(
map.subscribe((map_) => {
if (!map_) return;
2026-06-03 18:48:07 +02:00
map_.fitBounds(this._bounds, { padding: 80, linear: true, animate: false });
2025-10-19 16:14:05 +02:00
this.reset();
})
);
}
reset() {
if (this._fileStateCollectionObserver) {
this._fileStateCollectionObserver.destroy();
}
this._unsubscribes.forEach((unsubscribe) => unsubscribe());
this._unsubscribes = [];
2026-01-30 21:01:24 +01:00
this._bounds = new maplibregl.LngLatBounds([180, 90, -180, -90]);
2025-10-19 16:14:05 +02:00
}
centerMapOnSelection() {
2026-10-08 08:15:22 +02:00
const bounds = new maplibregl.LngLatBounds([180, 90, -180, -90]);
const stats = get(statistics);
2026-10-04 13:18:18 +02:00
if (stats.global.bounds) {
2026-10-08 08:15:22 +02:00
bounds.extend([
[stats.global.bounds.west, stats.global.bounds.south],
[stats.global.bounds.east, stats.global.bounds.north],
2026-10-04 13:18:18 +02:00
]);
2025-10-19 16:14:05 +02:00
}
2026-10-08 08:15:22 +02:00
// the waypoints are not part of the statistics of the selection
const selection = get(engine.selection);
let waypointIds: Set<string> | undefined; // all the waypoints of the file if undefined
let fileIds: string[] = [];
switch (selection.type) {
case 'file':
fileIds = selection.fileIds;
break;
case 'waypoints':
fileIds = [selection.fileId];
break;
case 'waypoint':
fileIds = [selection.fileId];
waypointIds = new Set(selection.waypointIds);
break;
}
const files = get(engine.files);
for (const fileId of fileIds) {
const file = files.get(fileId);
if (!file) continue;
for (const feature of get(file).waypoints.features) {
if (waypointIds && !waypointIds.has(feature.properties.waypointId)) continue;
bounds.extend(feature.geometry.coordinates as [number, number]);
}
}
if (!this.validBounds(bounds)) return;
get(map)?.fitBounds(bounds, {
padding: 80,
easing: () => 1,
maxZoom: 15,
});
2025-10-19 16:14:05 +02:00
}
2026-10-04 13:18:18 +02:00
validBounds(bounds: maplibregl.LngLatBounds) {
2025-10-19 16:14:05 +02:00
return (
2026-10-04 13:18:18 +02:00
bounds.getSouthWest().lat !== 90 ||
bounds.getSouthWest().lng !== 180 ||
bounds.getNorthEast().lat !== -90 ||
bounds.getNorthEast().lng !== -180
2025-10-19 16:14:05 +02:00
);
}
}
export const boundsManager = new BoundsManager();