// Earthquakes layer (Wave 2) — REAL data from the USGS all_day GeoJSON feed. // Time-anchored like the events layer, but with genuine events: a quake's // availability starts at its own origin time, so scrubbing the slider back // before it happened makes it vanish. Fetched directly (USGS sends ACAO:*), // refreshed every 5 min, deduped by feature id across refreshes. export default function create(ctx) { const { viewer, CONFIG, lib, ui, Cesium, start, stop } = ctx; const Q = CONFIG.quakes; const ds = new Cesium.CustomDataSource('quakes'); viewer.dataSources.add(ds); const amber = lib.cz(CONFIG.colors.quakeMin); const red = lib.cz(CONFIG.colors.quakeMax); const [magLo, magHi] = Q.magRamp; // id → { entity, sig }, so refreshes update the delta instead of duplicating. // The sig lets us detect USGS revisions (magnitude/place/depth/time) and // rebuild that entity, rather than leaving a stale one on screen. const byId = new Map(); const sigOf = (f) => { const p = f.properties || {}; const c = (f.geometry && f.geometry.coordinates) || []; return `${p.mag}|${p.place}|${c[2]}|${p.time}`; }; let enabled = true; let timer = null; let inFlight = false; ui.addLayer('quakes', 'Earthquakes (USGS)', true, (on) => { enabled = on; ds.show = on; }); ui.setStatus('quakes', 'loading USGS feed…', 'warn'); // Magnitude → color (amber→red over magRamp) and base pixel size. function magColor(mag) { const t = Cesium.Math.clamp((mag - magLo) / (magHi - magLo), 0, 1); return Cesium.Color.lerp(amber, red, t, new Cesium.Color()); } const baseSize = (mag) => Math.max(3, 4 + mag * 2.2); // Availability start clamped into the fixed clock window [start, stop]. // Older-than-window quakes are visible the whole time; in-window ones appear // as the slider crosses their origin time; (impossible) future ones clamp out. function availabilityFor(timeMs) { const jd = Cesium.JulianDate.fromDate(new Date(timeMs)); let s = jd; if (Cesium.JulianDate.lessThan(s, start)) s = start.clone(); if (Cesium.JulianDate.greaterThan(s, stop)) s = stop.clone(); return new Cesium.TimeIntervalCollection([ new Cesium.TimeInterval({ start: s, stop }), ]); } function buildEntity(f) { const p = f.properties || {}; const g = f.geometry || {}; const coords = g.coordinates || []; const lon = coords[0]; const lat = coords[1]; const depth = coords[2]; const mag = p.mag; const timeMs = p.time; const place = p.place || 'Unknown location'; const size = baseSize(mag); const color = magColor(mag); const recentAtBuild = typeof timeMs === 'number' && (Date.now() - timeMs) < 3_600_000; // Quakes under an hour old pulse; the callback is age-aware so it settles // on its own once the quake passes the 1 h mark (no rebuild needed). const pixelSize = recentAtBuild ? new Cesium.CallbackProperty(() => { const fresh = typeof timeMs === 'number' && (Date.now() - timeMs) < 3_600_000; return fresh ? size + 3 * Math.sin(Date.now() / 300) : size; }, false) : size; const ent = new Cesium.Entity({ id: `quake:${f.id}`, name: `M${mag.toFixed(1)} — ${place}`, position: Cesium.Cartesian3.fromDegrees(lon, lat), availability: availabilityFor(timeMs), point: { pixelSize, color, outlineColor: Cesium.Color.BLACK, outlineWidth: 1, disableDepthTestDistance: Number.POSITIVE_INFINITY, }, description: `
| Magnitude | M${mag.toFixed(1)} |
|---|---|
| Place | ${place} |
| Depth | ${depth != null ? depth.toFixed(1) + ' km' : '—'} |
| Time (UTC) | ${timeMs != null ? new Date(timeMs).toISOString().replace('T', ' ').replace('.000Z', ' UTC') : '—'} |