-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
513 lines (464 loc) · 14.8 KB
/
script.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
let source = document.getElementById('info-pane-template').innerHTML;
let template = Handlebars.compile(source);
const CENSUS_BLOCK_PBF_URL = "./data/cb_2010.pbf"
const ANALYSIS_DATA_URL = "./data/analysis_values.csv"
const CAC_PBF_URL = "./data/ral_cacs.pbf"
const SUBDIVISIONS_PBF_URL = "./data/wake_subdivisions.pbf"
const COUNCIL_DISTRICTS_PBF_URL = "./data/ral_council_districts.pbf"
mapboxgl.accessToken = "pk.eyJ1IjoicHJjcmRldmxhYiIsImEiOiJjamljNWE0Z2owMGJjM2tzM3gxYmRrNXZnIn0.exFKTScPuDEIqeY-Rv36gQ"
let map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/prcrdevlab/cjsfao9os15fc1fmuot5nthgs',
center: [-78.638176, 35.779591],
zoom: 13
});
Promise.all([
d3.csv(ANALYSIS_DATA_URL, (d) => {
return {
area: +d.area,
geoid10: d.geoid10,
grade: d.grade,
gw_score_loop_contiguous: +d.gw_score_loop_contiguous,
la_acre_score: +d.la_acre_score,
la_dist_score: +d.la_dist_score,
la_exp_score: +d.la_exp_score,
la_gw_total_score: +d.la_gw_total_score,
la_total_score: +d.la_total_score,
los_acre_score: +d.los_acre_score,
los_dist_score: +d.los_dist_score,
los_exp_score: +d.los_exp_score,
los_gw_total_score: +d.los_gw_total_score,
los_total_score: +d.los_total_score,
popdensity_sqmi: +d.popdensity_sqmi,
tothh: +d.tothh,
totpop_2018: +d.totpop_2018,
select_extrusion: +d.popdensity_sqmi * 0.0276752767527675
}
}),
d3.buffer(CENSUS_BLOCK_PBF_URL),
d3.buffer(CAC_PBF_URL),
d3.buffer(SUBDIVISIONS_PBF_URL),
d3.buffer(COUNCIL_DISTRICTS_PBF_URL)
]).then(([analysisData, censusBlockPbf, cacPbf, subdivisionsPbf, councilDistrictsPbf]) => {
let censusBlockData = geobuf.decode(new Pbf(censusBlockPbf))
let cacData = geobuf.decode(new Pbf(cacPbf))
let subdivisionsData = geobuf.decode(new Pbf(subdivisionsPbf))
let councilDistrictsData = geobuf.decode(new Pbf(councilDistrictsPbf))
map.once('data', () => {
///////////////////////////////////
// CENSUS BLOCKS W/ EBPA SCORES //
/////////////////////////////////
map.addSource("cb-source", {
"type": 'geojson',
"data": censusBlockData
})
map.addLayer({
"id": "cb-fill-layer",
"type": "fill",
"source": "cb-source",
// "source": {
// "type": 'geojson',
// "data": censusBlockData
// },
"layout": {},
"paint": {
"fill-color": "#abc123",
"fill-opacity": 0
}
});
map.on('mouseenter', 'cb-fill-layer', (e) => {
map.getCanvas().style.cursor = 'pointer'
})
map.on('mouseleave', 'cb-fill-layer', (e) => {
map.getCanvas().style.cursor = 'grab'
})
///////////
// CACs //
/////////
map.addSource("cac-source", {
"type": 'geojson',
"data": cacData
})
map.addLayer({
"id": "cac-fill-layer",
"type": "fill",
"source": "cac-source",
"layout": {},
"paint": {
"fill-color": "#0f8ba1",
"fill-opacity": 0
}
})
//////////////////
// SUBDIVSIONS //
////////////////
map.addSource("subdivisions-source", {
"type": 'geojson',
"data": subdivisionsData
})
map.addLayer({
"id": "subdivisions-fill-layer",
"type": "fill",
"source": "subdivisions-source",
"layout": {},
"paint": {
"fill-color": "#c3018d",
"fill-opacity": 0
}
})
////////////////////////
// COUNCIL DISTRICTS //
//////////////////////
map.addSource("council-districts-source", {
"type": 'geojson',
"data": councilDistrictsData
})
map.addLayer({
"id": "council-distrcts-fill-layer",
"type": "fill",
"source": "council-districts-source",
"layout": {},
"paint": {
"fill-color": "#8ec30b",
"fill-opacity": 0
}
})
////////////////////////////
// SELECTED CENSUS BLOCK //
//////////////////////////
map.addSource("cb-selected-source", {
"type": 'geojson',
"data": turf.featureCollection([])
})
// map.addLayer({
// "id": "cb-selected-fill-layer",
// "type": "fill",
// "source": "cb-selected-source",
// "layout": {},
// "paint": {
// "fill-color": "green",
// "fill-opacity": 0.35
// }
// }, 'building')
map.addLayer({
"id": "cb-selected-line-layer",
"type": "line",
"source": "cb-selected-source",
"layout": {},
"paint": {
"line-color": "#1eded2",
"line-width": 2
}
})
////////////////
// ISOCHRONE //
//////////////
map.addSource("isochrone-source", {
"type": 'geojson',
"data": turf.featureCollection([])
})
// map.addLayer({
// "id": "isochrone-fill-layer",
// "type": "fill",
// "source": 'isochrone-source',
// "layout": {},
// "paint": {
// "fill-color": "#5E35B1",
// "fill-opacity": 0.35
// }
// }, 'building')
map.addLayer({
"id": "isochrone-line-layer",
"type": "line",
"source": 'isochrone-source',
"layout": {},
"paint": {
"line-color": "#121212",
"line-width": 1,
"line-dasharray": [2, 2]
}
}, 'road-label-small')
//////////////
// BUFFFER //
////////////
map.addSource("buffer-source", {
"type": 'geojson',
"data": turf.featureCollection([])
})
// map.addLayer({
// "id": "buffer-line-layer",
// "type": "line",
// "source": "buffer-source",
// "layout": {},
// "paint": {
// "line-color": "#121212",
// "line-width": 2,
// "line-dasharray": [4, 4]
// }
// })
map.addSource("nearby-census-blocks-source", {
"type": 'geojson',
"data": turf.featureCollection([])
})
map.addLayer({
"id": "nearby-census-blocks-fill-layer",
"type": "fill",
"source": "nearby-census-blocks-source",
"layout": {},
"paint": {
"fill-color": [
'match',
['get', 'grade'],
'A', '#1a9641',
'B', '#a6d96a',
'C', '#ffffbf',
'D', '#fdae61',
'F', '#d7191c',
'#121212'
],
"fill-opacity": 0.5
}
}, 'waterway-river-canal')
})
map.on('click', 'cb-fill-layer', (e) => {
updateApp(e)
})
// TODO: Use URL hash to open map with a particular Census Block already selected
function updateApp(e) {
removeClassFromElement(document.getElementById("toggle-view-button"), "hide")
let selectedCensusBlockFC = turf.featureCollection([e.features[0]])
let selectedCensusBlockInfo = clickedFeatureInfo(e, 'geoid10', analysisData, 'geoid10')[0]
console.log(selectedCensusBlockInfo)
selectedCensusBlockFC.features[0].properties.select_extrusion = selectedCensusBlockInfo.select_extrusion
// TODO: BUFFER CENSUS BLOCK POLYGON AND GET INFORMATION ABOUT SURROUNDING CENSUS BLOCKS. NAMELY THIS WOULD BE USED TO FIND THE AVERAGE SCORE OF THE SURROUNDING CENSUS BLOCKS TO USE FOR COMPARISON TO THE SELECTED CENSUS BLOCK. This works but needs to be cleaned up. Some functions probably need to be refactored to make this work well.
let selectedCensusBlockBuffer = turf.buffer(selectedCensusBlockFC, 1, {units: "miles"})
map.getSource('buffer-source').setData(selectedCensusBlockBuffer)
map.fitBounds(turf.bbox(selectedCensusBlockBuffer), {
padding: 10
})
let intersectPolygonsFeatureCollection = intersectingPolygons(selectedCensusBlockBuffer, censusBlockData)
let intersectingPolygonInfos = []
turf.featureEach(intersectPolygonsFeatureCollection, (f) => {
// console.log(clickedFeatureInfo(f, 'geoid10', analysisData, 'geoid10'))
let fProperties = [f.properties];
let fEnrichedFeature = turf.feature(f.geometry,
alasql(`SELECT * FROM ? fProperties JOIN ? analysisData ON fProperties.geoid10 = analysisData.geoid10`, [fProperties, analysisData])[0])
fEnrichedFeature.properties.grade = losScoreToGrade(parseInt(fEnrichedFeature.properties.los_gw_total_score))
intersectingPolygonInfos.push(fEnrichedFeature)
})
let intersectingPolygonsEnriched = turf.featureCollection(intersectingPolygonInfos)
console.log(intersectingPolygonsEnriched)
map.getSource('nearby-census-blocks-source').setData(intersectingPolygonsEnriched)
// UPDATE SELECTED CENSUS BLOCK
map.getSource('cb-selected-source').setData(selectedCensusBlockFC)
// UPDATE 10-MINUTE WALK ISOCHRONE
// Get a point on surface of selected Census Block
let selectedCensusBlockPOS = turf.pointOnFeature(selectedCensusBlockFC)
// Get the coordinates of the point on surface
let selectedCensusBlockPOSCoordinates = turf.getCoord(selectedCensusBlockPOS)
// Request isochrone from Mapbox Directions API. When it is returned, set data on appropriate sources.
mapbox10MinuteWalkIsochrone(selectedCensusBlockPOSCoordinates, mapboxgl.accessToken)
.then((res) => {
map.getSource('isochrone-source').setData(res)
// map.fitBounds(turf.bbox(res), {
// padding: 10
// })
})
// FIND CAC
let cac = containingPolygonProperties(selectedCensusBlockPOS, cacData)
// FIND SUVDIVISION
let subdivision = containingPolygonProperties(selectedCensusBlockPOS, subdivisionsData)
// FIND CITY COUNCIL DISTRICT
let council = containingPolygonProperties(selectedCensusBlockPOS, councilDistrictsData);
// UPDATE INFO PANE
// Remove initial info pane info if it's there
removeElementById('initial-info-pane-content')
// Update elements of the info pane
mapboxReverseGeocode(selectedCensusBlockPOSCoordinates, mapboxgl.accessToken)
.then((res) => {
let addressBasic = ''
if (res.features[0].address) {
addressBasic += `${res.features[0].address} `
}
if (res.features[0].text) {
addressBasic += `${res.features[0].text}`
}
try {
updateHandlebarElement('info-pane', template, {
censusBlockId: selectedCensusBlockInfo.geoid10,
censusBlockAddress: addressBasic,
los: losScoreToGrade(parseInt(selectedCensusBlockInfo.los_gw_total_score)),
lalos: losScoreToGrade(parseInt(selectedCensusBlockInfo.la_gw_total_score)),
cac: () => {
if(!cac) {
return "-"
} else {
return cac.NAME
}
},
subdivision: () => {
if (!subdivision) {
return "-"
} else {
return subdivision.NAME;
}
},
council: () => {
if (!council) {
return "-"
} else {
return `District ${council.COUNCIL_DIST} - ${council.COUNCIL_PERSON}`
}
}
})
} catch (e) {
console.log(e)
} finally {
let chartLabels = ['Distance', 'Acre', 'Experience', 'Greenway']
let barColors = ["#9C27B0", "#FFC107", "#2196F3", "#4CAF50"]
ebpaScoreBarChart('los-chart',
chartLabels,
[
selectedCensusBlockInfo.los_dist_score,
selectedCensusBlockInfo.los_acre_score,
selectedCensusBlockInfo.los_exp_score,
selectedCensusBlockInfo.gw_score_loop_contiguous
],
barColors,
'Individual Factor Scores'
)
ebpaScoreBarChart('la-chart',
chartLabels,
[
selectedCensusBlockInfo.la_dist_score,
selectedCensusBlockInfo.la_acre_score,
selectedCensusBlockInfo.la_exp_score,
selectedCensusBlockInfo.gw_score_loop_contiguous
],
barColors,
'Individual Factor Scores'
)
}
// ADD MAPS TO INFO PANE
// Zoning
infoPaneEsriThematicMap('zoning-map', selectedCensusBlockFC, 'https://maps.raleighnc.gov/arcgis/rest/services/Planning/Zoning/MapServer', [0], 0.4, true, ['ZONING'])
// Future Land Use
infoPaneEsriThematicMap('flu-map', selectedCensusBlockFC, 'https://maps.raleighnc.gov/arcgis/rest/services/Planning/FutureLandUse/MapServer', [0], 0.4, true, ['Land_Use'])
// Greenway
infoPaneEsriThematicMap('greenway-map', selectedCensusBlockFC, 'https://maps.raleighnc.gov/arcgis/rest/services/Parks/Greenway/MapServer', [0,1,3], 0.4, true, ['LOCATION', 'CORRIDOR'])
// Flood Plain
infoPaneEsriThematicMap('flood-map', selectedCensusBlockFC, 'https://maps.wakegov.com/arcgis/rest/services/Environmental/FloodData/MapServer', [0], 0.4, true, ['ZONE_IMAPS'])
})
}
})
function removeClassFromElement(element, className) {
try {
element.classList.remove(className)
} catch(error) {
console.log(error)
}
}
let extrusion = false;
let toggleViewButton = document.getElementById('toggle-view-button');
let toggleViewButtonIcon = document.getElementById('toggle-view-button-icon');
document.addEventListener('DOMContentLoaded', () => {
M.Tooltip.init(toggleViewButton)
})
toggleViewButton.addEventListener('click', () => {
if (map.getLayer("nearby-census-blocks-fill-layer")){
map.removeLayer("nearby-census-blocks-fill-layer")
}
if (map.getLayer("cb-selected-line-layer")) {
map.removeLayer("cb-selected-line-layer")
}
if(!extrusion){
map.easeTo({'pitch': 45})
map.addLayer({
"id": "nearby-census-blocks-fill-layer",
"type": "fill-extrusion",
"source": "nearby-census-blocks-source",
"layout": {},
"paint": {
"fill-extrusion-color": [
'match',
['get', 'grade'],
'A', '#1a9641',
'B', '#a6d96a',
'C', '#ffffbf',
'D', '#fdae61',
'F', '#d7191c',
'#121212'
],
"fill-extrusion-opacity": 1,
"fill-extrusion-height": ['get', 'select_extrusion']
}
})
map.addLayer({
"id": "cb-selected-line-layer",
"type": "fill-extrusion",
"source": "cb-selected-source",
"layout": {},
"paint": {
"fill-extrusion-color": "#1eded2","fill-extrusion-height": ['get', 'select_extrusion'],
"fill-extrusion-base": 0
}
})
toggleViewButtonIcon.innerHTML = 'map';
} else if(extrusion) {
map.easeTo({ 'bearing':0, 'pitch': 0 })
map.addLayer({
"id": "nearby-census-blocks-fill-layer",
"type": "fill",
"source": "nearby-census-blocks-source",
"layout": {},
"paint": {
"fill-color": [
'match',
['get', 'grade'],
'A', '#1a9641',
'B', '#a6d96a',
'C', '#ffffbf',
'D', '#fdae61',
'F', '#d7191c',
'#121212'
],
"fill-opacity": 0.5
}
}, 'waterway-river-canal')
map.addLayer({
"id": "cb-selected-line-layer",
"type": "line",
"source": "cb-selected-source",
"layout": {},
"paint": {
"line-color": "#1eded2",
"line-width": 2
}
})
toggleViewButtonIcon.innerHTML = 'language';
}
extrusion = !extrusion
})
function mapboxReverseGeocode(coordinates, token) {
return d3.json(`https://api.mapbox.com/geocoding/v5/mapbox.places/${coordinates[0]},${coordinates[1]}.json?access_token=${token}`)
}
function clickedFeatureInfo(selectedFeature, selectedFeatureId, joinTable, joinTableId) {
let selectedFeatureArray = [selectedFeature.features[0].properties]
return alasql(`SELECT * FROM ? selectedFeatureArray JOIN ? joinTable ON selectedFeatureArray.${selectedFeatureId} = joinTable.${joinTableId}`, [selectedFeatureArray, joinTable])
}
function losScoreToGrade(score) {
return score > 16 ? 'A' :
score > 12 ? 'B' :
score > 8 ? 'C' :
score > 4 ? 'D' :
score == 4 ? 'F' :
'-';
}
function losScoreToColor(score) {
return score > 16 ? '#1a9641' :
score > 12 ? '#a6d96a' :
score > 8 ? '#ffffbf' :
score > 4 ? '#fdae61' :
score == 4 ? '#d7191c' :
'#121212';
}