Files
gods-eye-view/src/data/geoImportShared.js
T
Sameh Khamis c49191a87b Keep the file import's Cesium loading in its pins module and take the file reader off the Cesium list
src/data/geoFileImport.js now only reads, bounds and sanitizes the
imported files (GeoJSON, GPX, KML and KMZ). Loading the parsed file as a
Cesium data source, scrubbing its entities, settling its ground pins,
the pin billboard collection and the footprint read for Fly-to move into
src/data/geoImportPins.js, which stays on the Cesium list as the
import's Cesium-bound part. The error, the entity limit, the one-minute
deadline and feature-name cleanup both parts use live in the new
src/data/geoImportShared.js; the reader re-exports them for its
existing importers.

Nothing changes for the person importing: the same files load with the
same refusals, styles, ground heights, pins and framing.
2026-11-07 16:56:49 -08:00

58 lines
1.9 KiB
JavaScript

/** What the file import's parsing and drawing parts share: its error, its limits, its deadline and feature names. */
import { msg } from '../i18n/marker.js';
/** Most entities one file may create before it is refused. */
export const MAX_IMPORT_ENTITIES = 20_000;
/** Longest plain-text name kept from a file's features. */
const MAX_IMPORT_NAME_LENGTH = 120;
/**
* An error whose message is meant for the person who chose the file. The
* message stays English; `template` and `values`, when given, let the UI show
* it in the active language. Numbers stay numbers in `values`: the UI formats
* them in that language's numerals.
*/
export class GeoImportError extends Error {
constructor(message, template = message, values = {}) {
super(message);
this.name = 'GeoImportError';
this.template = template;
this.values = values;
}
}
/** A feature name as short plain text (no control characters), or undefined. */
export function importFeatureName(value) {
const text = String(value ?? '')
.replace(/[\u0000-\u001f\u007f]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
if (!text) return undefined;
return text.length > MAX_IMPORT_NAME_LENGTH
? `${text.slice(0, MAX_IMPORT_NAME_LENGTH - 1)}…`
: text;
}
/** `promise`, or a refusal when the import is aborted or takes over a minute. */
export async function boundedImport(promise, signal) {
let timer, abort;
try {
signal?.throwIfAborted();
return await Promise.race([
promise,
new Promise((_, reject) => {
abort = () => reject(signal.reason);
signal?.addEventListener('abort', abort, { once: true });
timer = setTimeout(
() =>
reject(new GeoImportError(msg('That file took too long to read.'))),
60_000,
);
}),
]);
} finally {
clearTimeout(timer);
signal?.removeEventListener('abort', abort);
}
}