-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirerisk-d3viz.js
481 lines (426 loc) · 14.8 KB
/
firerisk-d3viz.js
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
478
479
480
481
const log = console.log;
log("Document Ready");
function formatFireData(data) {
return {
fire_area_acres: +data.fire_area_acres,
fire_contained_date: data.fire_contained_date,
fire_name: data.fire_name,
fire_start_date: data.fire_start_date,
fire_year: +data.fire_year,
postal_code: data.postal_code,
}
}
function formatLandCoverageData(data) {
return {
year: +data.year,
postal_code: data.postal_code,
elevation: +data.elevation,
forest: +data.forest,
urban: +data.urban,
other: +data.other,
years_till_next_fire: +data.years_till_next_fire,
};
}
async function getData() {
const ca_counties = "./target-topojson/cb_2017_06_county_500k.topojson";
const ca_zipcodes = "./target-topojson/cb_2017_06_zcta510_500k-simple.topojson";
//const zip2county = "./d3-data/zcta_county_rel_10.csv";
const zip2county = "./target-json/zcta_county_rel_10-06.json";
const postcalcode2firedata = "./common-data/pass_2/ca_postalcode_fire_intersections_data.csv";
const postcalcode2landcoverage = "./common-data/pass_2/postal_code_fire_yearly_landcoverage.csv";
return await Promise.all([
d3.json(ca_counties),
d3.json(ca_zipcodes),
d3.json(zip2county),
d3.csv(postcalcode2firedata, formatFireData),
d3.csv(postcalcode2landcoverage, formatLandCoverageData),
])
}
function predictYearsUntilNextFire(variables, coefficients) {
const [elevation, forest, urban, other] = variables;
const [b, e, f, u, o] =
coefficients || [4.72102069, -.0000437228105, -0.935146471, 3.26084516, -2.352569869];
const year = b
+ (e*elevation)
+ (f*forest)
+ (u*urban)
+ (o*other)
return year;
}
function getWeatherData(zipCode) {
const proxyServer = "https://cors-anywhere.herokuapp.com/";
// TODO: move this into a private config...
const apiKey = "fa3197b32718f839f0a33d152abaa6a5";
const unit = 'imperial'; // metric
const openWeatherMapAPI = `https://api.openweathermap.org/data/2.5/weather?zip=${zipCode},us&appid=${apiKey}&units=${unit}`;
const url = `${proxyServer}${openWeatherMapAPI}`;
return d3.json(url);
}
function updateZipCodeDetails(data) {
let title = 'Fire Prediction for ZipCode';
if (data.zipCode) {
title += ` ${data.zipCode}`;
} else {
title += ` (Unselected)`;
}
d3.select('#zipcode-details-title').text(title);
d3.select("#fireCount").text(data.fireCount !== undefined ? data.fireCount : 'Unknown');
d3.select("#fireInYears").text(data.fireInYears !== undefined ? data.fireInYears : 'Unknown');
}
function updateLandcoverDetails(row) {
function valueOrUnknown(maybeValue, transformIfValue) {
if (maybeValue !== undefined) {
let definitelyValue = maybeValue;
if (transformIfValue) {
definitelyValue = transformIfValue(definitelyValue);
}
return definitelyValue;
}
return 'Unknown';
}
function toPercentage(maybeValue) {
return valueOrUnknown(maybeValue, d => Math.round(d * 100));
}
d3.select("#elevation").text(valueOrUnknown(row.elevation));
d3.select("#forest").text(toPercentage(row.forest));
d3.select('#forest-selection').property('value', row.forest * 100 || 0);
d3.select("#urban").text(toPercentage(row.urban));
d3.select('#urban-selection').property('value', row.urban * 100 || 0);
d3.select("#other").text(toPercentage(row.other));
d3.select('#other-selection').property('value', row.other * 100 || 0);
}
async function main() {
const [ca_cou, ca_zips, glue, fireHistory, landCoverage] = await getData();
const minYear = d3.min(fireHistory, row => row.fire_year);
const maxYear = d3.max(fireHistory, row => row.fire_year);
let currentYear = minYear;
let curFireHistory = fireHistory.filter(row => +row.fire_year === +minYear);
let curPostalCode = undefined;
updateZipCodeDetails({});
updateLandcoverDetails({});
// Details view on the right hand side
// d3.select('#details-container')
// fire_area_acres: "584.888061523437"
// fire_contained_date: "7/11/06"
// fire_name: "MESQUITE"
// fire_start_date: "7/9/06"
// fire_year: "2006"
// postal_code: "89019"
/* These 'infos' maps are for tieing zips->counties */
const zipInfos = d3.map();
glue.features.forEach(d => {
const arr = zipInfos.get(d.ZCTA5);
if (!arr) {
zipInfos.set(d.ZCTA5, [d]);
} else {
arr.push(d);
}
});
const countyInfos = d3.map();
ca_cou.objects.counties.geometries.forEach(d => {
countyInfos.set(d.properties.COUNTYFP, d.properties);
});
const body = d3.select('body')
// const margin = { left: 20, right: 20, top: 40, bottom: 5 };
const size = {
width: Math.min(600, window.innerWidth || document.body.clientWidth),
height: Math.min(720, window.innerHeight || document.body.clientHeight),
};
const svg = body.select('#map-container')
.attr('width', size.width)
.attr('height', size.height);
const zipCodeColorScale = d3.scaleSqrt()
.domain([0, curFireHistory.length])
.range(['#f9f9e3', 'darkorange']);
const map = svg.append('g');
const projection = d3.geoMercator()
.center([-117, 37])
.translate([size.width / 1.5, size.height / 2])
.scale([size.width * 5]);
drawCountyPaths();
drawZipCodePaths();
enableYearSlider()
enableZoom(svg, map);
// Title
svg
.append('text')
.attr('x', size.width / 2)
.attr('y', 30)
.style('text-anchor', 'middle')
.style('font-size', '2em')
.classed('title', true)
.text(`Fires in ${currentYear}`);
function enableZoom(svg, map) {
/* This enables ZOOM on the map:
* - scroll with mouse,
* - double click to zoom,
* - click and drag to PAN,
* - reset view with button */
const zoom = d3.zoom()
.scaleExtent([1, 5])
svg.call(zoom.on("zoom", () => {
map.attr("transform", d3.event.transform);
}));
d3.select("#reset-zoom")
.on("click", () => {
svg.transition()
.duration(100)
.call(zoom.transform, d3.zoomIdentity);
});
}
function enableYearSlider() {
const sliderID = "#year-selection";
d3.select(sliderID)
.property("min", minYear)
.property("max", maxYear)
.property("value", currentYear);
d3.select(sliderID)
.on("input", function () {
currentYear = +d3.event.target.value;
svg.select('.title')
.text(`Fires in ${currentYear}`)
curFireHistory = fireHistory.filter(row => +row.fire_year === currentYear);
d3.select('.zipcodes')
.selectAll("path")
.transition()
.attr('fill', d => {
const code = d.properties.ZCTA5CE10;
const fires = curFireHistory.filter(row => row.postal_code === code);
return zipCodeColorScale(fires.length);
});
})
.on('change', function() {
updateDetails(curPostalCode);
})
}
function drawCountyPaths() {
/* These are for generating html for the Tooltips
* when hovering over Zipcode and County(if no zip exists
* at location in county) */
const getCountyHTML = (d) => {
const countyHTML = [ `County Name: ${d.properties.NAME}` ]
.map(v => `<span class='tip-li'>${v}</span>`)
.join('');
return `<div class='tooltip'> ${countyHTML} </div>`;
};
const tipCounty = d3.tip()
.attr('class', 'd3-tip')
.offset([-15, 0])
.html(getCountyHTML);
// TODO: i wonder if drawing with canvas or using
// map tiles will be better performant
const path = d3.geoPath().projection(projection);
const counties = map.append("g")
.attr("class", "counties");
counties.call(tipCounty);
counties.selectAll("path")
.data(topojson.feature(ca_cou, ca_cou.objects.counties).features)
.enter()
.append("path")
.attr('class', 'county-path')
.attr('id', (d) => {
return `${d.properties.COUNTYFP}`;
})
.attr("d", path)
.on('mouseover', tipCounty.show)
.on('mouseout', tipCounty.hide);
}
function drawZipCodePaths() {
const getZipHTML = (d) => {
let props = d.properties;
const arr = zipInfos.get(props.ZCTA5CE10);
let zipCode = arr[0].ZCTA5;
let countyNames = arr
.map(el => countyInfos.get(el.COUNTY).NAME)
.join(', ');
const history = curFireHistory.filter(row => row.postal_code === zipCode);
const zipHTML = [
`County Name(s): ${countyNames}`,
`ZipCode: ${zipCode}`,
`# Fires: ${history.length} / ${curFireHistory.length}`]
.map(v => `<span class='tip-li'>${v}</span>`)
.join('');
return `<div class='tooltip'> ${zipHTML} </div>`;
};
const tipZip = d3.tip()
.attr('class', 'd3-tip')
.offset([-15, 0])
.html(getZipHTML);
const zipData = topojson.feature(ca_zips, ca_zips.objects.zipcodes).features;
const zipPath = d3.geoPath().projection(projection);
const zipcodes = map.append("g")
.attr("class", "zipcodes");
zipcodes.call(tipZip);
zipcodes.selectAll("path")
.data(zipData)
.enter()
.append("path")
.attr('class', 'zipcode-path')
.attr("d", zipPath)
.attr('fill', d => {
const code = d.properties.ZCTA5CE10;
const fires = curFireHistory.filter(row => row.postal_code === code);
return zipCodeColorScale(fires.length);
})
.on('mouseover', d => {
return tipZip.show(d)
})
.on('mouseout', tipZip.hide)
.on('click', async function(d) {
curPostalCode = d.properties.ZCTA5CE10;
updateDetails(curPostalCode);
updateHistSVG();
});
}
function getCurLandCoverage() {
return landCoverage.filter(
row => +row.year === +currentYear &&
row.postal_code === curPostalCode);
}
function updateLandcoverSpecifically(key, value) {
const curLandCoverage = getCurLandCoverage();
if (curLandCoverage.length) {
const row = Object.assign({}, curLandCoverage[0]);
row[key] = value;
updateLandcoverDetails(row);
const yearsUntilFire = predictYearsUntilNextFire([
row.elevation,
row.forest,
row.urban,
row.other
]);
d3.select("#fireInYears").text(yearsUntilFire.toFixed(1));
}
}
d3.select('#forest-selection')
.on('input', () => {
const value = +d3.event.target.value;
updateLandcoverSpecifically('forest', value/100);
});
d3.select('#urban-selection')
.on('input', () => {
const value = +d3.event.target.value;
updateLandcoverSpecifically('urban', value/100);
});
d3.select('#other-selection')
.on('input', () => {
const value = +d3.event.target.value;
updateLandcoverSpecifically('other', value/100);
});
function updateDetails(postalCode) {
const curLandCoverage = getCurLandCoverage();
// the land coverage data starts at 1997 and the last year is 2018.
// so we can't make any predictions, without that data.
let yearsUntilFire = undefined;
if (curLandCoverage.length) {
const row = curLandCoverage[0];
updateLandcoverDetails(row);
yearsUntilFire = predictYearsUntilNextFire([
row.elevation,
row.forest,
row.urban,
row.other
]);
} else {
updateLandcoverDetails({});
}
const fires = curFireHistory.filter(row => row.postal_code === curPostalCode);
updateZipCodeDetails({
zipCode: curPostalCode,
fireCount: fires.length,
fireInYears: yearsUntilFire ? yearsUntilFire.toFixed(1) : undefined,
});
getWeatherData(postalCode).then(weather => {
// weather.precipitation???
const rain_volume = weather.rain && weather.rain["1h"] || 0;
d3.select('#precipitation').text(rain_volume);
d3.select('#min-temp').text(weather.main.temp_min);
d3.select('#max-temp').text(weather.main.temp_max);
function predictTodayFireProb(vol, minTemp, maxTemp) {
return 0;
}
// TODO: use this to predict probabilty of fire TODAY
const prob = predictTodayFireProb(
rain_volume,
weather.main.temp_min,
weather.main.temp_max);
log(prob);
d3.select('#fireTodayProb').text(prob);
});
}
function updateHistSVG() {
// curPostalCode, currentYear
const fires = fireHistory.filter(row => row.postal_code === curPostalCode);
const width = 350;
const height = 250;
const padding = 40;
const histSVG = body.select('#histogram-svg')
.attr('width', width)
.attr('height', height);
const title = histSVG
.selectAll('.title')
.data([curPostalCode]);
title.exit().remove();
const newTitle = title
.enter()
.append('text')
.attr('x', width / 2)
.attr('y', 20)
.style('text-anchor', 'middle')
.style('font-size', '2em')
.classed('title', true);
newTitle.merge(title)
.text(`Fire Frequency at ${curPostalCode}`);
const xScale = d3.scaleLinear()
.domain([minYear, maxYear])
.range([0, width])
const optimalTicks = xScale.ticks();
const hist = d3.histogram()
.domain(xScale.domain())
.thresholds(optimalTicks)
.value(d => d.fire_year);
const bins = hist(fires);
const yScale = d3.scaleLinear()
.domain([0, d3.max(bins, d => d.length)])
.range([height, padding]);
const bars = histSVG
.selectAll('.bar')
.data(bins);
bars.exit().remove();
const g = bars.enter()
.append('g')
.classed('bar', true);
g.append('rect');
g.merge(bars)
.select('rect')
.transition()
.attr('x', d => xScale(d.x0))
.attr('y', d => yScale(d.length))
.attr('height', d => height - yScale(d.length))
.attr('width', d => {
const width = xScale(d.x1) - xScale(d.x0) - 1;
return width < 0 ? 0 : width;
})
.attr('fill', 'darkorange')
// const xAxis = d3.axisBottom(xScale)
// .tickSizeOuter(0)
// const yAxis = d3.axisLeft(yScale)
// .tickSizeOuter(0)
// histSVG.append('g')
// .attr('transform', `translate(0, ${height - padding})`)
// .call(xAxis);
// histSVG.append('g')
// .attr('transform', `translate(${padding}, 0)`)
// .call(yAxis);
}
}
main();
// TODO: use tilemap for better performance, possibly..
// // Alternatively using the leaflet zoom controls
// var mymap = L.map('mapid').setView([51.505, -0.09], 13);
// L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
// 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>',
// maxZoom: 18,
// id: 'mapbox.streets',
// accessToken: 'pk.eyJ1Ijoib3VscmljaDYiLCJhIjoiY2p0cmMwazR2MGZ0azQ1cGltNjNmb24waSJ9.uedEyuYU6dY5swCG9OHZTg'
// }).addTo(mymap);