This commit is contained in:
vcoppe
2026-10-04 13:18:18 +02:00
parent 03cbacfbb4
commit 1d534881f8
+32 -42
View File
@@ -1,22 +1,19 @@
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import { selection } from '$lib/logic/selection';
import maplibregl from 'maplibre-gl'; import maplibregl from 'maplibre-gl';
import { ListFileItem, ListWaypointItem } from '$lib/components/file-list/file-list';
import { fileStateCollection, GPXFileStateCollectionObserver } from '$lib/logic/file-state';
import { gpxStatistics } from '$lib/logic/statistics';
import { map } from '$lib/components/map/map'; import { map } from '$lib/components/map/map';
import type { GPXFileWithStatistics } from './statistics-tree';
import type { Coordinates } from 'gpx';
import { page } from '$app/state'; import { page } from '$app/state';
import { engine, FileStateCollectionObserver, type FileState } from '$lib/engine';
const { statistics } = engine;
export class BoundsManager { export class BoundsManager {
private _bounds: maplibregl.LngLatBounds = new maplibregl.LngLatBounds(); private _bounds: maplibregl.LngLatBounds = new maplibregl.LngLatBounds();
private _files: Set<string> = new Set(); private _files: Set<string> = new Set();
private _fileStateCollectionObserver: GPXFileStateCollectionObserver | null = null; private _fileStateCollectionObserver: FileStateCollectionObserver | null = null;
private _unsubscribes: (() => void)[] = []; private _unsubscribes: (() => void)[] = [];
constructor() { constructor() {
this._fileStateCollectionObserver = new GPXFileStateCollectionObserver( this._fileStateCollectionObserver = new FileStateCollectionObserver(
(newFiles) => { (newFiles) => {
if (page.url.hash.length == 0) { if (page.url.hash.length == 0) {
this.fitBoundsOnLoad(Array.from(newFiles.keys())); this.fitBoundsOnLoad(Array.from(newFiles.keys()));
@@ -31,7 +28,7 @@ export class BoundsManager {
this.reset(); this.reset();
this._files = new Set(files); this._files = new Set(files);
this._fileStateCollectionObserver = new GPXFileStateCollectionObserver( this._fileStateCollectionObserver = new FileStateCollectionObserver(
(newFiles) => { (newFiles) => {
newFiles.forEach((fileState, fileId) => { newFiles.forEach((fileState, fileId) => {
if (this._files.has(fileId)) { if (this._files.has(fileId)) {
@@ -48,16 +45,21 @@ export class BoundsManager {
); );
} }
addBoundsFromFile(fileId: string, file: GPXFileWithStatistics | undefined) { addBoundsFromFile(fileId: string, file: FileState) {
if (!file || !this._files.has(fileId)) return; if (!file || !this._files.has(fileId)) return;
this._files.delete(fileId); this._files.delete(fileId);
const bounds = file.statistics.getStatisticsFor(new ListFileItem(fileId)).global.bounds; 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; if (!this.validBounds(bounds)) return;
this._bounds.extend(bounds);
this._bounds.extend(bounds.southWest); }
this._bounds.extend(bounds.northEast);
if (this._files.size === 0) { if (this._files.size === 0) {
this.finalizeFitBounds(); this.finalizeFitBounds();
@@ -91,42 +93,30 @@ export class BoundsManager {
} }
centerMapOnSelection() { centerMapOnSelection() {
let selected = get(selection).getSelected(); let stats = get(statistics);
let bounds = new maplibregl.LngLatBounds(); if (stats.global.bounds) {
// TODO take waypoints into account
if (selected.find((item) => item instanceof ListWaypointItem)) { let bounds = new maplibregl.LngLatBounds([
selection.applyToOrderedSelectedItemsFromFile((fileId, level, items) => { stats.global.bounds.west,
let file = fileStateCollection.getFile(fileId); stats.global.bounds.south,
if (file) { stats.global.bounds.east,
items.forEach((item) => { stats.global.bounds.north,
if (item instanceof ListWaypointItem) { ]);
let waypoint = file.wpt[item.getWaypointIndex()]; if (!this.validBounds(bounds)) return;
if (waypoint) {
bounds.extend([waypoint.getLongitude(), waypoint.getLatitude()]);
}
}
});
}
});
} else {
let selectionBounds = get(gpxStatistics).global.bounds;
bounds.setNorthEast(selectionBounds.northEast);
bounds.setSouthWest(selectionBounds.southWest);
}
get(map)?.fitBounds(bounds, { get(map)?.fitBounds(bounds, {
padding: 80, padding: 80,
easing: () => 1, easing: () => 1,
maxZoom: 15, maxZoom: 15,
}); });
} }
}
validBounds(bounds: { southWest: Coordinates; northEast: Coordinates }) { validBounds(bounds: maplibregl.LngLatBounds) {
return ( return (
bounds.southWest.lat !== 90 || bounds.getSouthWest().lat !== 90 ||
bounds.southWest.lon !== 180 || bounds.getSouthWest().lng !== 180 ||
bounds.northEast.lat !== -90 || bounds.getNorthEast().lat !== -90 ||
bounds.northEast.lon !== -180 bounds.getNorthEast().lng !== -180
); );
} }
} }