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

52 lines
1.6 KiB
JavaScript

// Shared by browser viewport queries and the provider's cache-key validation.
export const WILDLIFE_MAX_VIEW_DEGREES = 40;
const cleanDegree = (value) => Math.round(value * 1e8) / 1e8;
const spanOf = (box) => Math.max(box.north - box.south, box.east - box.west);
/** Snap non-crossing boxes outward to one canonical grid. Validate the final
* span too: a legal raw box may expand past the supported query size. */
export function snapWildlifeBox(box) {
if (
!box ||
!['south', 'west', 'north', 'east'].every((key) =>
Number.isFinite(box[key]),
) ||
box.south < -90 ||
box.north > 90 ||
box.west < -180 ||
box.east > 180 ||
box.north <= box.south ||
box.east <= box.west ||
spanOf(box) > WILDLIFE_MAX_VIEW_DEGREES + 1e-8
)
return null;
let grid = spanOf(box) > 5 ? 1 : 0.1;
const snap = () => ({
south: Math.max(
-90,
cleanDegree(Math.floor((box.south + 1e-10) / grid) * grid),
),
west: Math.max(
-180,
cleanDegree(Math.floor((box.west + 1e-10) / grid) * grid),
),
north: Math.min(
90,
cleanDegree(Math.ceil((box.north - 1e-10) / grid) * grid),
),
east: Math.min(
180,
cleanDegree(Math.ceil((box.east - 1e-10) / grid) * grid),
),
});
let snapped = snap();
// Crossing the five-degree boundary while snapping must pick the same grid
// on the browser and proxy, and on every subsequent snap of this box.
if (grid === 0.1 && spanOf(snapped) > 5 + 1e-8) {
grid = 1;
snapped = snap();
}
return spanOf(snapped) <= WILDLIFE_MAX_VIEW_DEGREES ? snapped : null;
}