| 1 | import { NextResponse } from 'next/server'; |
| 2 | import { stealthFetch } from '@/lib/stealthFetch'; |
| 3 | |
| 4 | /** |
| 5 | * OSIRIS — Severe Weather & Anomalies API |
| 6 | * Fetches active natural events from NASA EONET and NOAA/NWS active alerts. |
| 7 | * Tracks: Severe storms, volcanoes, sea ice, and U.S. active weather alerts. |
| 8 | */ |
| 9 | |
| 10 | type Severity = 'low' | 'medium' | 'high'; |
| 11 | |
| 12 | type WeatherEvent = { |
| 13 | id: string; |
| 14 | title: string; |
| 15 | category: string; |
| 16 | type: string; |
| 17 | icon: string; |
| 18 | severity: Severity; |
| 19 | lat: number; |
| 20 | lng: number; |
| 21 | date?: string; |
| 22 | expires?: string; |
| 23 | area?: string; |
| 24 | source: string; |
| 25 | provider: 'NASA EONET' | 'NOAA/NWS'; |
| 26 | }; |
| 27 | |
| 28 | type EonetEvent = { |
| 29 | id: string; |
| 30 | title: string; |
| 31 | categories?: { |
| 32 | id?: string; |
| 33 | title?: string; |
| 34 | }[]; |
| 35 | geometry?: { |
| 36 | type?: string; |
| 37 | coordinates?: number[]; |
| 38 | date?: string; |
| 39 | }[]; |
| 40 | sources?: { |
| 41 | url?: string; |
| 42 | }[]; |
| 43 | }; |
| 44 | |
| 45 | type EonetResponse = { |
| 46 | events?: EonetEvent[]; |
| 47 | }; |
| 48 | |
| 49 | type NwsGeometry = |
| 50 | | { |
| 51 | type: 'Point'; |
| 52 | coordinates: number[]; |
| 53 | } |
| 54 | | { |
| 55 | type: 'Polygon'; |
| 56 | coordinates?: number[][][]; |
| 57 | } |
| 58 | | { |
| 59 | type: 'MultiPolygon'; |
| 60 | coordinates?: number[][][][]; |
| 61 | }; |
| 62 | |
| 63 | type NwsFeature = { |
| 64 | geometry?: NwsGeometry | null; |
| 65 | properties?: { |
| 66 | '@id'?: string; |
| 67 | id?: string; |
| 68 | headline?: string; |
| 69 | event?: string; |
| 70 | severity?: string; |
| 71 | effective?: string; |
| 72 | sent?: string; |
| 73 | expires?: string; |
| 74 | areaDesc?: string; |
| 75 | }; |
| 76 | }; |
| 77 | |
| 78 | type NwsResponse = { |
| 79 | features?: NwsFeature[]; |
| 80 | }; |
| 81 | |
| 82 | export async function GET() { |
| 83 | try { |
| 84 | const [eonetRes, nwsRes] = await Promise.allSettled([ |
| 85 | stealthFetch('https://eonet.gsfc.nasa.gov/api/v3/events?status=open&limit=100', { |
| 86 | signal: AbortSignal.timeout(10000), |
| 87 | }), |
| 88 | fetch('https://api.weather.gov/alerts/active?status=actual&message_type=alert', { |
| 89 | headers: { |
| 90 | Accept: 'application/geo+json', |
| 91 | 'User-Agent': 'OSIRIS Severe Weather Layer', |
| 92 | }, |
| 93 | signal: AbortSignal.timeout(10000), |
| 94 | }), |
| 95 | ]); |
| 96 | |
| 97 | const events: WeatherEvent[] = []; |
| 98 | let providerSucceeded = false; |
| 99 | |
| 100 | if (eonetRes.status === 'fulfilled' && eonetRes.value.ok) { |
| 101 | try { |
| 102 | const data = (await eonetRes.value.json()) as EonetResponse; |
| 103 | providerSucceeded = true; |
| 104 | |
| 105 | for (const event of data.events || []) { |
| 106 | const geom = event.geometry && event.geometry.length > 0 ? event.geometry[event.geometry.length - 1] : null; |
| 107 | if (!geom || geom.type !== 'Point' || !geom.coordinates) continue; |
| 108 | |
| 109 | const category = event.categories?.[0]?.id || 'unknown'; |
| 110 | |
| 111 | // We already track wildfires via FIRMS, so we skip EONET wildfires |
| 112 | if (category === 'wildfires') continue; |
| 113 | |
| 114 | let typeLabel = 'Event'; |
| 115 | let icon = 'alert'; |
| 116 | let severity: Severity = 'low'; |
| 117 | |
| 118 | if (category === 'severeStorms') { |
| 119 | typeLabel = 'Severe Storm'; |
| 120 | icon = 'cyclone'; |
| 121 | severity = 'high'; |
| 122 | } else if (category === 'volcanoes') { |
| 123 | typeLabel = 'Volcano Eruption'; |
| 124 | icon = 'volcano'; |
| 125 | severity = 'high'; |
| 126 | } else if (category === 'seaIce') { |
| 127 | typeLabel = 'Iceberg / Sea Ice'; |
| 128 | icon = 'ice'; |
| 129 | severity = 'medium'; |
| 130 | } else if (category === 'earthquakes') { |
| 131 | continue; |
| 132 | } else { |
| 133 | typeLabel = event.categories?.[0]?.title || 'Anomaly'; |
| 134 | } |
| 135 | |
| 136 | events.push({ |
| 137 | id: `eonet-${event.id}`, |
| 138 | title: event.title, |
| 139 | category, |
| 140 | type: typeLabel, |
| 141 | icon, |
| 142 | severity, |
| 143 | lat: geom.coordinates[1], |
| 144 | lng: geom.coordinates[0], |
| 145 | date: geom.date, |
| 146 | source: event.sources?.[0]?.url || 'NASA EONET', |
| 147 | provider: 'NASA EONET', |
| 148 | }); |
| 149 | } |
| 150 | } catch (error) { |
| 151 | console.error('NASA EONET normalization error:', error); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (nwsRes.status === 'fulfilled' && nwsRes.value.ok) { |
| 156 | try { |
| 157 | const data = (await nwsRes.value.json()) as NwsResponse; |
| 158 | providerSucceeded = true; |
| 159 | |
| 160 | for (const feature of data.features || []) { |
| 161 | const props = feature.properties || {}; |
| 162 | const coords = getRepresentativePoint(feature.geometry); |
| 163 | if (!coords) continue; |
| 164 | |
| 165 | events.push({ |
| 166 | id: `nws-${props.id || props['@id'] || props.event || coords.lat}`, |
| 167 | title: props.headline || props.event || 'NWS Weather Alert', |
| 168 | category: 'weatherAlerts', |
| 169 | type: props.event || 'Weather Alert', |
| 170 | icon: 'weather', |
| 171 | severity: normalizeNwsSeverity(props.severity), |
| 172 | lat: coords.lat, |
| 173 | lng: coords.lng, |
| 174 | date: props.effective || props.sent, |
| 175 | expires: props.expires, |
| 176 | area: props.areaDesc, |
| 177 | source: props['@id'] || 'https://api.weather.gov/alerts/active', |
| 178 | provider: 'NOAA/NWS', |
| 179 | }); |
| 180 | } |
| 181 | } catch (error) { |
| 182 | console.error('NOAA/NWS normalization error:', error); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (!providerSucceeded) { |
| 187 | return NextResponse.json({ events: [], error: 'Failed to fetch weather data' }, { status: 500 }); |
| 188 | } |
| 189 | |
| 190 | return NextResponse.json({ |
| 191 | events, |
| 192 | total: events.length, |
| 193 | timestamp: new Date().toISOString(), |
| 194 | }); |
| 195 | } catch (error) { |
| 196 | console.error('Weather API error:', error); |
| 197 | return NextResponse.json({ events: [], error: 'Failed to fetch weather data' }, { status: 500 }); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | function normalizeNwsSeverity(severity?: string): Severity { |
| 202 | switch (severity) { |
| 203 | case 'Extreme': |
| 204 | case 'Severe': |
| 205 | return 'high'; |
| 206 | case 'Moderate': |
| 207 | return 'medium'; |
| 208 | default: |
| 209 | return 'low'; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | function getRepresentativePoint(geometry?: NwsGeometry | null) { |
| 214 | if (!geometry) return null; |
| 215 | |
| 216 | if (geometry.type === 'Point') { |
| 217 | const [lng, lat] = geometry.coordinates; |
| 218 | return { lat, lng }; |
| 219 | } |
| 220 | |
| 221 | if (geometry.type === 'Polygon') { |
| 222 | return averageCoordinates(geometry.coordinates?.[0]); |
| 223 | } |
| 224 | |
| 225 | if (geometry.type === 'MultiPolygon') { |
| 226 | return averageCoordinates(geometry.coordinates?.[0]?.[0]); |
| 227 | } |
| 228 | |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | function averageCoordinates(coords?: number[][]) { |
| 233 | if (!coords || coords.length === 0) return null; |
| 234 | |
| 235 | const totals = coords.reduce( |
| 236 | (acc, coord) => { |
| 237 | acc.lng += coord[0]; |
| 238 | acc.lat += coord[1]; |
| 239 | return acc; |
| 240 | }, |
| 241 | { lat: 0, lng: 0 } |
| 242 | ); |
| 243 | |
| 244 | return { |
| 245 | lat: totals.lat / coords.length, |
| 246 | lng: totals.lng / coords.length, |
| 247 | }; |
| 248 | } |