-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaretel.html
468 lines (399 loc) · 19.7 KB
/
faretel.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> Subway Ridership and Telework Capability </title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<style>
body {
margin: 0;
padding: 0;
}
#mainmap {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#legend {
position: absolute;
top: 1rem;
right: 1rem;
width: auto;
height: auto;
max-height: 90%;
padding-top: 1rem;
padding-right: 1rem;
padding-left: 1rem;
padding-bottom: 1rem;
color: rgba(0, 0, 0, 0.9);
background-color: rgba(255, 255, 255, 0.9);
font-family: sans-serif;
font-size: 1rem;
overflow: auto;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
border-radius: 4px;
}
#header {
font-weight: bold;
}
#chevron {
margin-left: 0.5rem;
}
#datadate {
font-size: 0.9rem;
}
.itemgroup1 {
margin-top: 0.5rem;
}
.item1-input {
width: 0.9rem;
height: 0.9rem;
margin-right: 0.5rem;
vertical-align: 1.2rem;
}
.item1-label {
font-size: 0.9rem;
font-weight: bold;
width: 19rem;
}
.itemgroup2 {
margin-left: 1.4rem;
}
.item2-input {
width: 0.8rem;
height: 0.8rem;
margin-right: 0.5rem;
vertical-align: -0.1rem;
}
.item2-label {
font-size: 0.8rem;
}
.key-circle {
width: 0.8rem;
height: 0.8rem;
margin-right: 0.5rem;
border-radius: 100%;
display: inline-block;
vertical-align: -0.1rem;
}
.key-line {
width: 0.8rem;
height: 0.3rem;
margin-right: 0.5rem;
border-radius: 0%;
display: inline-block;
vertical-align: 0.15rem;
}
.key-fill {
width: 0.8rem;
height: 0.8rem;
margin-right: 0.5rem;
border-radius: 0%;
display: inline-block;
vertical-align: -0.1rem;
}
</style>
</head>
<body>
<div id="mainmap"></div> <!-- placeholder for main map -->
<nav id="legend">
<div class="container g-0">
<div class="row g-0 justify-content-between" id="header">
<div class="col-auto"> Subway Ridership and Telework Capability </div>
<div class="col-auto">
<i class="bi bi-chevron-down" id='chevron'></i>
</div>
</div>
<div class="row g-0 justify-content-start" id="datadate">
<div class="col-auto"> </div>
</div>
<div class="row g-0 justify-content-start">
<div class="col-auto" id="legends"> </div> <!-- placeholder for layer selector and legend -->
</div>
</div>
</nav>
<script>
'use strict';
// Set MapBox token
mapboxgl.accessToken = 'pk.eyJ1IjoibWF5aWp1biIsImEiOiJjaXg4ZmlyY20wMDBjMm9tcjI0bjQ0Z21zIn0.Io0XJ4JelN903V9HGo4TfQ'; // still need to find a way to store the token
// Initialize the map
var map = new mapboxgl.Map({ // creating a new map
container: 'mainmap', // same as the div id to place the map
style: 'mapbox://styles/mapbox/light-v10', // light base map
hash: true, // make sure it's on to get the url with map view
attributionControl: true, // show the credit bar
customAttribution: '<b> Data Sources: <a href="http://web.mta.info/developers/fare.html" target="blank">MTA Fare Data</a>; <a href="https://lehd.ces.census.gov/" target="blank">Census Bureau LEHD 2017</a></b>', // add overall credit to the beggining
preserveDrawingBuffer: true, // make sure it's on to allow printing
center: [-73.9469, 40.7121], // testing to get the centroid of bounds
zoom: 9.5, // set the initial zoom of the map view
});
// Add layers
map.on('load', function () {
var layerContentList = []; // store all the layer contents
// Add navigation control
map.addControl(new mapboxgl.NavigationControl({
showCompass: true, // show compass
showZoom: true, // show zoom
visualizePitch: true, // show pitch
}),
'top-left', // control position
);
// Add full screen control
map.addControl(new mapboxgl.FullscreenControl({
container: document.querySelector('body'),
}),
'top-left', // control position
);
// Add telework am
// Set layer contents
var layerContent = {
'sourceid': 'telsubam', // source id
'sourcetype': 'geojson', // source type
'sourcedata': 'telsubam.geojson', // data source
'layerid': 'telsubam', // layer id
'layertype': 'fill', // symbology type
'layername': 'Telework Capability of Subway Commuters by Place of Residence', // layer name for the legend
'layervisibility': 'visible', // visibility of the layer
'layercolname': 'cat', // category column name
'layercat': ['22%~30%', '31%~35%', '36%~54%'], // categories
'layercolor': ['rgba(209, 227, 243, 0.7)', 'rgba(154, 200, 225, 0.7)', 'rgba(82, 157, 204, 0.7)'], // color for each category; use rgba
'layeropacity': 1, // fill opacity
'layeroutlinecolor': 'rgba(255,255,255,0)', // outline color; for legend only
};
// Generate layer colors
var layerColor = ['match', ['get', layerContent['layercolname']]];
for (var i = 0; i < layerContent.layercat.length; i++) {
layerColor.push(layerContent.layercat[i]);
layerColor.push(layerContent.layercolor[i]);
};
layerColor.push('#000000');
// Add layer content to the overall layer content list
layerContentList.push(layerContent);
// Add data source
map.addSource(layerContent['sourceid'], {
'type': layerContent['sourcetype'],
'data': layerContent['sourcedata'],
});
// Add fill layer
map.addLayer({
'id': layerContent['layerid'],
'type': layerContent['layertype'],
'source': layerContent['sourceid'],
'layout': {
'visibility': layerContent['layervisibility'],
},
'paint': {
'fill-color': layerColor,
'fill-opacity': layerContent['layeropacity'],
},
});
// Add fare
// Set layer contents
var layerContent = {
'sourceid': 'fare', // source id
'sourcetype': 'geojson', // source type
'sourcedata': 'fare.geojson', // data source
'layerid': 'fare', // layer id
'layertype': 'circle', // symbology type
'layername': 'Current Weekly Ridership as Percentage of Equivalent Week in 2019', // layer name for the legend
'layervisibility': 'visible', // visibility of the layer
'layercolname': 'PctCat', // category column name
'layercat': ['<=30%', '31%~40%', '>40%'], // categories
'layercolor': ['rgba(153, 52, 4, 0.9)', 'rgba(223, 103, 17, 0.9)', 'rgba(254, 171, 70, 0.9)'], // color for each category; use rgba
'layeropacity': 1, // circle opacity
'layerradius': 2.5, // circle radius; change size based on zoom level
'layeroutlinecolor': 'rgba(255,255,255,0)', // outline color; for legend only
};
// Generate layer colors
var layerColor = ['match', ['get', layerContent['layercolname']]];
for (var i = 0; i < layerContent.layercat.length; i++) {
layerColor.push(layerContent.layercat[i]);
layerColor.push(layerContent.layercolor[i]);
};
layerColor.push('#000000');
// Add layer content to the overall layer content list
layerContentList.push(layerContent);
// Add data source
map.addSource(layerContent['sourceid'], {
'type': layerContent['sourcetype'],
'data': layerContent['sourcedata'],
});
// Add circle layer
map.addLayer({
'id': layerContent['layerid'],
'type': layerContent['layertype'],
'source': layerContent['sourceid'],
'layout': {
'visibility': layerContent['layervisibility'],
},
'paint': {
'circle-color': layerColor,
'circle-opacity': layerContent['layeropacity'],
'circle-radius': layerContent['layerradius'],
},
});
// Add popup
var popup = new mapboxgl.Popup({
closeButton: true, // close button
closeOnClick: false, // close if click on map
closeOnMove: false, // close if move the map
anchor: 'bottom', // anchor of the popup
offset: 0, // offset from the feature
maxWidth: 'none', // max width of the popoup; 'none' to fit to the content
});
// Add telam popup
// Add click event
map.on('click', 'telsubam', function (e) {
var coordinates = e.lngLat; // get pointer coordinates
var description = "<b>PUMA: </b><span>" + e.features[0].properties.puma + "</span><br>"; // description in the popup
description += "<b>Telework Capability by Place of Residence: </b><span>" + (Number(e.features[0].properties.telsubpct) * 100).toFixed(2) + "%</span><br>";
popup.setLngLat(coordinates).setHTML(description).addTo(map); //add popup
});
// Add hover events
map.on('mouseenter', 'telsubam', function () {
map.getCanvas().style.cursor = 'pointer'; // mouse becoming pointer
});
map.on('mouseleave', 'telsubam', function () {
map.getCanvas().style.cursor = '';
});
// Add fare popup
// Add click event
map.on('click', 'fare', function (e) {
var coordinates = e.features[0].geometry.coordinates.slice(); // get point coordinates
var description = "<b>Complex ID: </b><span>" + e.features[0].properties.CplxID + "</span><br>"; // description in the popup
description += "<b>Borough: </b><span>" + e.features[0].properties.Borough + "</span><br>";
description += "<b>Complex Name: </b><span>" + e.features[0].properties.CplxName + "</span><br>";
description += "<b>Routes: </b><span>" + e.features[0].properties.Routes + "</span><br>";
description += "<b>Current Week: </b><span>" + e.features[0].properties.WeekTD + "</span><br>";
description += "<b>Current Weekly Ridership: </b><span>" + e.features[0].properties.FareTD.toLocaleString() + "</span><br>";
description += "<b>Equivalent Week in 2019: </b><span>" + e.features[0].properties.WeekPR + "</span><br>";
description += "<b>2019 Weekly Riderhsip: </b><span>" + e.features[0].properties.FarePR.toLocaleString() + "</span><br>";
description += "<b>Current Weekly Ridership as Percentage of 2019 Ridership: </b><span>" + (Number(e.features[0].properties.Pct) * 100).toFixed(2) + "%</span><br>";
popup.setLngLat(coordinates).setHTML(description).addTo(map); //add popup
});
// Add hover events
map.on('mouseenter', 'fare', function () {
map.getCanvas().style.cursor = 'pointer'; // mouse becoming pointer
});
map.on('mouseleave', 'fare', function () {
map.getCanvas().style.cursor = '';
});
// Add layer selector and legend
var layerOrder = ['fare', 'telsubam']; // set layer order in the legend
var layerContentListOrder = []; // create ordered layer content list
for (var i = 0; i < layerOrder.length; i++) {
layerContentListOrder.push(layerContentList.filter(e => e.layerid == layerOrder[i])[0]);
};
var filterLegend = document.getElementById('legends');
for (var i = 0; i < layerContentListOrder.length; i++) {
// Create layer selector
var itemgroup1 = document.createElement('div'); // create level-1 item group
itemgroup1.className = 'itemgroup1';
var item1 = document.createElement('div'); // create level-1 item
item1.className = 'item1';
var input1 = document.createElement('input'); // create level-1 checkbox
input1.type = 'checkbox';
input1.className = 'item1-input';
input1.id = layerContentListOrder[i]['layerid'];
if (layerContentListOrder[i]['layervisibility'] === 'visible') {
input1.checked = true;
} else {
input1.checked = false;
};
var label1 = document.createElement('label'); // create level-1 checkbox label
label1.className = 'item1-label';
label1.setAttribute('for', layerContentListOrder[i]['layerid']);
label1.textContent = layerContentListOrder[i]['layername'];
item1.appendChild(input1);
item1.appendChild(label1);
itemgroup1.appendChild(item1);
// Create legend selector
var itemgroup2 = document.createElement('div'); // create level-2 item group
itemgroup2.className = 'itemgroup2';
itemgroup2.id = layerContentListOrder[i]['layerid'] + '-cat';
if (layerContentListOrder[i]['layervisibility'] === 'visible') {
itemgroup2.style.display = 'block';
} else {
itemgroup2.style.display = 'none';
};
for (var j = 0; j < layerContentListOrder[i]['layercat'].length; j++) {
var item2 = document.createElement('div'); // create level-2 item
item2.className = 'item2';
var input2 = document.createElement('input'); // create level-2 checkbox
input2.type = 'checkbox';
input2.className = 'item2-input';
input2.id = layerContentListOrder[i]['layerid'] + '|' + layerContentListOrder[i]['layercolname'] + '|' + layerContentListOrder[i]['layercat'][j];
input2.checked = true;
var key2 = document.createElement('span'); // create level-2 key
key2.className = 'key-' + layerContentListOrder[i]['layertype'];
key2.style.backgroundColor = layerContentListOrder[i]['layercolor'][j];
key2.style.borderColor = layerContentListOrder[i]['layeroutlinecolor'];
key2.style.borderStyle = 'solid';
key2.style.borderWidth = 'thin';
var label2 = document.createElement('label'); // create level-2 label
label2.className = 'item2-label';
label2.setAttribute('for', layerContentListOrder[i]['layerid'] + '|' + layerContentListOrder[i]['layercolname'] + '|' + layerContentListOrder[i]['layercat'][j]);
label2.textContent = layerContentListOrder[i]['layercat'][j];
item2.appendChild(input2);
item2.appendChild(key2);
item2.appendChild(label2);
itemgroup2.appendChild(item2);
// Create legend selector click event
input2.addEventListener('click', function (e) {
var clickedLayer = this.id.split('|')[0];
var clickedLayerColname = this.id.split('|')[1];
var catGroup = document.getElementById(clickedLayer + '-cat').getElementsByClassName('item2-input');
var checkedFilter = ['any'];
for (var k = 0; k < catGroup.length; k++) {
if (catGroup[k]['checked'] === true) {
checkedFilter.push(['==', clickedLayerColname, catGroup[k].id.split('|')[2]]);
};
};
map.setFilter(clickedLayer, checkedFilter);
});
};
itemgroup1.appendChild(itemgroup2);
filterLegend.appendChild(itemgroup1);
// Create layer selector click event
input1.addEventListener('click', function (e) {
var clickedLayer = this.id;
var vis = map.getLayoutProperty(clickedLayer, 'visibility');
if (vis === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none'); // set layer visibility
document.getElementById(clickedLayer + '-cat').style.display = 'none'; // set legend visibility
} else {
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
document.getElementById(clickedLayer + '-cat').style.display = 'block';
};
});
};
});
// Add data dates
Plotly.d3.json("fare.geojson", function (data) {
var weektd = data['features'][0]['properties']['WeekTD'];
var weekpr = data['features'][0]['properties']['WeekPR'];
document.getElementById('datadate').textContent = '(' + weektd + ' vs ' + weekpr + ')';
});
// Set collapse event
var collapsevalue = 0;
header.addEventListener('click', function () {
if (collapsevalue === 0) {
document.getElementById('legends').style.display = "none";
document.getElementById('chevron').className = "bi bi-chevron-up";
collapsevalue = 1;
} else {
document.getElementById('legends').style.display = "block";
document.getElementById('chevron').className = "bi bi-chevron-down";
collapsevalue = 0;
};
});
</script>
</body>
</html>