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

+401
View File
@@ -171,3 +171,404 @@ export function drawStorySource(engine, { view, NavaraColor }, data) {
},
};
}
// Graphics an imported file (UTILITIES ▸ Import) may carry that Navara does
// not draw; each one present is counted and reported.
const UNDRAWN_GRAPHICS = Object.freeze([
'label',
'model',
'rectangle',
'wall',
'ellipse',
'ellipsoid',
'corridor',
'box',
'cylinder',
'path',
'plane',
'polylineVolume',
'tileset',
]);
const DEGREES = 180 / Math.PI;
// Unique mover set ids for imported files.
let importSets = 0;
const css = (c) =>
'#' +
[c.red, c.green, c.blue]
.map((v) =>
Math.round(Math.max(0, Math.min(1, v)) * 255)
.toString(16)
.padStart(2, '0'),
)
.join('');
const lonLat = (position) => {
const c = position && geo.cartographic.fromCartesian(position);
return c && Number.isFinite(c.longitude) && Number.isFinite(c.latitude)
? [c.longitude * DEGREES, c.latitude * DEGREES]
: null;
};
const ringOf = (positions) => {
const ring = (positions ?? []).map(lonLat).filter(Boolean);
if (ring.length >= 3) {
const [first] = ring;
const last = ring[ring.length - 1];
if (first[0] !== last[0] || first[1] !== last[1]) ring.push([...first]);
}
return ring;
};
const colorOf = (material, fallback) => {
const value = val(material);
return value?.color ?? val(material?.color) ?? fallback;
};
/**
* The plan for an imported data source on Navara, as plain data: GeoJSON
* features grouped by paint (`fills` and `lines`: Map of style key →
* { color: '#rrggbb', opacity, width?, features }), point sprites (`points`)
* and billboards grouped by image (`images`: Map of image key →
* { image, sizePx, items }), and what is left out (`undrawn`: graphic kind →
* count; `drawn`: entities with something drawn).
*/
export function planImportedSource(dataSource, { sizes = new Map() } = {}) {
const fills = new Map();
const lines = new Map();
const points = [];
const images = new Map();
const undrawn = {};
let drawn = 0;
const group = (map, key, entry) => {
let target = map.get(key);
if (!target) map.set(key, (target = { ...entry, features: [] }));
return target;
};
for (const entity of dataSource.entities.values) {
if (val(entity.show) === false) continue;
let any = false;
const polygon = entity.polygon;
if (polygon && val(polygon.fill) !== false) {
const hierarchy = val(polygon.hierarchy);
const outer = ringOf(hierarchy?.positions);
if (outer.length >= 4) {
const holes = (hierarchy.holes ?? [])
.map((hole) => ringOf(hole?.positions))
.filter((ring) => ring.length >= 4);
const color = colorOf(polygon.material, null);
const fill = color ? css(color) : '#ffffff';
const opacity = color?.alpha ?? 1;
group(fills, `${fill}|${opacity.toFixed(3)}`, {
color: fill,
opacity,
}).features.push({
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [outer, ...holes] },
});
any = true;
}
}
const polyline = entity.polyline;
if (polyline) {
const path = (val(polyline.positions) ?? []).map(lonLat).filter(Boolean);
if (path.length >= 2) {
const color = colorOf(polyline.material, null);
const stroke = color ? css(color) : '#ffffff';
const opacity = color?.alpha ?? 1;
const width = Number(val(polyline.width)) || 1;
group(lines, `${stroke}|${opacity.toFixed(3)}|${width}`, {
color: stroke,
opacity,
width,
}).features.push({
type: 'Feature',
properties: {},
geometry: { type: 'LineString', coordinates: path },
});
any = true;
}
}
const position = val(entity.position);
if (position && entity.billboard && val(entity.billboard.image)) {
const raw = val(entity.billboard.image);
// A URL (a Cesium Resource's too), else a canvas; one sprite set per
// image (Cesium's pin builder hands every marker of a colour the same
// canvas).
const image =
typeof raw === 'string'
? raw
: typeof raw?.url === 'string'
? raw.url
: typeof raw?.getContext === 'function'
? raw
: typeof raw?.src === 'string'
? raw.src
: null;
if (!image) {
undrawn.billboard = (undrawn.billboard ?? 0) + 1;
continue;
}
const natural = sizes.get(image);
const width =
Number(val(entity.billboard.width)) ||
Number(image.width) ||
natural?.width ||
32;
const height =
Number(val(entity.billboard.height)) ||
Number(image.height) ||
natural?.height ||
width;
const entry = group(images, image, {
image,
sizePx: Math.max(width, height),
});
const tint = val(entity.billboard.color);
entry.features.push({
entity,
position,
scale: Number(val(entity.billboard.scale)) || 1,
color: tint ? rgba(tint) : [1, 1, 1, 1],
});
any = true;
} else if (position && entity.point) {
const p = entity.point;
const color = val(p.color);
const outline = val(p.outlineColor);
points.push({
entity,
position,
sizePx: Number(val(p.pixelSize)) || 1,
color: color ? rgba(color) : [1, 1, 1, 1],
outline: outline && Number(val(p.outlineWidth)) ? rgba(outline) : null,
});
any = true;
}
for (const kind of UNDRAWN_GRAPHICS) {
const graphic = entity[kind];
if (!graphic || val(graphic.show) === false) continue;
if (kind === 'label' && !val(graphic.text)) continue;
undrawn[kind] = (undrawn[kind] ?? 0) + 1;
}
if (any) drawn++;
}
return { fills, lines, points, images, undrawn, drawn };
}
/**
* Draw an imported file's data source (UTILITIES ▸ Import) on Navara: its
* polygons and lines as clamped GeoJSON vector layers (one per paint), its
* points as circle sprites and its billboards as sprites of their own image.
* Labels, models and the other graphics Navara has no drawing for are left
* out and counted in `report`. `setShow` follows the row's visibility;
* `refresh` re-reads the pins' positions after a ground refresh.
*/
export function drawImportedSource(
engine,
{ view },
dataSource,
{ sizes } = {},
) {
const plan = planImportedSource(dataSource, { sizes });
const layers = [];
const sets = [];
let shown = dataSource.show !== false;
const layerStyle = (kind, entry) =>
kind === 'polygon'
? {
polygon: {
color: Number.parseInt(entry.color.slice(1), 16),
show: shown,
clampToGround: true,
transparent: true,
opacity: entry.opacity,
},
}
: {
polyline: {
color: Number.parseInt(entry.color.slice(1), 16),
show: shown,
clampToGround: true,
width: entry.width,
transparent: true,
opacity: entry.opacity,
},
};
const id = (kind) => `geo-import-${kind}-${++importSets}`;
try {
for (const [kind, groups] of [
['polygon', plan.fills],
['polyline', plan.lines],
])
for (const entry of groups.values()) {
const source = view.addSource({
type: 'geojson',
data: { type: 'FeatureCollection', features: entry.features },
});
const record = { kind, entry, source, layer: null };
layers.push(record);
record.layer = view.addLayer({
type: 'vector',
source,
...layerStyle(kind, entry),
});
}
if (plan.points.length) {
const set = engine.movers.createSet({
id: id('points'),
kind: 'sprite',
capacity: plan.points.length * 2,
pickable: false,
depthTest: 'always',
sprite: { sizePx: 64, atlas: circleAtlas() },
});
const write = () => {
let at = 0;
for (const item of plan.points) {
const scale = item.sizePx / 58;
set.set(at++, {
position: item.position,
cell: 'fill',
color: item.color,
scale,
show: true,
});
if (item.outline)
set.set(at++, {
position: item.position,
cell: 'rim',
color: item.outline,
scale,
show: true,
});
}
return at;
};
sets.push({ set, count: write(), write });
}
for (const entry of plan.images.values()) {
const { image } = entry;
const set = engine.movers.createSet({
id: id('pins'),
kind: 'sprite',
capacity: entry.features.length,
pickable: false,
depthTest: 'always',
sprite: {
sizePx: entry.sizePx,
atlas: {
...(typeof image === 'string' ? { url: image } : { canvas: image }),
cells: { image: [0, 0, 1, 1] },
},
},
});
const write = () => {
entry.features.forEach((item, index) =>
set.set(index, {
position: item.position,
cell: 'image',
color: item.color,
scale: item.scale,
show: true,
}),
);
return entry.features.length;
};
sets.push({ set, count: write(), write });
}
for (const record of sets) {
record.set.setCount(shown ? record.count : 0);
record.set.commit();
}
} catch (error) {
release();
throw error;
}
engine.render.request('geo-import');
function release() {
for (const { layer, source } of layers.splice(0)) {
try {
layer?.delete();
source.delete();
} catch {
/* the view went first */
}
}
for (const { set } of sets.splice(0)) set.destroy();
}
const undrawn = Object.values(plan.undrawn).reduce((n, c) => n + c, 0);
return {
report: { drawn: plan.drawn, undrawn, kinds: { ...plan.undrawn } },
setShow(show) {
if (show === shown) return;
shown = show;
for (const { kind, entry, source, layer } of layers)
layer.update({ type: 'vector', source, ...layerStyle(kind, entry) });
for (const record of sets) {
record.set.setCount(shown ? record.count : 0);
record.set.commit();
}
engine.render.request('geo-import');
},
/** The pins' positions moved onto refreshed ground heights. */
refresh() {
const items = [
...plan.points,
...[...plan.images.values()].flatMap((entry) => entry.features),
];
for (const item of items)
item.position = val(item.entity.position) ?? item.position;
for (const record of sets) {
record.count = record.write();
record.set.setCount(shown ? record.count : 0);
record.set.commit();
}
engine.render.request('geo-import');
},
destroy() {
release();
engine.render.request('geo-import');
},
};
}
/**
* The natural sizes of an imported file's pin images (the file's own, as
* data: or blob: URLs), so each pin draws at the size Cesium gives it.
* Remote URLs are never loaded. Resolves with a Map of URL → { width, height }.
*/
export async function measureImportImages(
dataSource,
{ timeoutMs = 2000, createImage = () => new Image() } = {},
) {
const urls = new Set();
for (const entity of dataSource.entities.values) {
const image = val(entity.billboard?.image);
const url = typeof image === 'string' ? image : image?.url;
if (typeof url === 'string' && /^(data|blob):/.test(url)) urls.add(url);
}
const sizes = new Map();
await Promise.all(
[...urls].map(async (url) => {
const image = createImage();
image.src = url;
let timer;
try {
await Promise.race([
image.decode(),
new Promise((_, reject) => {
timer = setTimeout(reject, timeoutMs);
}),
]);
if (image.naturalWidth > 0)
sizes.set(url, {
width: image.naturalWidth,
height: image.naturalHeight,
});
} catch {
/* drawn at the default size */
} finally {
clearTimeout(timer);
}
}),
);
return sizes;
}
@@ -0,0 +1,192 @@
import '../../../testSupport/registerDom.mjs';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import { createFakeEngine } from '../../../globe/testing/fakeEngine.js';
import {
drawImportedSource,
measureImportImages,
planImportedSource,
} from './navaraStory.js';
// happy-dom has no 2D canvas; the circle atlas only needs to draw.
globalThis.HTMLCanvasElement.prototype.getContext = () => ({
beginPath() {},
arc() {},
fill() {},
stroke() {},
});
const degrees = (...pairs) => Cesium.Cartesian3.fromDegreesArray(pairs.flat());
/** A file's data source as Cesium's loaders build one. */
function importedSource() {
const source = new Cesium.CustomDataSource('file');
const amber = Cesium.Color.fromCssColorString('#ffb547');
source.entities.add({
polygon: {
hierarchy: new Cesium.PolygonHierarchy(
degrees([0, 0], [1, 0], [1, 1], [0, 1]),
[
new Cesium.PolygonHierarchy(
degrees([0.2, 0.2], [0.4, 0.2], [0.4, 0.4]),
),
],
),
material: amber.withAlpha(0.25),
},
});
source.entities.add({
polyline: { positions: degrees([0, 0], [2, 2]), width: 3, material: amber },
});
source.entities.add({
polyline: { positions: degrees([1, 0], [2, 1]), width: 3, material: amber },
});
source.entities.add({
position: Cesium.Cartesian3.fromDegrees(0.5, 0.5),
point: { pixelSize: 10, color: Cesium.Color.RED },
});
// Two pins sharing one image, one with a label Navara cannot draw.
for (const lon of [3, 4])
source.entities.add({
position: Cesium.Cartesian3.fromDegrees(lon, 0),
billboard: { image: 'data:image/png;base64,AAAA', scale: 0.5 },
...(lon === 3 ? { label: { text: 'Pin' } } : {}),
});
source.entities.add({
position: Cesium.Cartesian3.fromDegrees(5, 0),
model: { uri: 'model.glb' },
});
source.entities.add({
show: false,
position: Cesium.Cartesian3.fromDegrees(6, 0),
point: { pixelSize: 4 },
});
return source;
}
function fakeView() {
const sources = [];
const layers = [];
return {
sources,
layers,
view: {
addSource(config) {
const source = {
config,
deleted: false,
delete: () => (source.deleted = true),
};
sources.push(source);
return source;
},
addLayer(config) {
const layer = {
config,
updates: [],
deleted: false,
update: (next) => layer.updates.push(next),
delete: () => (layer.deleted = true),
};
layers.push(layer);
return layer;
},
},
};
}
test('an imported file plans fills, lines, points and pins, and counts what Navara leaves out', () => {
const plan = planImportedSource(importedSource());
assert.equal(plan.fills.size, 1);
const [fill] = plan.fills.values();
assert.equal(fill.color, '#ffb547');
assert.ok(Math.abs(fill.opacity - 0.25) < 1e-6);
const [polygon] = fill.features;
assert.equal(polygon.geometry.coordinates.length, 2, 'outer ring and hole');
assert.deepEqual(
polygon.geometry.coordinates[0][0].map((v) => Math.round(v)),
polygon.geometry.coordinates[0].at(-1).map((v) => Math.round(v)),
'rings close',
);
assert.equal(plan.lines.size, 1, 'lines of one paint share a group');
assert.equal([...plan.lines.values()][0].features.length, 2);
assert.equal(plan.points.length, 1);
assert.equal(plan.images.size, 1, 'pins sharing an image share a set');
assert.equal([...plan.images.values()][0].features.length, 2);
assert.deepEqual(plan.undrawn, { label: 1, model: 1 });
assert.equal(plan.drawn, 6, 'the hidden point is not drawn');
});
test('the Navara drawing builds vector layers and sprite sets, follows the row and lets go', () => {
const engine = createFakeEngine({ kind: 'navara' });
const { view, sources, layers } = fakeView();
const drawing = drawImportedSource(engine, { view }, importedSource());
assert.deepEqual(drawing.report, {
drawn: 6,
undrawn: 2,
kinds: { label: 1, model: 1 },
});
assert.equal(sources.length, 2);
assert.deepEqual(
layers.map((layer) =>
Object.keys(layer.config).filter((k) => k !== 'type' && k !== 'source'),
),
[['polygon'], ['polyline']],
);
assert.equal(layers[0].config.polygon.clampToGround, true);
assert.equal(layers[0].config.polygon.color, 0xffb547);
assert.equal(layers[1].config.polyline.width, 3);
const sets = engine.movers.list();
assert.deepEqual(
sets.map(({ count }) => count).sort(),
[1, 2],
'one point (no outline) and two pins',
);
drawing.setShow(false);
assert.equal(layers[0].updates.at(-1).polygon.show, false);
assert.ok(engine.movers.list().every(({ count }) => count === 0));
drawing.setShow(true);
assert.deepEqual(
engine.movers
.list()
.map(({ count }) => count)
.sort(),
[1, 2],
);
drawing.refresh();
drawing.destroy();
assert.ok(sources.every((s) => s.deleted) && layers.every((l) => l.deleted));
assert.equal(engine.movers.list().length, 0);
});
test('pin images are measured from the file itself, never fetched from elsewhere', async () => {
const source = new Cesium.CustomDataSource('file');
for (const image of [
'data:image/png;base64,AAAA',
'blob:http://127.0.0.1/abc',
'https://example.invalid/icon.png',
])
source.entities.add({
position: Cesium.Cartesian3.fromDegrees(0, 0),
billboard: { image },
});
const loaded = [];
const sizes = await measureImportImages(source, {
createImage: () => {
const image = {
naturalWidth: 12,
naturalHeight: 20,
decode: async () => loaded.push(image.src),
};
return image;
},
});
assert.deepEqual(loaded.sort(), [
'blob:http://127.0.0.1/abc',
'data:image/png;base64,AAAA',
]);
const plan = planImportedSource(source, { sizes });
assert.equal(plan.images.get('data:image/png;base64,AAAA').sizePx, 20);
assert.equal(plan.images.get('https://example.invalid/icon.png').sizePx, 32);
});