Files
gods-eye-view/src/layers/wildlifeSightings/viewport.js
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

84 lines
2.3 KiB
JavaScript

import {
WILDLIFE_MAX_VIEW_DEGREES,
WILDLIFE_RETRY_MIN_MS,
WILDLIFE_RETRY_MAX_MS,
} from './policy.js';
export { snapWildlifeBox } from './bounds.js';
const degrees = (radians) => (radians * 180) / Math.PI;
const cleanDegree = (value) => Math.round(value * 1e8) / 1e8;
function validBox(box) {
return (
!!box &&
['south', 'west', 'north', 'east'].every((key) =>
Number.isFinite(box[key]),
) &&
box.south >= -90 &&
box.north <= 90 &&
box.north >= box.south &&
box.west >= -180 &&
box.west <= 180 &&
box.east >= -180 &&
box.east <= 180
);
}
function longitudeRanges(box) {
return box.west <= box.east
? [[box.west, box.east]]
: [
[box.west, 180],
[-180, box.east],
];
}
/** Test geographic overlap, including boxes that cross the antimeridian. */
export function boxIntersects(a, b) {
if (!validBox(a) || !validBox(b) || a.south > b.north || a.north < b.south)
return false;
return longitudeRanges(a).some(([aw, ae]) =>
longitudeRanges(b).some(([bw, be]) => aw <= be && ae >= bw),
);
}
/** Split a local camera rectangle into noncrossing observation query boxes. */
export function wildlifeViewportBoxes(viewer) {
const camera = viewer?.camera;
if (typeof camera?.computeViewRectangle !== 'function') return null;
const rectangle = camera.computeViewRectangle(
viewer?.scene?.globe?.ellipsoid,
);
if (!rectangle) return null;
const box = Object.fromEntries(
['south', 'west', 'north', 'east'].map((key) => [
key,
cleanDegree(degrees(rectangle[key])),
]),
);
if (!validBox(box) || box.north <= box.south) return null;
const width =
box.east >= box.west ? box.east - box.west : 360 - box.west + box.east;
if (
width <= 0 ||
Math.max(box.north - box.south, width) > WILDLIFE_MAX_VIEW_DEGREES
)
return null;
return longitudeRanges(box)
.filter(([west, east]) => east > west)
.map(([west, east]) => ({
south: box.south,
west,
north: box.north,
east,
}));
}
/** 30 seconds, doubling to a four-minute ceiling, for local retry guidance. */
export function retryDelayMs(previousMs) {
return Number.isFinite(previousMs) && previousMs > 0
? Math.min(WILDLIFE_RETRY_MAX_MS, previousMs * 2)
: WILDLIFE_RETRY_MIN_MS;
}