-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
343 lines (286 loc) · 10.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<base target="_top">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>UFOKN - CONUS Map Demo</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<link rel="stylesheet" href="styles.css"/>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom"></script>
</head>
<body>
<div id='map'></div>
<canvas id="myChart"></canvas>
<script type="text/javascript" src="./us-states.js"></script>
<script type="text/javascript" src="./fips.js"></script>
<script type="text/javascript" src="./utils.js"></script>
<script type="text/javascript">
// Register the zoom plugin with Chart.js
Chart.register(ChartZoom);
const urlParams = new URLSearchParams(window.location.search);
let datefile = urlParams.get('date') || undefined;
let FEATURE_TYPE = urlParams.get('feature-type') || undefined;
let DATE_LABEL = formatDateFromFile(datefile)
function getNavigation(datefile, feature_type = undefined) {
let DATEOBJ = getDateObjFromFile(datefile)
let HOUR = getHourFromFile(datefile)
let PREV_DAY = getPreviousDay(DATEOBJ, HOUR)
let PREV_HOUR = getPreviousHour(DATEOBJ, HOUR)
let query_string = '';
if (feature_type != undefined) {
query_string += 'feature-type=' + feature_type;
}
let PREV = getLink('<< day', PREV_DAY, query_string) + " " + getLink('< hour', PREV_HOUR, query_string)
let NEXT_DAY = getNextDay(DATEOBJ, HOUR)
let NEXT_HOUR = getNextHour(DATEOBJ, HOUR)
let NEXT = getLink(' hour >', NEXT_HOUR, query_string) + " " + getLink(' day >>', NEXT_DAY, query_string)
let FEATURES = 'Features: '
let all_link = getLink('all', datefile)
let bldg_link = getLink('buildings', datefile, 'feature-type=building')
let ust_link = getLink('ust', datefile, 'feature-type=ust')
let power_link = getLink('power stations', datefile, 'feature-type=power')
if (feature_type == 'building') {
FEATURES += all_link + ' | <strong>buildings</strong> | ' + ust_link + ' | ' + power_link
} else if (feature_type == 'ust') {
FEATURES += all_link + ' | ' + bldg_link + ' | <strong>ust</strong> | ' + power_link
} else if (feature_type == 'power') {
FEATURES += all_link + ' | ' + bldg_link + ' | ' + ust_link + ' | <strong>power stations</strong>'
} else {
FEATURES += '<strong>all</strong> | ' + bldg_link + ' | ' + ust_link+ ' | ' + power_link
}
return FEATURES + '<br/><br/>' + PREV + " || " + NEXT;
}
let NAVIGATION = ''
if (datefile != undefined) {
NAVIGATION = getNavigation(datefile, FEATURE_TYPE)
}
const map = L.map('map').setView([37.8, -96], 4);
const tiles = L.tileLayer(TILE_LAYER, {
maxZoom: 19,
attribution: MAP_ATTRIBUTION
}).addTo(map);
// control that shows state info on click
const info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (id, props) {
let counties = ''
if (props && props.counties != undefined && props.counties != '') {
counties = props.counties.join('')
}
let query = ''
if (FEATURE_TYPE != undefined) {
query += '&feature-type=' + FEATURE_TYPE
}
const contents = props ? `${NAVIGATION}<br /><br /><b><a href="county.html?date=${datefile}&scope=${id}${query}">${props.name}</a></b><br />${numberWithCommas(props.count)} impacts<br /><br />${counties}` : 'Click a state<br /><br />' + NAVIGATION;
this._div.innerHTML = `<h4>${DATE_LABEL}</h4>${contents}`;
};
info.addTo(map);
// get color depending on population density value
function getScaledColor(d, grades) {
/*
return d >= grades[7] ? '#800026' :
d > grades[6] ? '#BD0026' :
d > grades[5] ? '#E31A1C' :
d > grades[4] ? '#FC4E2A' :
d > grades[3] ? '#FD8D3C' :
d > grades[2] ? '#FEB24C' :
d > grades[1] ? '#FED976' : '#FFEDA0';
*/
return d >= 25000 ? '#800026' :
d > 10000 ? '#BD0026' :
d > 5000 ? '#E31A1C' :
d > 1000 ? '#FC4E2A' :
d > 500 ? '#FD8D3C' :
d > 100 ? '#FEB24C' :
d > 0 ? '#FED976' : '#FFEDA0';
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: getScaledColor(feature.properties.count, grades)
};
}
function highlightFeature(e) {
const layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
layer.bringToFront();
}
let max_impacts = 1000;
let grades = []
map.properties = {
'title': 'CONUS Impacts - ' + DATE_LABEL
}
let states_api_url = API_ENDPOINT+'?fips=state'
let counties_api_url = API_ENDPOINT+'?fips=county'
if (datefile != undefined) {
states_api_url += '&date=' + datefile;
counties_api_url += '&date=' + datefile;
}
if (FEATURE_TYPE != undefined) {
states_api_url += '&feature-type=' + FEATURE_TYPE;
counties_api_url += '&feature-type=' + FEATURE_TYPE;
}
let counties = {}
let myData = {}
let chartData = {}
const statesReq = new Request(states_api_url);
let geojson = fetch(statesReq)
.then((response) => response.json())
.then((data) => {
if (datefile == undefined) {
datefile = getFileFromDateParts(data['request']['date'])
DATE_LABEL = formatDateFromFile(datefile)
NAVIGATION = getNavigation(datefile, FEATURE_TYPE)
info.update();
}
sorted = sortObjectByValues(data['impacts'])
for (const idx in sorted) {
fips = sorted[idx]
myData[fips] = data['impacts'][fips]
var state = fips + "000"
if (FIPS[state] != undefined) {
chartData[FIPS[state]] = data['impacts'][fips]
}
}
//console.log('map: ', myData)
//console.log('chart: ', chartData)
drawChart(chartData, DATE_LABEL)
for (let state of statesData.features) {
if (undefined != myData[state.id]){
state.properties['count'] = myData[state.id];
}
else {
state.properties['count'] = 0;
}
}
const average = array => array.reduce((a, b) => a + b) / array.length;
mean = average(Object.values(myData))
rounded_mean = Math.round(mean/1000)*1000
doubled = rounded_mean * 2
increment = Math.round(doubled / 8)
setLegend(increment)
// Get Counties
const countyReq = new Request(counties_api_url);
fetch(countyReq)
.then((response) => response.json())
.then((data) => {
sorted = sortObjectByValues(data['impacts'])
for (const idx in sorted) {
fips = sorted[idx]
state = JSON.stringify(fips.substring(0, 2))
// initialize the array
if (counties[state] == undefined) {
counties[state] = []
}
counties[state].push({'fips': fips, 'label': FIPS[fips] + ': ' + numberWithCommas(data['impacts'][fips])})
}
for (let state of statesData.features) {
let state_id = JSON.stringify(state.id)
if (undefined != counties[state_id]){
state.properties['counties'] = []
for (let county of counties[state_id]) {
state.properties['counties'].push(county.label + "<br/>")
}
}
else {
state.properties['counties'] = '';
}
}
geojson = L.geoJson(statesData, {
style,
onEachFeature
}).addTo(map);
return geojson;
})
.catch((error) => {
console.log(error)
// still setup the geojson from the states
geojson = L.geoJson(statesData, {
style,
onEachFeature
}).addTo(map);
return geojson;
});
})
function resetHighlight(e) {
geojson.resetStyle(e.target);
}
function setInfo(e) {
const layer = e.target;
info.update(layer.feature.id, layer.feature.properties);
}
function resetInfo(e) {
info.update();
}
function zoomToFeature(e) {
setInfo(e)
map.fitBounds(e.target.getBounds());
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
}
map.attributionControl.addAttribution(MAP_ATTRIBUTION_PROVIDER);
function setLegend(increment) {
const legend = L.control({position: 'bottomleft'});
legend.onAdd = function (map) {
const div = L.DomUtil.create('div', 'info legend');
/*grades = [
0,
increment,
2*increment,
3*increment,
4*increment,
5*increment,
6*increment,
7*increment,
];*/
grades = [
0,
1,
100,
500,
1000,
5000,
10000,
25000,
];
const labels = [];
let from, to;
for (let i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
if (0 == from) {
labels.push(`<i style="background:${getScaledColor(from + 1, grades)}"></i> ${numberWithCommas(from)}`);
}
else {
labels.push(`<i style="background:${getScaledColor(from + 1, grades)}"></i> ${numberWithCommas(from)}${to ? `–${numberWithCommas(to)}` : '+'}`);
}
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
}
</script>
</body>
</html>