Import GeoJSON, KML, KMZ and GPX files on Navara: shapes as clamped vector layers, pins as sprites, and the graphics it cannot draw counted and reported

This commit is contained in:
German Khokhlov committed 2026-11-03 21:19:55 +04:00
1 parent eee4f48745
commit d8c63c69d8
22 files changed
+1368 -68

No files matched your search

+113 -49
View File
@@ -42,11 +42,14 @@ export function initGeoFileImport({ viewer, ...options }) {
* and returns their disconnect.
*
* The imported files belong to the session; their drawing belongs to the
* attached Cesium viewer. `attachScene({ viewer })` draws them (a file kept
* while no viewer was attached is read again from its bytes) and listens for
* drops and camera gestures there; `detachScene()` takes them off the viewer
* and keeps the list. Without a viewer (an engine without Cesium) the list
* stays, and import and fly-to wait for one.
* attached globe. `attachScene({ viewer })` draws them on a Cesium viewer;
* `attachScene({ engine, draw })` on a globe engine without one (Navara),
* where `draw(dataSource)` resolves with that engine's drawing of a file
* ({ report, setShow, refresh, destroy }: src/layers/shared/render/navaraStory.js).
* A file kept while no globe was attached is read again from its bytes; the
* control listens for drops and camera gestures on the attached globe.
* `detachScene()` takes the files off the globe and keeps the list. Without a
* globe the list stays, and import and fly-to wait for one.
* @returns {{destroy: Function, importFiles: Function, list: Function,
* setVisible: Function, flyTo: Function, remove: Function,
* diagnostics: Function, attachScene: Function, detachScene: Function}|null}
@@ -75,8 +78,8 @@ export function createGeoFileImport({
let cameraIntent = 0;
let hint = '';
const lifetime = new AbortController();
// The attached viewer and the lifetime of that attachment.
let viewer = null;
// The attached globe ({ viewer, engine, draw, controller }): a Cesium
// viewer, or an engine and its drawing; null while none is attached.
let scene = null;
const listen = (target, type, listener, options) => {
@@ -88,13 +91,16 @@ export function createGeoFileImport({
hint = text || '';
present();
};
const render = () => viewer?.scene?.requestRender?.();
const render = () => {
if (scene?.viewer) scene.viewer.scene?.requestRender?.();
else scene?.engine?.render.request('geo-import');
};
let groundTimer,
groundQueued = false,
groundAgain = false;
let groundRemovers = [];
const scheduleGround = () => {
if (destroyed || !viewer || !imports.length) return;
if (destroyed || !scene || !imports.length) return;
if (groundQueued) {
groundAgain = true;
return;
@@ -109,8 +115,10 @@ export function createGeoFileImport({
try {
for (const item of imports) {
if (destroyed || scene !== attached) return;
if (item.visible)
if (item.visible) {
await item.refreshGround?.(attached.controller.signal);
item.drawing?.refresh?.();
}
}
render();
} catch {
@@ -182,6 +190,7 @@ export function createGeoFileImport({
if (!item) return false;
item.visible = Boolean(visible);
if (item.dataSource) item.dataSource.show = item.visible;
item.drawing?.setShow(item.visible);
if (item.visible) scheduleGround();
present();
render();
@@ -189,9 +198,9 @@ export function createGeoFileImport({
}
const fly = (item) => {
if (!viewer || !item.dataSource) return false;
if (!scene || !item.dataSource) return false;
if (!item.visible) setVisible(item.id, true);
flyToSource(viewer, item.dataSource, { ground });
flyToSource(scene.viewer ?? scene.engine, item.dataSource, { ground });
return true;
};
@@ -201,7 +210,7 @@ export function createGeoFileImport({
*/
function flyTo(id) {
const item = find(id);
if (!item || !viewer || !item.dataSource) return false;
if (!item || !scene || !item.dataSource) return false;
if (typeof navigation?.runImmediateNavigation === 'function')
return (
navigation.runImmediateNavigation('file', () => fly(item)) !== false
@@ -223,9 +232,12 @@ export function createGeoFileImport({
: true;
};
/** Take one file's drawing off the viewer; the file stays in the list. */
function undraw(item, from = viewer) {
if (item.dataSource) from?.dataSources?.remove?.(item.dataSource, true);
/** Take one file's drawing off the globe; the file stays in the list. */
function undraw(item, from = scene) {
if (item.dataSource)
from?.viewer?.dataSources?.remove?.(item.dataSource, true);
item.drawing?.destroy();
item.drawing = null;
item.revoke?.();
item.dataSource = null;
item.revoke = null;
@@ -243,17 +255,27 @@ export function createGeoFileImport({
return true;
}
/** Read a file for the attached viewer. Null once that viewer went. */
/** Draw a file read for the attached globe. False once that globe went. */
async function draw(item, attached, loaded) {
item.dataSource = loaded.dataSource;
item.revoke = loaded.revoke;
item.refreshGround = loaded.refreshGround;
item.dataSource.show = item.visible;
await attached.viewer.dataSources.add(item.dataSource);
if (attached.viewer) await attached.viewer.dataSources.add(item.dataSource);
else {
try {
item.drawing = await attached.draw(item.dataSource);
} catch (error) {
undraw(item, attached);
throw error;
}
}
if (destroyed || scene !== attached) {
undraw(item, attached.viewer);
undraw(item, attached);
return false;
}
// What this globe leaves out of the file (Navara: labels, models...).
item.undrawn = item.drawing?.report?.undrawn ?? 0;
loaded.mount?.();
return true;
}
@@ -267,8 +289,8 @@ export function createGeoFileImport({
function importFiles(files) {
const list = [...(files || [])];
if (destroyed || !list.length) return Promise.resolve([]);
if (!viewer) {
// Import draws on the Cesium globe; this engine has none.
if (!scene) {
// Import draws on the attached globe; there is none.
const error = t('Action unavailable or cancelled');
setHint(error);
return Promise.resolve(
@@ -311,6 +333,7 @@ export function createGeoFileImport({
try {
loaded = await load(file, {
viewer: attached.viewer,
engine: attached.engine,
color,
signal: attached.controller.signal,
});
@@ -343,13 +366,26 @@ export function createGeoFileImport({
removed: loaded.removed || 0,
color,
visible: true,
// The bytes, so a later viewer can read the file again.
// The bytes, so a later globe can read the file again.
file,
dataSource: null,
drawing: null,
undrawn: 0,
revoke: null,
refreshGround: null,
};
if (!(await draw(item, attached, loaded))) break;
try {
if (!(await draw(item, attached, loaded))) break;
} catch (error) {
// A Cesium viewer's own failure stops the batch, as it always did.
if (attached.viewer) throw error;
outcomes.push({
name: fileName,
ok: false,
error: t('That file could not be read.'),
});
continue;
}
imports.push(item);
present();
render();
@@ -358,6 +394,7 @@ export function createGeoFileImport({
ok: true,
id: item.id,
removed: item.removed,
undrawn: item.undrawn,
});
if (
outcomes.filter((o) => o.ok).length === 1 &&
@@ -370,7 +407,7 @@ export function createGeoFileImport({
return outcomes;
}
// Draw the files kept while no viewer was attached, as they were.
// Draw the files kept while no globe was attached, as they were.
async function redraw(attached) {
for (const item of [...imports]) {
if (destroyed || scene !== attached) return;
@@ -378,6 +415,7 @@ export function createGeoFileImport({
try {
const loaded = await load(item.file, {
viewer: attached.viewer,
engine: attached.engine,
color: item.color,
signal: attached.controller.signal,
});
@@ -392,6 +430,9 @@ export function createGeoFileImport({
}
}
if (!destroyed && scene === attached) {
// Say what this globe leaves out of the files it drew again.
const left = imports.filter((item) => item.undrawn);
if (left.length) setHint(undrawnHint(left));
present();
render();
}
@@ -423,35 +464,42 @@ export function createGeoFileImport({
target.removeEventListener(type, listener, options);
dragDepth = 0;
setDragging(false);
for (const item of imports) undraw(item, previous.viewer);
viewer = null;
for (const item of imports) undraw(item, previous);
}
function attachScene({
viewer: next = null,
viewer = null,
engine = null,
draw: drawOnEngine = null,
navigation: nextNavigation = navigation,
subscribeMapSource: nextMapSource = subscribeMapSource,
} = {}) {
if (destroyed) return;
detachScene();
if (!next) return;
viewer = next;
if (!viewer && !(engine && drawOnEngine)) return;
// The shell and the map sources of this attachment.
navigation = nextNavigation;
const offMapSource = nextMapSource?.(scheduleGround);
if (typeof offMapSource === 'function') groundRemovers.push(offMapSource);
const attached = (scene = { viewer, controller: new AbortController() });
ownEvent(viewer.camera?.moveEnd, scheduleGround);
ownEvent(viewer.scene?.globe?.tileLoadProgressEvent, (pending) => {
const attached = (scene = {
viewer,
engine: viewer ? null : engine,
draw: viewer ? null : drawOnEngine,
controller: new AbortController(),
});
ownEvent(
viewer ? viewer.camera?.moveEnd : engine.camera.moveEnd,
scheduleGround,
);
ownEvent(viewer?.scene?.globe?.tileLoadProgressEvent, (pending) => {
if (!pending) scheduleGround();
});
const container = viewer.container;
listen(viewer.scene?.canvas || container, 'pointerdown', gesture, {
passive: true,
});
listen(viewer.scene?.canvas || container, 'wheel', gesture, {
passive: true,
});
const container = viewer ? viewer.container : engine.host.container;
const canvas = viewer
? viewer.scene?.canvas || container
: engine.host.canvas;
listen(canvas, 'pointerdown', gesture, { passive: true });
listen(canvas, 'wheel', gesture, { passive: true });
listen(container, 'dragenter', (event) => {
if (!hasFiles(event)) return;
event.preventDefault();
@@ -487,14 +535,17 @@ export function createGeoFileImport({
const handle = {
importFiles,
list: () =>
imports.map(({ id, name, format, entityCount, removed, visible }) => ({
id,
name,
format,
entityCount,
removed,
visible,
})),
imports.map(
({ id, name, format, entityCount, removed, visible, undrawn }) => ({
id,
name,
format,
entityCount,
removed,
visible,
undrawn,
}),
),
setVisible,
flyTo,
remove,
@@ -505,12 +556,12 @@ export function createGeoFileImport({
drawn: imports.filter((item) => item.dataSource).length,
entities: imports.reduce((n, item) => n + item.entityCount, 0),
listeners: listeners.length,
attached: Boolean(viewer),
attached: Boolean(scene),
destroyed,
}),
destroy() {
if (destroyed) return;
const last = viewer;
const last = scene;
detachScene();
destroyed = true;
disconnect();
@@ -558,5 +609,18 @@ export function summarize(outcomes) {
0,
);
if (added && stripped) parts.push(t('Some KML content was left out.'));
const left = outcomes.filter((o) => o.ok && o.undrawn);
if (left.length) parts.push(undrawnHint(left));
return parts.join(' ');
}
/**
* What the attached globe leaves out of the given files (each `{ name,
* undrawn }`): the first file's name and the total count.
*/
export function undrawnHint(items) {
return t('Map objects this engine leaves out of {name}: {count}.', {
name: items[0].name,
count: formatNumber(items.reduce((n, item) => n + item.undrawn, 0)),
});
}