-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_features.mjs
More file actions
101 lines (92 loc) · 2.86 KB
/
javascript_features.mjs
File metadata and controls
101 lines (92 loc) · 2.86 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
/**
* Road511 API — JavaScript features example (Node.js 18+)
* Fetch cameras, signs, and weather stations near a location.
*
* Sign up at https://portal.road511.com for a free API key
*
* Usage:
* export ROAD511_API_KEY="sk_live_..."
* node javascript_features.mjs
*/
const BASE_URL = "https://api.road511.com/api/v1";
const API_KEY = process.env.ROAD511_API_KEY;
if (!API_KEY) {
console.error("Set ROAD511_API_KEY environment variable");
process.exit(1);
}
async function fetchFeatures(params) {
const url = new URL(`${BASE_URL}/features`);
for (const [key, val] of Object.entries(params)) {
url.searchParams.set(key, val);
}
const resp = await fetch(url, {
headers: { "X-API-Key": API_KEY },
});
if (!resp.ok) {
const body = await resp.text();
throw new Error(`HTTP ${resp.status}: ${body}`);
}
return resp.json();
}
// --- Example 1: Traffic cameras near Denver ---
console.log("=== Traffic cameras near Denver, CO ===");
const cameras = await fetchFeatures({
type: "cameras",
lat: 39.74,
lng: -104.99,
radius_km: 50,
limit: 5,
});
console.log(`Found ${cameras.total} cameras within 50km`);
for (const cam of cameras.data) {
const img = cam.properties?.image_url || "no image";
console.log(` ${cam.name} — ${img}`);
}
// --- Example 2: DMS signs in New York ---
console.log("\n=== Dynamic message signs in NY ===");
const signs = await fetchFeatures({
type: "signs",
jurisdiction: "NY",
limit: 5,
});
console.log(`Found ${signs.total} signs`);
for (const sign of signs.data) {
const msg = sign.properties?.message || "blank";
console.log(` ${sign.name}: "${msg}"`);
}
// --- Example 3: Weather stations in Montana ---
console.log("\n=== RWIS weather stations in MT ===");
const weather = await fetchFeatures({
type: "weather_stations",
jurisdiction: "MT",
limit: 5,
});
console.log(`Found ${weather.total} weather stations`);
for (const ws of weather.data) {
const temp = ws.properties?.temperature;
const road = ws.properties?.road_surface;
console.log(
` ${ws.name}: ${temp != null ? temp + "°F" : "N/A"}, surface: ${road || "N/A"}`
);
}
// --- Example 4: Feature detail (lazy-loaded) ---
if (cameras.data.length > 0) {
const id = cameras.data[0].id;
console.log(`\n=== Detail for camera ${id} ===`);
const resp = await fetch(`${BASE_URL}/features/${id}/details`, {
headers: { "X-API-Key": API_KEY },
});
if (resp.ok) {
const detail = await resp.json();
console.log(` Name: ${detail.name}`);
console.log(` Properties:`, JSON.stringify(detail.properties, null, 2).slice(0, 300));
}
}
// --- Example 5: All feature types available ---
console.log("\n=== Available feature types ===");
const types = await fetch(`${BASE_URL}/features/types`, {
headers: { "X-API-Key": API_KEY },
}).then((r) => r.json());
for (const t of types) {
console.log(` ${t.type}: ${t.count} features`);
}