refactor(earthquakes): extract native renderer with identical ground-disc output

Keep refresh, cancellation and label policy engine-neutral; inject the original native entities at composition. Prove static geometry, analyst records, owner disposal and last-good refresh behavior. Native dev and QA-preview pass 18 checks each; all eight baseline PNGs remain byte-identical.
This commit is contained in:
German Khokhlov committed 2026-10-27 10:56:41 +04:00
1 parent e8870a4766
commit c89185f352
13 files changed
+343 -92

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
// Native earthquake entities, moved from the layer without changing its
// static CLAMP_TO_GROUND ellipses, properties or analyst snapshot semantics.
import * as Cesium from 'cesium';
export function createEarthquakesCesiumRenderer(_engine, { viewer }) {
if (!viewer?.dataSources)
throw new TypeError('Earthquakes require a native viewer');
const dataSource = new Cesium.CustomDataSource('earthquakes');
dataSource.show = false;
viewer.dataSources.add(dataSource);
let destroyed = false;
return {
kind: 'cesium',
setVisible(show) {
if (!destroyed) dataSource.show = show !== false;
},
replace(discs) {
if (destroyed) return;
const entities = discs.map(
({
stableId,
usgsId,
position,
baseRadius,
color,
isSignificant,
fillAlpha,
outlineAlpha,
mag,
place,
time,
depthKm,
}) =>
new Cesium.Entity({
id: `earthquake:${stableId}`,
position,
ellipse: {
semiMajorAxis: baseRadius,
semiMinorAxis: baseRadius,
material: new Cesium.ColorMaterialProperty(
color.withAlpha(fillAlpha),
),
outline: true,
outlineColor: color.withAlpha(outlineAlpha),
outlineWidth: isSignificant ? 3 : 2,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
},
properties: { usgsId, mag, place, time, depth: depthKm },
}),
);
dataSource.entities.removeAll();
for (const entity of entities) dataSource.entities.add(entity);
},
records(limit) {
if (destroyed || !dataSource.show) return [];
const now = Cesium.JulianDate.now();
const rows = [];
for (const entity of dataSource.entities.values) {
if (rows.length >= limit) break;
const position = entity.position?.getValue(now);
const carto = position
? Cesium.Cartographic.fromCartesian(position)
: null;
const p = entity.properties;
rows.push({
id: p?.usgsId?.getValue(now) ?? null,
mag: p?.mag?.getValue(now),
place: p?.place?.getValue(now),
time: p?.time?.getValue(now),
depth: p?.depth?.getValue(now),
lat: carto ? Cesium.Math.toDegrees(carto.latitude) : null,
lon: carto ? Cesium.Math.toDegrees(carto.longitude) : null,
});
}
return rows;
},
destroy() {
if (destroyed) return;
destroyed = true;
viewer.dataSources.remove(dataSource, true);
},
};
}
@@ -0,0 +1,162 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import * as Cesium from 'cesium';
import * as geo from '../../../geo/index.js';
import { createEarthquakesLayer } from '../index.js';
import { createEarthquakesCesiumRenderer } from './cesium.js';
const rows = [
{
stableId: 'a',
usgsId: 'us-a',
lon: -122.42,
lat: 37.77,
depthKm: 8,
mag: 2.5,
place: 'A',
time: 1000,
},
{
stableId: 'b',
usgsId: 'us-b',
lon: 30,
lat: 20,
depthKm: 70,
mag: 5,
place: 'B',
time: 2000,
},
{
stableId: 'c',
usgsId: null,
lon: 150,
lat: -30,
depthKm: 300,
mag: 6,
place: 'C',
time: 3000,
},
];
function harness() {
const sources = new Set();
const viewer = {
dataSources: {
add(source) {
sources.add(source);
},
remove(source, destroy) {
assert.equal(destroy, true);
return sources.delete(source);
},
},
};
let snapshot = rows;
const layer = createEarthquakesLayer({
source: {
async getSnapshot() {
if (snapshot instanceof Error) throw snapshot;
return snapshot;
},
},
overlayHost: { setEntries() {}, setVisible() {}, clearSource() {} },
renderer: {
create: (options) => createEarthquakesCesiumRenderer(null, options),
},
});
layer.init(viewer);
layer.enable();
return {
viewer,
sources,
layer,
setSnapshot(value) {
snapshot = value;
},
};
}
test('extracted native ground discs preserve static axes, depth palette and analyst identity', async () => {
const h = harness();
try {
assert.equal(await h.layer.update(), true);
const dataSource = [...h.sources][0];
const now = Cesium.JulianDate.now();
const colors = [Cesium.Color.RED, Cesium.Color.ORANGE, Cesium.Color.YELLOW];
dataSource.entities.values.forEach((entity, index) => {
const row = rows[index];
const ellipse = entity.ellipse;
assert.equal(entity.id, `earthquake:${row.stableId}`);
assert.equal(ellipse.semiMajorAxis.isConstant, true);
assert.equal(ellipse.semiMinorAxis.getValue(now), 2 ** row.mag * 1000);
assert.equal(ellipse.semiMajorAxis.getValue(now), 2 ** row.mag * 1000);
assert.equal(
ellipse.heightReference.getValue(now),
Cesium.HeightReference.CLAMP_TO_GROUND,
);
assert.equal(ellipse.outlineWidth.getValue(now), row.mag >= 5 ? 3 : 2);
assert.deepEqual(
{ ...ellipse.material.color.getValue(now) },
{ ...colors[index].withAlpha(row.mag >= 5 ? 0.4 : 0.3) },
);
const expected = Cesium.Cartesian3.fromDegrees(row.lon, row.lat);
assert.deepEqual(entity.position.getValue(now), expected);
assert.deepEqual(
{ ...geo.cartesian3.fromDegrees(row.lon, row.lat) },
{
x: expected.x,
y: expected.y,
z: expected.z,
},
);
});
const records = h.layer.getAnalystRecords();
assert.deepEqual(
records.map((r) => r.id),
['us-a', 'us-b', 'QUAKE-0002'],
);
assert.equal(h.layer.getAnalystRecords(1).length, 1);
assert.equal(h.layer.getAnalystRecords(0).length, 1);
assert.equal(h.layer.getAnalystRecords(Infinity).length, 3);
h.layer.disable();
assert.deepEqual(h.layer.getAnalystRecords(), []);
assert.equal(dataSource.show, false);
} finally {
h.layer.destroy();
}
assert.equal(h.sources.size, 0);
});
test('failed native refresh retains the last complete geometry and analyst snapshot', async () => {
const h = harness();
try {
await h.layer.update();
const source = [...h.sources][0];
const before = [...source.entities.values];
const records = h.layer.getAnalystRecords();
h.setSnapshot(new Error('fixture source unavailable'));
assert.equal(await h.layer.update(), false);
assert.deepEqual(source.entities.values, before);
assert.deepEqual(h.layer.getAnalystRecords(), records);
assert.equal(h.layer.getStats().error, 'fixture source unavailable');
h.setSnapshot([]);
assert.equal(await h.layer.update(), true);
assert.equal(source.entities.values.length, 0);
assert.equal(h.layer.getStats().error, null);
} finally {
h.layer.destroy();
}
});
test('renderer destroys its owner even when passed another viewer and repeated destroy is safe', () => {
const h = harness();
const alien = {
dataSources: {
remove() {
assert.fail('must not remove from another viewer');
},
},
};
h.layer.destroy(alien);
h.layer.destroy(alien);
assert.equal(h.sources.size, 0);
});