2024-04-17 11:44:37 +02:00
|
|
|
import { writable } from 'svelte/store';
|
|
|
|
|
|
|
|
import mapboxgl from 'mapbox-gl';
|
2024-04-18 10:55:55 +02:00
|
|
|
import { GPXFile, parseGPX } from 'gpx';
|
2024-04-17 11:44:37 +02:00
|
|
|
|
2024-04-17 16:46:51 +02:00
|
|
|
export const map = writable<mapboxgl.Map | null>(null);
|
2024-04-18 10:55:55 +02:00
|
|
|
export const files = writable<GPXFile[]>([]);
|
|
|
|
|
|
|
|
export function triggerFileInput() {
|
|
|
|
const input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.accept = '.gpx';
|
|
|
|
input.multiple = true;
|
|
|
|
input.className = 'hidden';
|
|
|
|
input.onchange = () => {
|
|
|
|
if (input.files) {
|
|
|
|
loadFiles(input.files);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadFiles(files: FileList) {
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
loadFile(files[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadFile(file: File) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
let data = reader.result?.toString() ?? null;
|
|
|
|
if (data) {
|
|
|
|
let gpx = parseGPX(data);
|
|
|
|
if (gpx.metadata.name === undefined) {
|
|
|
|
gpx.metadata['name'] = file.name.split('.').slice(0, -1).join('.');
|
|
|
|
}
|
|
|
|
files.update($files => [...$files, gpx]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|