-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
319 lines (307 loc) · 14 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
<!DOCTYPE html>
<html>
<head>
<title>COVID by County</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<script src="./citysdk.js"></script>
<style>
#Container {
position: relative;
}
#Header {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
}
#Header h1 {
margin: 0;
}
#Legend {
line-height: 18px;
color: #555;
position: absolute;
bottom: 40px;
right: 20px;
z-index: 400;
background-color: white;
padding: 5px;
}
#Legend i {
width: 50px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
#Map {
height: 600px;
}
</style>
</head>
<body>
<div id="Header">
<h1>COVID-19 Cases per county per capita</h1>
<div>
<label for="StatePicker">State: </label>
<select id="StatePicker">
<option></option>
<option value="01">Alabama</option>
<option value="02">Alaska</option>
<option value="04">Arizona</option>
<option value="05">Arkansas</option>
<option value="06">California</option>
<option value="08">Colorado</option>
<option value="09">Connecticut</option>
<option value="10">Delaware</option>
<option value="11">District Of Columbia</option>
<option value="12">Florida</option>
<option value="13">Georgia</option>
<option value="15">Hawaii</option>
<option value="16">Idaho</option>
<option value="17">Illinois</option>
<option value="18">Indiana</option>
<option value="19">Iowa</option>
<option value="20">Kansas</option>
<option value="21">Kentucky</option>
<option value="22">Louisiana</option>
<option value="23">Maine</option>
<option value="24">Maryland</option>
<option value="25">Massachusetts</option>
<option value="26">Michigan</option>
<option value="27">Minnesota</option>
<option value="28">Mississippi</option>
<option value="29">Missouri</option>
<option value="30">Montana</option>
<option value="31">Nebraska</option>
<option value="32">Nevada</option>
<option value="33">New Hampshire</option>
<option value="34">New Jersey</option>
<option value="35">New Mexico</option>
<option value="36">New York</option>
<option value="37">North Carolina</option>
<option value="38">North Dakota</option>
<option value="39">Ohio</option>
<option value="40">Oklahoma</option>
<option value="41">Oregon</option>
<option value="42">Pennsylvania</option>
<option value="44">Rhode Island</option>
<option value="45">South Carolina</option>
<option value="46">South Dakota</option>
<option value="47">Tennessee</option>
<option value="48">Texas</option>
<option value="49">Utah</option>
<option value="50">Vermont</option>
<option value="51">Virginia</option>
<option value="53">Washington</option>
<option value="54">West Virginia</option>
<option value="55">Wisconsin</option>
<option value="56">Wyoming</option>
</select>
<p>After picking a state, give it 10 seconds to get data and draw the map. The cases are cumulative. <a href="https://github.com/openbrian/covid-19-map-by-region">source code</a></p>
</div>
</div>
<div id="Container">
<div id="Map"></div>
<div id="Legend"></div>
</div>
<script>
var map = L.map('Map').setView([39.5, -98.3], 5);
// L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
// maxZoom: 18,
// attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
// '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
// 'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
// id: 'mapbox/streets-v11',
// tileSize: 512,
// zoomOffset: -1
// }).addTo(map);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
const nytExceptions = {
"36005": ["New York", "New York City"], // Bronx
"36081": ["New York", "New York City"], // Queens
"36047": ["New York", "New York City"], // Kings
"36061": ["New York", "New York City"], // New York
"36085": ["New York", "New York City"], // Richmond
};
function style_county(all_covid_data, feature) {
const [covid_data, covid_data_no_fips] = all_covid_data;
let fips = feature.properties.STATEFP + feature.properties.COUNTYFP;
// console.log("fips", fips);
let cases = null;
if (fips in covid_data)
cases = covid_data[fips][0];
// console.log(feature);
let pop = feature.properties.B00001_001E;
if (pop === "NAN: null")
pop = null;
let no_data = {
fillColor: "#AAAAAA",
fillOpacity: 0.7,
weight: 0.5,
color: "rgba(255, 255, 255, 0.7)"
};
if (! cases) {
console.log(fips + " no covid data, " + feature.properties.NAME);
// Try the exceptions.
if (fips in nytExceptions) {
console.log(' Found an exception');
const [state, city] = nytExceptions[fips];
if (state in covid_data_no_fips) {
if (city in covid_data_no_fips[state]) {
cases = covid_data_no_fips[state][city][0];
}
}
} else {
console.log(' Did not find an exception');
// If there's no covid_data, assume it's zero cases.
cases = 0;
}
}
if (! pop) {
if (!(fips in covid_data)){
console.log(fips + " no census data, and fips not in covid_data");
return no_data;
}
console.log(fips + " no census data, " + covid_data[fips][2] + " / " + covid_data[fips][3]);
return no_data;
}
const percent = (cases / pop) * 100;
// console.log(`${fips} percent ${cases} / ${pop} is ${percent}`);
return {
fillColor: getColor(percent),
fillOpacity: 0.7,
weight: 0.5,
color: "rgba(255, 255, 255, 0.7)"
};
}
// const grades_red = [
// [0.32, "#c90000"],
// [0.16, "#d12413"],
// [0.08, "#d74323"],
// [0.04, "#df6936"],
// [0.02, "#e48245"],
// [0.01, "#f3c568"],
// [0.005, "#f5d06e"],
// [0.0025, "#f8de75"],
// [0.0000, "#ffff87"]
// ];
// From https://colorbrewer2.org
const grades = [
[16.0, '#08519c'],
[8.0, '#3182bd'],
[4.0, '#6baed6'],
[2.0, '#bdd7e7'],
[1.0, '#eff3ff'],
];
// https://cssgradient.io/
function getColor(percent) {
for (let i = 0; i < grades.length; i++) {
if (percent >= grades[i][0]) {
return grades[i][1];
}
}
}
let getCensusData = (options) => {
return new Promise((resolve, reject) => {
function callback(error, response) {
if (error)
reject(error);
resolve(response);
}
census(options, callback);
});
};
// TODO: Let's try to stream this.
let covid = fetch("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
.then(response => response.text())
.then((input) => {
let lastDate = null;
let data = {};
let dataNoFips = {};
let header = true;
input.split(/\n/).forEach((line) => {
if (header) {
header = false;
return;
}
const [date, county, state, fips, cases, deaths] = line.split(/,/);
// Input is sorted by date. Later dates will overwrite prior dates and that's ok.
// In some cases, we don't know what county the cases are in. They are assigned
// to the Unknown county. But on a later day, they may become assigned, so clear
// out dataNoFips.
if (date !== lastDate) {
dataNoFips = {};
lastDate = date;
}
if (fips)
data[fips] = [cases, deaths, state, county];
else {
// console.log(`No fips for ${county}, ${state}.`)
if (! (state in dataNoFips))
dataNoFips[state] = {};
if (! (county in dataNoFips[state]))
dataNoFips[state][county] = [cases, deaths];
}
});
console.log(dataNoFips);
return [data, dataNoFips];
})
.catch(error => console.error(error));
// curry
let combine = function(covid_data, renderer) {
return function(feature) {
return renderer(covid_data, feature);
}
};
let cd = null;
document.querySelector('#StatePicker').addEventListener('change', event => {
if (! event.target.value)
return;
// Counties
const options = {
vintage: 2017,
geoHierarchy: { state: event.target.value, county: "*"},
geoResolution: "5m",
sourcePath: ["acs", "acs5"],
values: ["B00001_001E"]
};
census_data = getCensusData(options);
Promise.all([covid, census_data]).then(function(values) {
let style = combine(values[0], style_county);
cd = values[0]; // Store for later.
L.geoJson(values[1], {
style: style,
onEachFeature: onEachCounty
}).addTo(map);
});
});
function onEachCounty(feature, layer) {
let covid_data = cd[0];
let fips = feature.properties.STATEFP + feature.properties.COUNTYFP;
let cases = null;
if (fips in covid_data)
cases = covid_data[fips][0];
let pop = feature.properties.B00001_001E;
const percent = (cases / pop) * 100;
layer.bindPopup(
"fips " + fips +
", pop " + feature.properties.B00001_001E +
", cases " + cases +
", percent " + percent);
}
var legendDiv = document.getElementById("Legend");
grades.forEach((grade, i) => {
const rhs = i === 0 ? `+ cases` : ` – ${grades[i - 1][0]}%`;
legendDiv.innerHTML += `<i style="background: ${grade[1]}"></i>${grade[0]}%${rhs}<br/>`;
});
</script>
</body>
</html>