feat(tours): import KML and KMZ routes as tours, with a preview first

A route planned in Google My Maps or Google Earth can now become a tour
without converting it to GPX first. The import button and a file dropped on
the tours workspace take .kml and .kmz files beside .gpx, through the same
preview, the same import routes and the same transaction as a GPX file.

The preview shows every line of the file on its own, numbered, with its
distance and heights; two lines of one placemark are numbered after its name
and never joined. Heights count only where they are heights: kept for an
absolute line, dropped for any other altitude mode and for the all-zero
placeholder My Maps writes, so a missing height stays unknown. What the file
holds besides its lines (pins, areas, overlays, links to other files) is
counted in a dashed card, and nothing is fetched from elsewhere.

The KML reading reuses the place import's codec. A KMZ only ever has its KML
entry unpacked, counted byte by byte against the decompressed cap whatever
its header claims. A file that cannot be read is refused with its reason
(damaged, an archive without KML, too large unpacked), and the toast names
the file and the reason. A KML or KMZ import keeps the gpx origin of a file
import; its provenance names the real format from the stored file name.
This commit is contained in:
Maurice
2026-10-20 12:28:57 +02:00
parent f57a0dbd31
commit 3659297af1
58 changed files with 1883 additions and 400 deletions
@@ -55,3 +55,34 @@ describe('Tours GPX preview', () => {
expect(tourGpxPreviewResponseSchema.parse(preview)).toEqual(preview);
});
});
describe('Tours KML preview (IO-02)', () => {
const track = {
name: 'Ridge (1/2)',
geometry: [
[47, 11],
[47.01, 11.01],
],
point_count: 2,
distance_km: 1.4,
elevation_gain_m: null,
elevation_loss_m: null,
duplicate: false,
};
it('counts what a KML file held besides its lines, and a GPX preview may leave that out', () => {
const kml = { tracks: [track], left_out: { points: 3, areas: 1, other: 0 } };
expect(tourGpxPreviewResponseSchema.parse(kml)).toEqual(kml);
expect(tourGpxPreviewResponseSchema.parse({ tracks: [track] })).toEqual({ tracks: [track] });
});
it('refuses a count that is negative or not whole', () => {
for (const left_out of [
{ points: -1, areas: 0, other: 0 },
{ points: 0, areas: 0.5, other: 0 },
{ points: 0, areas: 0 },
]) {
expect(tourGpxPreviewResponseSchema.safeParse({ tracks: [track], left_out }).success).toBe(false);
}
});
});
@@ -14,6 +14,10 @@ import { z } from 'zod';
* runs the file through the same parser the import uses and answers what it
* found; nothing is written. The import that follows takes the type the user
* chose for it.
*
* IO-02: the same routes read a KML or KMZ file (by its extension) for its
* lines, each line one track, and say what else the file held that no tour is
* made of.
*/
const previewPointSchema = z.union([z.tuple([z.number(), z.number()]), z.tuple([z.number(), z.number(), z.number()])]);
@@ -39,8 +43,23 @@ export const tourGpxPreviewTrackSchema = z.object({
});
export type TourGpxPreviewTrack = z.infer<typeof tourGpxPreviewTrackSchema>;
const leftOutCount = z.number().int().nonnegative();
/** What a KML or KMZ file held besides its lines, counted by kind; none of it is imported. */
export const tourFileLeftOutSchema = z.object({
/** Single points (pins). */
points: leftOutCount,
/** Polygons. */
areas: leftOutCount,
/** Models, overlays, links to other files (never followed), placemarks without a geometry, too short lines. */
other: leftOutCount,
});
export type TourFileLeftOut = z.infer<typeof tourFileLeftOutSchema>;
export const tourGpxPreviewResponseSchema = z.object({
tracks: z.array(tourGpxPreviewTrackSchema),
/** Only a KML or KMZ preview counts what it leaves out; a GPX preview never counted its loose waypoints. */
left_out: tourFileLeftOutSchema.optional(),
});
export type TourGpxPreviewResponse = z.infer<typeof tourGpxPreviewResponseSchema>;