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

124 lines
3.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-04 13:18:18 +02:00
let stats = get(statistics);
if (stats.global.bounds) {
// TODO take waypoints into account
let bounds = new maplibregl.LngLatBounds([
stats.global.bounds.west,
stats.global.bounds.south,
stats.global.bounds.east,
stats.global.bounds.north,
]);
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();