Files
gods-eye-view/src/layers/wildlifeSightings/renderPerformanceFixtures.mjs
T
Yehe LiuandClaude Opus 5.5 14f88b3e08 feat: Wildlife Sightings layer from iNaturalist and Acartia
Recent community wildlife observations, as a new Wildlife group in the
layer panel.

- Server: keyless iNaturalist proxy (v2 observations with only the
  projected fields; undocumented v1 UTFGrid density tiles, decoded on the
  server) sharing one paced queue (burst 4, ~1/s, 60/min, 8,000/day budget,
  up to four in flight), in-memory caches and upstream-only cooldowns;
  Acartia Salish Sea marine-mammal feed rounded to 0.01 degrees with
  comments dropped.
- Layer: grid counts drawn as a soft view-ranked density glow; real sampled
  sightings as taxon badges at their true positions with ambient photo
  thumbnails and labels; obscured records stay privacy cells; a clicked
  sighting stays pinned through camera moves and refreshes.
- Anchored photo card per selection with the single iNaturalist link;
  attribution in the layer row and the Data attribution popover.
- Filters (CONFIRMED, taxa presets) round-trip in share links with token 9.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 16:17:12 -05:00

186 lines
5.5 KiB
JavaScript

import * as Cesium from 'cesium';
import { createRendering } from './rendering.js';
import { createSelection } from './selection.js';
export const DENSE_CONTINENT_CELLS = 6 * 32 * 32;
export const DENSE_CONTINENT_ROWS = 500;
const NOW = Date.parse('2026-11-04T18:00:00Z');
/** The real render + selection pipeline with Cesium coordinates/billboards.
* Only the WebGL scene and external overlay host are replaced. Six fully
* occupied logical tiles deliberately include off-window cells and collisions. */
export function denseContinentalRendering({ onOperations } = {}) {
const cells = [];
const z = 3;
for (let x = 1; x < 3; x++)
for (let y = 1; y < 4; y++)
for (let row = 1; row < 64; row += 2)
for (let column = 1; column < 64; column += 2)
cells.push({
id: `grid:${z}/${x}/${y}:${row}:${column}`,
lat:
(Math.atan(
Math.sinh(Math.PI * (1 - (2 * (y + row / 64)) / 2 ** z)),
) *
180) /
Math.PI,
lon: ((x + column / 64) / 2 ** z) * 360 - 180,
cellCount: row * 64 + column,
});
const rows = cells
.slice(1200, 1200 + DENSE_CONTINENT_ROWS)
.map((cell, i) => ({
stableId: `synthetic:continent:${i}`,
origin: 'inaturalist',
lat: cell.lat,
lon: cell.lon,
iconic: 'Aves',
commonName: i % 2 ? `Synthetic bird ${i}` : null,
observedAt: NOW - i * 1000,
quality: 'research',
obscured: false,
cell: null,
}));
const primitives = {
add: (value) => value,
remove(value) {
value.destroy();
return true;
},
};
const scene = {
canvas: { clientWidth: 1440, clientHeight: 900 },
primitives,
};
const state = {
enabled: true,
viewer: {
scene,
camera: { positionWC: {}, positionCartographic: { height: 2_000_000 } },
},
rows: new Map(rows.map((row) => [row.stableId, row])),
gridCells: cells,
gridAvailable: true,
renderedRows: [],
rowById: new Map(),
pointById: new Map(),
positions: new Map(),
cells: [],
staging: null,
taxa: 'all',
selectedId: null,
query: { visibleBoxes: [{ south: 5, west: -130, north: 75, east: -50 }] },
band: 'regional',
ambientRows: [],
ambientIds: [],
labelRows: [],
carriers: new Map(),
contextIds: new Set(),
};
const centre = Cesium.Cartesian3.fromDegrees(-90, 42);
const frame = Cesium.Transforms.eastNorthUpToFixedFrame(centre);
const east = Cesium.Matrix4.getColumn(frame, 0, new Cesium.Cartesian4());
const north = Cesium.Matrix4.getColumn(frame, 1, new Cesium.Cartesian4());
const metrics = { projections: 0, publications: 0, cartesianBuilds: 0 };
let pan = 0;
let nextIdle = 0;
const idle = new Map();
const slices = [];
const timers = new Map();
let nextTimer = 0;
const parts = {};
const services = {
now: () => NOW,
setTimer(fn, delay) {
timers.set(++nextTimer, { fn, delay });
return nextTimer;
},
clearTimer: (id) => timers.delete(id),
requestIdleCallback(fn) {
idle.set(++nextIdle, fn);
return nextIdle;
},
cancelIdleCallback(id) {
idle.delete(id);
},
performanceSlice(slice) {
slices.push(slice);
},
cesium: {
...Cesium,
Cartesian3: class extends Cesium.Cartesian3 {
static fromDegrees(...args) {
metrics.cartesianBuilds++;
return Cesium.Cartesian3.fromDegrees(...args);
}
},
},
project(_scene, anchor) {
metrics.projections++;
return {
x:
720 +
(anchor.x * east.x + anchor.y * east.y + anchor.z * east.z) / 8000 +
pan,
y:
450 -
(anchor.x * north.x + anchor.y * north.y + anchor.z * north.z) / 8000,
};
},
occluderFactory: () => ({ isPointVisible: () => true }),
badgeSprites: {
get: (group) => `data:image/svg+xml,${group}`,
subscribe: () => () => {},
},
render: { governorRequestRender() {} },
overlayHost: {
setEntries() {
metrics.publications++;
},
},
performanceOperations: onOperations,
};
parts.rendering = createRendering({ state, services, parts });
parts.photos = { get: () => null, setAmbient() {} };
parts.selection = createSelection({ state, services, parts });
parts.rendering.init();
return {
state,
metrics,
moveEnd(offset = 0) {
pan = offset;
metrics.projections = metrics.publications = metrics.cartesianBuilds = 0;
slices.length = 0;
const result = parts.rendering.rebuild();
if (result !== 'pending') {
parts.selection.refreshAmbient();
parts.selection.publishSelected();
parts.selection.refreshContexts();
}
return result;
},
flush(maxSlices = Infinity) {
let count = 0;
while (idle.size && count++ < maxSlices) {
const [id, fn] = idle.entries().next().value;
idle.delete(id);
const threadStart = process.threadCpuUsage();
fn({ timeRemaining: () => 8, didTimeout: false });
const threadUsed = process.threadCpuUsage(threadStart);
if (slices.length)
slices.at(-1).threadCpuMs =
(threadUsed.user + threadUsed.system) / 1000;
}
return {
...metrics,
billboards: state.renderedRows.length,
glowPrimitives: parts.rendering.density?.size ?? 0,
slices: [...slices],
};
},
destroy() {
parts.rendering.destroy();
},
};
}