Repository navigation
Expand file tree
/
Copy patheonet.ts
More file actions
116 lines (97 loc) · 2.92 KB
/
Copy patheonet.ts
File metadata and controls
116 lines (97 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import type { NaturalEvent, NaturalEventCategory } from '@/types';
interface EonetGeometry {
magnitudeValue?: number;
magnitudeUnit?: string;
date: string;
type: string;
coordinates: [number, number];
}
interface EonetSource {
id: string;
url: string;
}
interface EonetCategory {
id: string;
title: string;
}
interface EonetEvent {
id: string;
title: string;
description: string | null;
closed: string | null;
categories: EonetCategory[];
sources: EonetSource[];
geometry: EonetGeometry[];
}
interface EonetResponse {
title: string;
events: EonetEvent[];
}
const EONET_API_URL = 'https://eonet.gsfc.nasa.gov/api/v3/events';
const CATEGORY_ICONS: Record<NaturalEventCategory, string> = {
severeStorms: '🌀',
wildfires: '🔥',
volcanoes: '🌋',
earthquakes: '🔴',
floods: '🌊',
landslides: '⛰️',
drought: '☀️',
dustHaze: '🌫️',
snow: '❄️',
tempExtremes: '🌡️',
seaLakeIce: '🧊',
waterColor: '🦠',
manmade: '⚠️',
};
export function getNaturalEventIcon(category: NaturalEventCategory): string {
return CATEGORY_ICONS[category] || '⚠️';
}
// Wildfires older than 48 hours are filtered out (stale data)
const WILDFIRE_MAX_AGE_MS = 48 * 60 * 60 * 1000;
export async function fetchNaturalEvents(days = 30): Promise<NaturalEvent[]> {
try {
const url = `${EONET_API_URL}?status=open&days=${days}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`EONET API error: ${response.status}`);
}
const data: EonetResponse = await response.json();
const events: NaturalEvent[] = [];
const now = Date.now();
for (const event of data.events) {
const category = event.categories[0];
if (!category) continue;
// Skip earthquakes - USGS provides better data for seismic events
if (category.id === 'earthquakes') continue;
// Get most recent geometry point
const latestGeo = event.geometry[event.geometry.length - 1];
if (!latestGeo || latestGeo.type !== 'Point') continue;
const eventDate = new Date(latestGeo.date);
const [lon, lat] = latestGeo.coordinates;
const source = event.sources[0];
// Filter out wildfires older than 48 hours
if (category.id === 'wildfires' && now - eventDate.getTime() > WILDFIRE_MAX_AGE_MS) {
continue;
}
events.push({
id: event.id,
title: event.title,
description: event.description || undefined,
category: category.id as NaturalEventCategory,
categoryTitle: category.title,
lat,
lon,
date: eventDate,
magnitude: latestGeo.magnitudeValue,
magnitudeUnit: latestGeo.magnitudeUnit,
sourceUrl: source?.url,
sourceName: source?.id,
closed: event.closed !== null,
});
}
return events;
} catch (error) {
console.error('[EONET] Failed to fetch natural events:', error);
return [];
}
}