Draw S2 GhostMaps through the renderer draw port, off Cesium

Each product edition now draws as three renderer sets (pins, lines and
fills) instead of a Cesium data source. Pins keep the depth test off and
the Street Level horizon cull, clicks resolve through the draw hit of the
current edition, and a new edition is drawn before the old one goes. A
viewer with no renderer bound draws nothing and releases the edition's
packed images. The layer leaves the Cesium list; a caller handing it a
viewer of their own loads ./render/cesium/unbound first. The QA script
reads pins from the scene instead of data sources.
This commit is contained in:
Sameh Khamis committed 2026-11-08 11:54:43 -08:00
1 parent 4a9b1389ba
commit dabdab47d6
7 files changed
+317 -159

No files matched your search

+192 -90
View File
@@ -1,4 +1,3 @@
import * as Cesium from 'cesium';
import {
decodeXml,
kmzImageResolver,
@@ -15,9 +14,9 @@ import {
tn,
} from '../../i18n/index.js';
import { createHorizonCull } from '../streetLevel/view.js';
// The horizon cull reads the camera and frame ports: a viewer of a
// caller's own gets them from the unbound factories this loads.
import '../../render/cesium/camera.js';
import { fromDegrees } from '../../geo/ellipsoid.js';
import { hexColor } from '../../render/color.js';
import { drawOf, findFrame } from '../../render/rendererOf.js';
import { readGhostMapsKml } from './kml.js';
import { fieldLabel, parseDescriptionFields, wrapCardText } from './records.js';
import { inputFor as defaultInputFor } from '../../input/dispatcher.js';
@@ -110,10 +109,34 @@ function markerImage(color) {
return url;
}
const cesiumColor = (rgba, fallback, alpha) =>
const drawColor = (rgba, fallback, alpha) =>
rgba
? new Cesium.Color(rgba[0], rgba[1], rgba[2], alpha ?? rgba[3])
: Cesium.Color.fromCssColorString(fallback).withAlpha(alpha ?? 1);
? [rgba[0], rgba[1], rgba[2], alpha ?? rgba[3]]
: hexColor(fallback, alpha ?? 1);
/** The size a marker image draws at, before its scale: the product marker. */
const MARKER_SIZE = [20, 20];
/**
* An image's own size in pixels, as an icon drew it unsized; null when it
* cannot be read here (no DOM, or the image does not decode).
*/
async function imageSizeOf(url) {
if (typeof globalThis.Image !== 'function' || !url) return null;
try {
const image = new globalThis.Image();
image.src = url;
await image.decode();
return image.naturalWidth > 0 && image.naturalHeight > 0
? [image.naturalWidth, image.naturalHeight]
: null;
} catch {
return null;
}
}
/** Each layer's draw sets have ids of their own. */
let instances = 0;
function anchorOf(geometry) {
if (geometry.type === 'point') return geometry.position;
@@ -157,7 +180,7 @@ export function createGhostMapsLayer({
let _selectedLink = null;
let _lifetime = new AbortController();
let _selection = readStoredSelection(storage);
/** productId → {version, dataSource, urls, count, features} */
/** productId → {version, sets, urls, features, pins} */
const _loaded = new Map();
/** productId → in-flight version */
const _loading = new Map();
@@ -165,7 +188,11 @@ export function createGhostMapsLayer({
const _failed = new Map();
const notifyRow = () => _rowListener?.();
const render = () => _viewer?.scene?.requestRender?.();
const render = () =>
_viewer && findFrame(_viewer)?.requestRender?.('ghostmaps');
/** This layer's draw sets' ids start with this; one set per edition. */
const setBase = `${GHOSTMAPS_LAYER_ID}:${++instances}`;
let editions = 0;
// Pins skip the depth test, and with Google 3D tiles the globe is hidden,
// so nothing else stops far-side pins drawing through the planet.
const _horizon = createHorizonCull({
@@ -173,7 +200,16 @@ export function createGhostMapsLayer({
items: function* () {
for (const item of _loaded.values()) yield* item.pins;
},
onChange: render,
// Pins the cull turned are written to their editions' icon sets.
onChange: (changed) => {
const bySet = new Map();
for (const pin of changed) {
if (!bySet.has(pin.set)) bySet.set(pin.set, []);
bySet.get(pin.set).push(pin.item);
}
for (const [set, items] of bySet) set?.upsert(items);
render();
},
});
const canSelect = () => overlayHost && inputFor && picking;
@@ -189,12 +225,61 @@ export function createGhostMapsLayer({
const item = _loaded.get(id);
if (!item) return;
_loaded.delete(id);
_viewer?.dataSources?.remove(item.dataSource, true);
disposeSets(item.sets);
item.urls.forEach((url) => URL.revokeObjectURL(url));
if (_selectedKey?.startsWith(`${id}:`)) clearSelection();
}
/** Build one product's data source off-scene, then swap it in. */
function disposeSets(sets) {
if (!sets) return;
for (const set of Object.values(sets)) set.dispose();
}
/**
* Draw a loaded edition in sets of its own, beside any edition drawing
* now. One that cannot be drawn draws nothing and lets its images go.
*/
function mount(next) {
const base = `${setBase}:${++editions}`;
const sets = {};
try {
// Inside the try: a viewer with no renderer bound draws nothing and
// still lets the edition's images go.
const draw = drawOf(_viewer);
sets.shapes = draw.shapes({ id: `${base}:shapes`, clampTo: 'both' });
sets.lines = draw.lines({ id: `${base}:lines`, clampTo: 'both' });
sets.icons = draw.icons({ id: `${base}:icons`, clampTo: 'both' });
sets.shapes.set(next.items.shapes);
sets.lines.set(next.items.lines);
sets.icons.set(next.items.icons);
} catch (error) {
disposeSets(sets);
next.urls.forEach((url) => URL.revokeObjectURL(url));
throw error;
}
for (const pin of next.pins) pin.set = sets.icons;
next.sets = sets;
}
/** The placemark a hit on one of this layer's items is, or null. */
function hitKey(hit) {
if (
!hit?.set?.startsWith(`${setBase}:`) ||
!hit.id?.startsWith(PICK_PREFIX)
)
return null;
const [productId, index] = hit.id.slice(PICK_PREFIX.length).split(':');
const loaded = _loaded.get(productId);
// Only the drawing edition's sets: a retiring one may still pick.
const current = loaded?.sets
? Object.values(loaded.sets).some((set) => set?.id === hit.set)
: false;
return current && loaded.features[Number(index)]
? `${productId}:${index}`
: null;
}
/** Build one product's draw items off-scene, then swap them in. */
async function load(product, signal) {
const bytes = await source.getFile(product.id, product.version, { signal });
signal.throwIfAborted();
@@ -222,93 +307,113 @@ export function createGhostMapsLayer({
entries.clear();
signal.throwIfAborted();
const color = PRODUCT_COLORS[product.id] || ACCENT;
const dataSource = new Cesium.CustomDataSource(
`${GHOSTMAPS_LAYER_ID}:${product.id}`,
);
const icons = [];
const lines = [];
const shapes = [];
const features = [];
const pins = [];
dataSource.entities.suspendEvents();
// Each distinct icon's own size, read once: icons draw at it, scaled.
const sizes = new Map();
const marker = markerImage(color);
for (const feature of parsed.features)
for (const geometry of feature.geometries)
if (geometry.type === 'point') {
const image = feature.style.icon || marker;
if (image && !sizes.has(image))
sizes.set(
image,
image === marker ? MARKER_SIZE : imageSizeOf(image),
);
}
for (const [image, size] of sizes) sizes.set(image, await size);
signal.throwIfAborted();
parsed.features.forEach((feature, index) => {
const key = `${product.id}:${index}`;
const { style } = feature;
feature.geometries.forEach((geometry, part) => {
const id = `${PICK_PREFIX}${key}:${part}`;
if (geometry.type === 'point') {
const position = Cesium.Cartesian3.fromDegrees(
...geometry.position,
);
const entity = dataSource.entities.add({
const position = fromDegrees(...geometry.position);
const image = style.icon || marker;
// An image that cannot be drawn shows nothing, as before.
if (!image) return;
const item = {
id,
position,
billboard: {
image: style.icon || markerImage(color),
scale: Math.min(
1.5,
Math.max(0.35, (style.iconScale ?? 1) * 0.7),
),
color:
style.icon && style.iconColor
? cesiumColor(style.iconColor)
: undefined,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
verticalOrigin: Cesium.VerticalOrigin.CENTER,
// Ground-clamped icons would sink into terrain and 3D tiles; the
// horizon cull hides the far side instead (see `_horizon`).
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
});
pos: position,
image,
size: sizes.get(image) ?? MARKER_SIZE,
scale: Math.min(
1.5,
Math.max(0.35, (style.iconScale ?? 1) * 0.7),
),
...(style.icon && style.iconColor
? { color: style.iconColor }
: {}),
height: 'clampGround',
anchor: 'center',
// Ground-clamped icons would sink into terrain and 3D tiles; the
// horizon cull hides the far side instead (see `_horizon`).
disableDepthTest: Number.POSITIVE_INFINITY,
};
const at = icons.push(item) - 1;
// What the horizon cull turns: the icon as it draws now, in its
// edition's icon set once mounted.
pins.push({
position,
set: null,
get item() {
return icons[at];
},
get show() {
return entity.show;
return icons[at].show !== false;
},
set show(value) {
entity.show = value;
icons[at] = { ...icons[at], show: value };
},
});
} else if (geometry.type === 'line') {
dataSource.entities.add({
lines.push({
id,
polyline: {
positions: Cesium.Cartesian3.fromDegreesArray(
geometry.positions.flat(),
),
width: Math.min(8, Math.max(1.5, style.lineWidth ?? 2)),
material: cesiumColor(style.lineColor, color),
clampToGround: true,
positions: geometry.positions.map(([lon, lat]) =>
fromDegrees(lon, lat),
),
widthPx: Math.min(8, Math.max(1.5, style.lineWidth ?? 2)),
mode: 'draped',
style: {
kind: 'solid',
color: drawColor(style.lineColor, color),
},
});
} else {
const ring = (points) =>
Cesium.Cartesian3.fromDegreesArray(points.flat());
dataSource.entities.add({
const ring = (points) => points.map(([lon, lat]) => ({ lon, lat }));
shapes.push({
id,
polygon: {
hierarchy: new Cesium.PolygonHierarchy(
ring(geometry.outer),
geometry.holes.map(
(hole) => new Cesium.PolygonHierarchy(ring(hole)),
),
),
material:
style.fill === false
? Cesium.Color.TRANSPARENT
: cesiumColor(
style.polyColor,
color,
style.polyColor ? undefined : 0.25,
),
classificationType: Cesium.ClassificationType.BOTH,
geometry: {
kind: 'polygon',
rings: [ring(geometry.outer), ...geometry.holes.map(ring)],
},
// An unfilled polygon is still there to click, as before.
fill:
style.fill === false
? [0, 0, 0, 0]
: drawColor(
style.polyColor,
color,
style.polyColor ? undefined : 0.25,
),
height: 'clampGround',
});
if (style.outline !== false)
dataSource.entities.add({
lines.push({
id: `${id}:outline`,
polyline: {
positions: ring([...geometry.outer, geometry.outer[0]]),
width: Math.min(6, Math.max(1.5, style.lineWidth ?? 2)),
material: cesiumColor(style.lineColor, color),
clampToGround: true,
positions: [...geometry.outer, geometry.outer[0]].map(
([lon, lat]) => fromDegrees(lon, lat),
),
widthPx: Math.min(6, Math.max(1.5, style.lineWidth ?? 2)),
mode: 'draped',
style: {
kind: 'solid',
color: drawColor(style.lineColor, color),
},
});
}
@@ -320,8 +425,14 @@ export function createGhostMapsLayer({
anchor: anchorOf(feature.geometries[0]),
});
});
dataSource.entities.resumeEvents();
return { version: product.version, dataSource, urls, features, pins };
return {
version: product.version,
items: { icons, lines, shapes },
sets: null,
urls,
features,
pins,
};
} catch (error) {
urls.forEach((url) => URL.revokeObjectURL(url));
throw error;
@@ -351,8 +462,8 @@ export function createGhostMapsLayer({
return;
}
// Cull before the first frame so far-side pins never flash.
_horizon.update(next.pins);
await _viewer.dataSources.add(next.dataSource);
_horizon.update(next.pins, { quiet: true });
mount(next);
// The new edition is on the globe before the old one leaves.
unload(product.id);
_loaded.set(product.id, next);
@@ -459,7 +570,7 @@ export function createGhostMapsLayer({
// when its marker turns behind the planet.
stateless: true,
horizonCull: true,
position: Cesium.Cartesian3.fromDegrees(...feature.anchor),
position: fromDegrees(...feature.anchor),
};
if (card.interactive) {
const link = _selectedLink;
@@ -487,16 +598,6 @@ export function createGhostMapsLayer({
overlayHost?.setEntries(GHOSTMAPS_OVERLAY_SOURCE_ID, [], CARD_HOST_OPTIONS);
}
function pickedKey(picked) {
const pickId = picking.resolvePickId(picked);
if (typeof pickId !== 'string' || !pickId.startsWith(PICK_PREFIX))
return null;
const [productId, index] = pickId.slice(PICK_PREFIX.length).split(':');
return _loaded.get(productId)?.features[Number(index)]
? `${productId}:${index}`
: null;
}
function installClickHandler() {
if (!canSelect() || _clickSubscription || !_viewer) return;
picking.registerPickOwner?.(
@@ -515,7 +616,7 @@ export function createGhostMapsLayer({
return;
}
const picked = event.pick;
const key = picked ? pickedKey(picked) : null;
const key = picked ? hitKey(event.drawHit) : null;
if (key) {
_selectedKey = key;
publishCard();
@@ -563,7 +664,8 @@ export function createGhostMapsLayer({
enable() {
_enabled = true;
_lifetime = new AbortController();
for (const item of _loaded.values()) item.dataSource.show = true;
for (const item of _loaded.values())
for (const set of Object.values(item.sets ?? {})) set.show = true;
installClickHandler();
if (!_unsubscribe && typeof source.subscribe === 'function')
_unsubscribe = source.subscribe(
+38 -17
View File
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import * as Cesium from 'cesium';
import { createGhostMapsLayer } from './index.js';
import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs';
function element(localName, children = [], value = '', attributes = {}) {
const node = {
@@ -104,17 +104,17 @@ function fixture(t, { beforeFileReturns, onCreateUrl } = {}) {
});
t.mock.method(URL, 'revokeObjectURL', (url) => revoked.push(url));
t.mock.method(console, 'warn', () => {});
const mounted = new Set();
const viewer = {
scene: { requestRender() {} },
dataSources: {
async add(dataSource) {
mounted.add(dataSource);
return dataSource;
},
remove(dataSource) {
return mounted.delete(dataSource);
},
const viewer = {};
const { draw, unbind } = bindFakeDraw(viewer);
t.after(unbind);
// The editions drawn: each draws in sets of its own.
const mounted = {
get size() {
return new Set(
[...draw.sets.values()]
.filter((set) => !set.disposed)
.map((set) => set.spec.id.split(':').slice(0, 3).join(':')),
).size;
},
};
const layer = createGhostMapsLayer({
@@ -132,7 +132,7 @@ function fixture(t, { beforeFileReturns, onCreateUrl } = {}) {
layer.init(viewer);
layer.enable();
t.after(() => layer.destroy());
return { layer, viewer, mounted, created, revoked };
return { layer, viewer, draw, unbind, mounted, created, revoked };
}
test('cancelling a file read does not allocate packed-image URLs', async (t) => {
@@ -155,9 +155,9 @@ test('cancellation during parsing releases every packed-image URL', async (t) =>
assert.equal(mounted.size, 0);
});
test('entity construction failure releases parsed image URLs', async (t) => {
const { layer, viewer, mounted, created, revoked } = fixture(t);
t.mock.method(Cesium.EntityCollection.prototype, 'add', () => {
test('a draw failure releases parsed image URLs', async (t) => {
const { layer, viewer, draw, mounted, created, revoked } = fixture(t);
t.mock.method(draw, 'icons', () => {
throw new Error('fixture entity failure');
});
await layer.update(viewer);
@@ -167,13 +167,34 @@ test('entity construction failure releases parsed image URLs', async (t) => {
assert.equal(layer.getStats().error, 'fixture entity failure');
});
test('a viewer with no renderer bound releases parsed image URLs', async (t) => {
const { layer, viewer, unbind, mounted, created, revoked } = fixture(t);
unbind();
await layer.update(viewer);
assert.equal(created.length, 1);
assert.deepEqual(revoked, created);
assert.equal(mounted.size, 0);
assert.ok(layer.getStats().error);
});
test('successful load keeps icons until the product is disabled', async (t) => {
const { layer, viewer, mounted, created, revoked } = fixture(t);
const { layer, viewer, draw, mounted, created, revoked } = fixture(t);
await layer.update(viewer);
assert.equal(created.length, 1);
assert.equal(revoked.length, 0);
assert.equal(mounted.size, 1);
assert.equal(layer.getStats().count, 1);
const pins = [...draw.sets.values()].find(
(set) => !set.disposed && set.kind === 'icons',
);
const [pin] = pins.items.values();
assert.equal(pin.id, 'ghostmaps:cip:0:0');
assert.equal(pin.image, 'blob:local-0');
assert.equal(pin.height, 'clampGround');
assert.equal(pins.spec.clampTo, 'both');
assert.equal(pin.disableDepthTest, Infinity);
assert.equal(pin.scale, 0.7);
assert.deepEqual(pin.size, [20, 20], 'unread in Node: the marker size');
layer.disable();
assert.deepEqual(revoked, created);
assert.equal(mounted.size, 0);
-1
View File
@@ -14,7 +14,6 @@
"src/layers/cctv/projection.js",
"src/layers/cctv/rendering.js",
"src/layers/firms/rendering.js",
"src/layers/ghostMaps/index.js",
"src/layers/installations/rendering.js",
"src/layers/radio/rendering.js",
"src/layers/recentImagery/rendering.js",
+3
View File
@@ -103,6 +103,9 @@ const DRAW_FROM_CALLER = new Set([
// Not wholly Cesium-free (its pointer input still is), but its missions
// draw, pick and fly through the ports alone.
'./layers/launches',
// Its pins, footprints and outlines draw through `drawOf`; its horizon
// cull reads the camera and frame ports.
'./layers/ghostmaps',
]);
/**