-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
411 lines (326 loc) · 11 KB
/
map.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
mapboxgl.accessToken = 'pk.eyJ1IjoiYnJlYWt0aHJvdWdoLW1hcHMiLCJhIjoiY2t0MXhjcjh6MGZrdzJubzJpbXJ6ODczMiJ9.5rvh7Oj4MJOraoG2FcSrxw';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/peterqliu/cksunnhipaemh17qsf5iwlrhx/draft', // style URL
center: [-100, 40], // starting position [lng, lat]
zoom: 4 // starting zoom
});
var tooltip = new mapboxgl.Popup({
closeButton: false,
closeOnClick:false,
anchor:'left',
offset:10
})
.addTo(map);
const updateVis = category => {
// visualizing state vs county
if (category === 'level') {
console.log('level', state.level)
const stateLevel = state.level === 'State';
map
.setLayoutProperty('stat-label', 'visibility', stateLevel ? 'none': 'visible')
.setLayoutProperty('county-bg', 'visibility', stateLevel ? 'none': 'visible')
.setLayoutProperty('county-border', 'visibility', stateLevel ? 'none': 'visible')
d3.select('#scenario')
.attr('level', state.level)
if (state.level === 'County' && state.scenario === 'HighEV'){
d3
.select('#scenario')
.attr('level', state.level)
.selectAll('.toggle-container input')
.attr('checked', (d,i)=> {
if (state.level === 'County' && state.scenario === 'HighEV') {
state.scenario = 'SDS';
return i === 0 ? 'checked' : null;
}
// else return d === state.scenario ? 'checked' : null;
})
}
updateVis('visualize')
}
// toggling view between map and graph
else if (category === 'view') {
d3.select('body')
.attr('mode', state.view);
const mapView = state.view === 'map'
const visibility = mapView ? 'visible' : 'none';
const countiesVisible = mapView && state.level === 'County' ? 'visible' : 'none';
// toggle map elements
map
.setLayoutProperty(
'county-bg',
'visibility',
countiesVisible
)
.setLayoutProperty(
'state-bg',
'visibility',
visibility
)
.setLayoutProperty(
'stat-label',
'visibility',
countiesVisible
)
if (mapView) {
updateVis('visualize')
generateBarGeometry() // clear geometry
updateBarGraphLayers(true)
map.easeTo({
pitch:0,
zoom:3,
bearing:0,
duration:250
});
map.dragPan.enable();
}
else {
constants.layerModes
.forEach((l,i)=>l.forEach(layer =>map.setLayoutProperty(layer, 'visibility', 'none')))
return
}
}
// toggling stranded data vis scheme
else if (category === 'visualize') {
const index = constants.visualizes.indexOf(state.visualize) + constants.levels.indexOf(state.level)*2;
constants.layerModes
.forEach((l,i)=>l.forEach(layer =>map.setLayoutProperty(layer, 'visibility', i===index ? 'visible' : 'none')))
// map.easeTo({pitch: index === 2 ? 60 : 0})
return
}
// toggling county stat to visualize (without changing county)
else if (category === 'statistic') {
updateBarGraphLayers();
return
}
// toggling scenario and products
const prop = `${state.product}${state.scenario}`;
const onlyWithLoss = ['all', ['has', prop]];
const colorRamp = [
"interpolate",
["linear"],['get', prop],
0, '#e5e6e6',
0.001, "#ffd84d",
1,"#dd2727"
]
map
.setFilter(
'counties',
onlyWithLoss
)
.setPaintProperty(
'counties',
'fill-opacity',
['case', ['boolean', ['has', prop], true], 0.7, 0]
)
.setPaintProperty(
'states',
'fill-opacity',
['case', ['boolean', ['has', prop], true], 0.7, 0]
)
// update filters to show only areas with loss
.setFilter('states', onlyWithLoss)
.setFilter(
'counties-offshore',
onlyWithLoss
)
.setFilter(
'stat-label',
onlyWithLoss
)
.setFilter(
'stranded-volume-circle',
onlyWithLoss
)
.setFilter(
'states-volume-circle',
onlyWithLoss
)
// update color ramps to visualize loss percentage
// county fills
.setPaintProperty(
'counties',
'fill-color',
colorRamp
)
.setPaintProperty(
'counties',
'fill-outline-color',
colorRamp
)
// state fills
.setPaintProperty(
'states',
'fill-outline-color',
colorRamp
)
.setPaintProperty(
'states',
'fill-color',
colorRamp
)
.setPaintProperty(
'counties-offshore',
'circle-color',
colorRamp
)
// county circle fills
.setPaintProperty(
'stranded-volume-circle',
'circle-color',
colorRamp
)
.setPaintProperty(
'stranded-volume-circle',
'circle-stroke-color',
colorRamp
)
// state circle fills
.setPaintProperty(
'states-volume-circle',
'circle-color',
colorRamp
)
.setPaintProperty(
'states-volume-circle',
'circle-stroke-color',
colorRamp
)
// county circle radius
.setPaintProperty(
'stranded-volume-circle',
'circle-radius',
// ["get",`${prop}Vol`],
[
'interpolate', ['exponential', 2], ['zoom'],
4, ['*', ["sqrt",["get",`${prop}Vol`]], 1],
22, ['*', ["sqrt",["get",`${prop}Vol`]], 160000],
]
)
// state circle radius
.setPaintProperty(
'states-volume-circle',
'circle-radius',
[
'interpolate', ['exponential', 2], ['zoom'],
4, ['*', ["sqrt",["get",`${prop}Vol`]], 0.5],
22, ['*', ["sqrt",["get",`${prop}Vol`]], 160000],
]
)
.setLayoutProperty(
'stat-label',
'text-field',
["concat",
["get", 'c'],
// ' ',
// prop+'Vol',
' ',
["to-string",
["round",
["*",
["get", prop],100
]
]
],"% ",
// ["to-string",["get", `${prop}Vol`]],
]
)
}
map.on('load', ()=> {
updateVis();
updateVis('visualize');
setupBarGraphLayers();
d3.json('countyData.json', (e,r) => {
const countyData = processCountyData(r);
state.econData.County = countyData;
map.on('click', e => {
tooltip.remove();
const h = map.queryRenderedFeatures(e.point, {layers:[`${state.level.toLowerCase()}-bg`, 'counties-offshore', 'stranded-volume-circle']});
if (h[0]) {
console.log(h[0])
const props = h[0].properties;
const pole = JSON.parse(props.pole).map(s=>parseFloat(s));
map.easeTo({
center: [-100, 40],
duration:200,
zoom: 3.5,
pitch: 60,
easing: t => t
});
map.dragPan.disable();
d3.select('#countyLabel')
.text(placeName(props))
d3.select('#stateLabel')
.text(props.s)
const location = `${props.s}_${props.c || ''}`;
state.currentLocation = location;
generateBarGeometry(
{lng: -100, lat:40},
4,
state.econData[state.level][state.currentLocation]
);
state.view = 'graph';
updateVis('view');
map.once('moveend', ()=> {
updateBarGraphLayers();
setTimeout(()=>updateMaxLabel(), 1);
})
}
})
})
map.on('mousemove', e => {
// if looking at bar graph
if (state.view === 'graph') {
const h = map.queryRenderedFeatures(e.point);
const bar = h.find(l=>l.layer.type ==='fill-extrusion' && l.layer.id !=='boundingWalls');
if (!bar || isNaN(bar.properties.bar)) {
tooltip.remove();
if (state.currentBarIndex>=0){
state.currentBarIndex = undefined;
updateBarGraphColors()
}
return
}
tooltip
.setLngLat(map.unproject(e.point));
const barIndex = bar.properties.bar;
// exit if same highlight as before
if (barIndex === state.currentBarIndex) return
tooltip
.addTo(map)
state.currentBarIndex = barIndex;
const scenario = constants.graphRows.State[Math.floor(barIndex/constants.graphRows.State.length)];
const decade = barIndex % constants.graphRows.State.length
const number = state.econData[state.level][state.currentLocation][state.statistic][scenario][decade]
const context = `projected annually for ${constants.decades[decade]},<br> given <span class='color-gray-deep'>${scenario}</span> conditions`;
tooltip.setHTML(
`<div class="txt-h5" style='color:#44647e'>${formatStatistic[state.statistic](number)}</div>
<p class='quiet'>${context}</p>`
)
updateBarGraphColors(scenario, decade)
}
// if viewing map
else {
const targetLayer = `${state.level.toLowerCase()}-bg`
const h = map.queryRenderedFeatures(e.point, {layers:[targetLayer, 'counties-offshore', 'stranded-volume-circle']});
if (h[0]) {
const props = h[0].properties;
const stat = props[state.product+state.scenario]
const strandedStatement = stat ? `${Math.round(stat*100)}% ${state.product.toLowerCase()} production stranded in ${state.scenario}`
: `${state.product} production unaffected under ${state.scenario}`
tooltip
.setHTML(`<div class='txt-h5 txt-nowrap' style='color:#44647e'>${placeName(props)}</div>
<p>${strandedStatement}<br>
<i class='quiet'>Click for economic projections</i>
</p>`)
.setLngLat(map.unproject(e.point))
.addTo(map)
}
else tooltip.remove()
}
})
})
const placeName = props => {
if (state.level === 'State') return props.s
else return `${props.c}${props.s === 'Offshore' ? '': ' County'}, ${props.s}`
}