adds KMZ support

This commit is contained in:
Christian Beutel
2025-03-15 10:24:05 +01:00
parent 2e6ae6f934
commit a49830bcf1
2 changed files with 35 additions and 16 deletions

BIN
db/__debug_bin2531684965 Executable file

Binary file not shown.

View File

@@ -14,6 +14,7 @@ import EasyFit from "$lib/vendor/easy-fit/easy-fit";
import type { Feature, FeatureCollection, GeoJSON, GeoJsonProperties, Position } from 'geojson';
import * as xmldom from 'xmldom';
import { bbox, splitMultiLineStringToLineStrings } from "./geojson_util";
import JSZip from "jszip";
export async function gpx2trail(gpxString: string, fallbackName?: string) {
@@ -108,8 +109,19 @@ export async function fromFile(file: File | Blob) {
const fileContent = await file.text();
const fileBuffer = await file.arrayBuffer();
if (!isFITFile(fileBuffer)) {
if (fileContent.includes("http://www.opengis.net/kml")) {
if (isFITFile(fileBuffer)) {
gpxData = await fromFIT(fileBuffer);
gpxFile = new Blob([gpxData], {
type: "application/gpx+xml",
});
} else if (isKMZFile(fileBuffer)) {
gpxData = await fromKMZ(fileBuffer);
gpxFile = new Blob([gpxData], {
type: "application/gpx+xml",
});
} else if (fileContent.includes("http://www.opengis.net/kml")) {
gpxData = fromKML(fileContent);
gpxFile = new Blob([gpxData], {
type: "application/gpx+xml",
@@ -123,12 +135,7 @@ export async function fromFile(file: File | Blob) {
gpxData = fileContent;
gpxFile = file;
}
} else {
gpxData = await fromFIT(fileBuffer);
gpxFile = new Blob([gpxData], {
type: "application/gpx+xml",
});
}
return { gpxData, gpxFile };
}
@@ -143,6 +150,13 @@ export function fromKML(kmlData: string) {
return gpx
}
export async function fromKMZ(kmzData: ArrayBuffer) {
const zip = new JSZip()
const zipContents = await zip.loadAsync(kmzData)
const kmlFile = await zip.file('doc.kml')?.async('string');
return fromKML(kmlFile ?? "")
}
export function fromTCX(tcxData: string) {
const parser = browser ? new DOMParser() : new xmldom.DOMParser();
const nodes = parser.parseFromString(tcxData, "text/xml")
@@ -315,6 +329,11 @@ export function isFITFile(buffer: ArrayBuffer) {
return true;
}
function isKMZFile(buffer: ArrayBuffer) {
const blob = new Uint8Array(buffer);
return blob[0] === 0x50 && blob[1] === 0x4B && blob[2] === 0x03 && blob[3] === 0x04;
}
export function toGeoJson(gpxData: string) {
const parser = browser ? new DOMParser() : new xmldom.DOMParser();
let geojson = gpx(