-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
477 lines (433 loc) · 14.7 KB
/
index.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<html>
<head>
<title>🚒 Seattle Fire Active Incidents Map</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.2/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.2/dist/leaflet.js"></script>
<script src="https://unpkg.com/datalib@1.8.0/datalib.min.js"></script>
<style type="text/css">
body {
font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
}
h1, #whatisthis {
position: absolute;
z-index: 1000;
opacity: 0.6;
top: 0;
left: 50px;
color: black;
text-shadow: white 0 0 3px;
margin: 0;
padding: 0;
}
.overlay:hover h1, .overlay:hover #whatisthis {
opacity: 1;
}
h1 {
font-weight: normal;
font-size: 40px;
line-height: 40px;
color: #ce2029;
padding: 0;
margin: 0;
white-space: nowrap;
}
h1 a {
color: #ce2029;
text-decoration: none;
cursor: pointer;
}
h1 small {
font-size: 20px;
line-height: 40px;
color: black;
vertical-align: top;
}
#whatisthis {
top: 40px;
max-width: 24em;
}
html, body, #map {
height: 100%;
}
.station-icon span {
font-size: 12px;
min-width: 12px;
text-align: center;
color: white;
background-color: #aa3311;
border-radius: 2px;
padding: 0 3px;
display: block;
position: absolute;
transform: translate(-50%, 0%);
}
.incident-icon {
position: absolute;
}
.incident-icon.siren {
color: #ce2029;
font-size: 18px;
top: -9px;
left: -4px;
}
.incident-icon.medic {
color: blue;
font-size: 26px;
top: -12px;
left: 0;
}
.incident-icon.fire {
color: #f30;
font-size: 18px;
top: -12px;
left: 0;
}
.leaflet-popup-content h4 {
font-size: 1.1em;
margin-bottom: 1em;
}
.leaflet-popup-content ul {
margin-top: 0;
margin-bottom: 0;
padding-left: 2em;
}
.whatisthis-toggle-default h1 small, .whatisthis-toggle-default #whatisthis {
visibility: hidden;
}
@media only screen and (max-width: 800px) {
h1 small, #whatisthis {
visibility: hidden;
}
.whatisthis-toggle-default h1 small, .whatisthis-toggle-default #whatisthis {
visibility: visible;
}
}
a.with-icon {
text-decoration: none;
}
a.with-icon > span {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="overlay">
<h1>
<a onclick="document.body.classList.toggle('whatisthis-toggle-default')">🚒 </a>
<small>Where are those sirens headed?</small>
</h1>
<div id="whatisthis">
Displaying the home stations and
<a href="https://web.seattle.gov/sfd/realtime911/getRecsForDatePub.asp?action=Today&incDate=&rad1=des">active response locations</a>
of
<a href="http://seattle.gov/fire/">Seattle Fire Department’s</a>
apparatus.
A mini-project by <a href="https://tsibley.net">Thomas Sibley</a>.
<br>
<small>
Data last updated <span id="age">an unknown time</span> ago //
Next refresh in <span id="countdown">60s</span>.
</small>
<br>
<small>
<a href="https://github.com/tsibley/seattle-fire">Source code</a>
</small>
<br><br>
<a href="http://komonews.com/news/local/live-police--fire-scanner-11-20-2015"
onclick="tuneIn(this, event)"
target="_new"
class="with-icon">
🔊 <span>Listen live</span>
</a>
<script>
function tuneIn(link, event) {
if (Hls.isSupported()) {
event.preventDefault();
var audio = document.createElement("audio");
audio.setAttribute("controls", "controls");
link.parentNode.replaceChild(audio, link);
var hls = new Hls();
hls.loadSource('https://edgecastcdn.net/hls-live/201527F/KOMO/scanner/stream128k.m3u8');
hls.attachMedia(audio);
hls.on(Hls.Events.MANIFEST_PARSED, function() {
audio.play();
});
}
}
</script>
</div>
</div>
<div id="map"></div>
<script>
'use strict';
function esc(text) {
var div = document.createElement("div");
div.textContent = text;
return div.innerHTML;
}
var Seattle = [47.63323873424374, -122.32006072998048];
var Map = L.map('map').setView(Seattle, 12);
var streets = L.tileLayer('https://tiles.stadiamaps.com/tiles/stamen_toner_lite/{z}/{x}/{y}{r}.png', {
attribution: `
© <a href="https://stadiamaps.com/" target="_blank">Stadia Maps</a>
© <a href="https://www.stamen.com/" target="_blank">Stamen Design</a>
© <a href="https://openmaptiles.org/" target="_blank">OpenMapTiles</a>
© <a href="https://www.openstreetmap.org/about/" target="_blank">OpenStreetMap contributors</a>
`,
subdomains: 'abcd',
minZoom: 0,
maxZoom: 20
}).addTo(Map);
var stationLayer = L.geoJson(null, {
pointToLayer: function(station, ll) {
return L.marker(ll, {
title: "Station #" + station.properties.name,
icon: L.divIcon({
html: "<span>" + esc(station.properties.name) + "</span>",
className: 'station-icon'
})
});
},
onEachFeature: function(station, layer) {
layer.bindPopup(
"<h4>Station #"
+ esc(station.properties.name)
+ "</h4>"
+ esc(station.properties.address)
);
}
}).addTo(Map);
var formatTime = dl.format.time("%A, %B %-d at %H:%M");
var sirenIcon = L.divIcon({ html: '🚨 ', className: 'incident-icon siren' })
var medicIcon = L.divIcon({ html: '⚕ ', className: 'incident-icon medic' });
var fireIcon = L.divIcon({ html: '🔥 ', className: 'incident-icon fire' });
function incidentIcon(incident) {
var r = incident.properties.reason;
return r.match(/(Medic|Aid) Response/) ? medicIcon :
r.match(/fire/i) && !r.match(/alarm/i) ? fireIcon :
sirenIcon ;
}
var incidentLayer = L.geoJson(null, {
pointToLayer: function(incident, ll) {
return L.marker(ll, {
title: incident.properties.address,
icon: incidentIcon(incident)
});
},
style: function(path) {
return {
color: '#ce2029',
opacity: 0.8
};
},
onEachFeature: function(feature, layer) {
var html,
geometry = feature.geometry.type;
if (geometry === "Point") {
html = "<h4>" + esc(feature.properties.address) + "</h4>"
+ "<p class='timestamp'>First reported " + esc(formatTime(feature.properties.timestamp)) + "</p>"
+ "<p>Incident "
+ feature.properties.incident_numbers.map(function(_){
return "<a target='_new' href='https://web.seattle.gov/sfd/IncidentSearch/incidentDetail.asp?ID="
+ esc(encodeURIComponent(_))
+ "'>" + esc(_) + "</a>"
}).join("\n")
+ "</p>"
+ "<div class='units'>"
+ feature.properties.units.length
+ " unit"
+ (feature.properties.units.length === 1 ? "" : "s")
+ ": "
+ "<ul>"
+ feature.properties.units.map(function(_){ return "<li>" + esc(_) + "</li>" }).join("\n")
+ "</ul>"
+ "</div>"
+ "<p class='reason'>" + esc(feature.properties.reason) + "</p>";
}
else if (geometry === "LineString") {
html = 'Unit ' + feature.properties.name;
}
layer.bindPopup(html);
}
}).addTo(Map);
// Chain load the data
var stations = {};
var apparatusStations = {};
dl.csv('data/stations-geocoded.csv', processStations);
function processStations(err, data) {
if (err) {
console.error("Error fetching stations: ", err);
return;
}
data.forEach(function(d){
stationLayer.addData({
type: "Feature",
properties: {
name: d.station,
address: d.address
},
geometry: {
type: "Point",
coordinates: [d.lon, d.lat]
}
});
stations[d.station] = d;
});
dl.csv('data/apparatus-locations.csv', processApparatus);
}
function processApparatus(err, data) {
if (err) {
console.error("Error fetching apparatus stations: ", err);
return;
}
data.forEach(function(d){
var station = stations[d.station];
if (station) {
apparatusStations[d.apparatus] = station;
} else {
console.log("Hmm, no station for apparatus: ", apparatus);
console.log("I know these stations: ", stations);
}
});
loadIncidents()
.then(updated => setInterval(checkForUpdate, 1000, { loaded: new Date(), updated }));
}
function checkForUpdate(state) {
var refresh = 60;
var elapsed = Math.floor((new Date() - state.loaded) / 1000);
if (elapsed >= refresh) {
loadIncidents()
.then(updated => state.updated = updated);
state.loaded = new Date();
elapsed = refresh;
}
document.getElementById("countdown")
.textContent = (refresh - elapsed) + "s";
document.getElementById("age").textContent = state.updated
? Math.floor((new Date() - new Date(state.updated)) / 1000) + "s"
: "an unknown time";
}
function loadIncidents() {
return fetch('data/active-incidents.tsv', {cache: 'no-cache'})
.then(res => res.text().then(text => ({
text,
updated: res.headers.get("Last-Modified"),
})))
.then(({text, updated}) => ({
data: dl.read(text, {
type: 'tsv',
parse: {
timestamp: 'date'
}
}),
updated,
}))
.then(({data, updated}) => {
processIncidents(null, data);
return updated;
})
.catch(err => processIncidents(err));
}
function processIncidents(err, data) {
if (err) {
console.error("Error fetching active incidents: ", err);
return;
}
// Clear out existing data
incidentLayer.clearLayers();
dl.groupby('location').execute(data).forEach(function(d){
geocode(d.location, function(incidentCoord) {
var units = dl.unique(
d.values
.map(function(incident) { return incident.units.split(/\s+/) })
.reduce(function(a,b) { return a.concat(b) })
.sort()
);
var reason = dl.unique(d.values, 'incident_type').join(" / ");
var timestamp = dl.min(d.values, 'timestamp');
var ids = dl.unique(d.values, 'incident_number');
incidentLayer.addData({
type: "Feature",
properties: {
address: d.location,
units: units,
reason: reason,
timestamp: timestamp,
incident_numbers: ids
},
geometry: {
type: "Point",
coordinates: incidentCoord
}
});
units.map(function(unit) {
var station = apparatusStations[unit];
if (!station)
console.error("Active incident reported unit for which we have no station: ", unit);
return { unit: unit, station: station };
})
.filter(function (d){ return d.station })
.forEach(function (d) {
incidentLayer.addData({
type: "Feature",
properties: {
name: d.unit
},
geometry: {
type: "LineString",
coordinates: [
[d.station.lon, d.station.lat],
incidentCoord
]
}
});
});
});
});
}
// Geocoding
var geocodeCache = {};
function geocode(address, callback) {
address += " Seattle WA";
address = address.replace(/\s+/, ' ', 'g');
address = address.replace(/ \/ /, ' & ');
if (geocodeCache[address] === undefined) {
console.debug("Geocoding: ", address);
var url = 'https://maps.googleapis.com/maps/api/geocode/json'
+ '?key=AIzaSyBtQUh1mtYqSo8FzPxZ3iA25o4rN-pV1Fc'
+ '®ion=us'
+ '&language=en'
+ '&address=' + encodeURIComponent(address);
dl.load({ url: url }, function(err, data){
data = JSON.parse(data || "{}").results;
if (err || !data || !data.length) {
console.error("Error geocoding: ", address);
console.error("Error was: ", err || "No data returned");
geocodeCache[address] = null;
return;
}
geocodeCache[address] = [ data[0].geometry.location.lng, data[0].geometry.location.lat ];
callback( geocodeCache[address] );
});
}
else if (geocodeCache[address]) {
console.debug("Using cached location for address: ", address);
callback( geocodeCache[address] );
}
else {
console.debug("Cache says no good for address: ", address);
}
}
// Close popup on Escape
document.addEventListener("keyup", function(ev){
if (ev.keyCode == 27) // Esc
Map.closePopup();
}, false);
</script>
<script src="https://unpkg.com/hls.js@0.8.3/dist/hls.light.min.js"></script>
</body>
</html>