-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
executable file
·527 lines (477 loc) · 19.2 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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<!DOCTYPE html>
<html>
<head>
<title>Transit Analyst</title>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0001, minimum-scale=1.0001, maximum-scale=1.0001, user-scalable=no">
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/otpagraph.css" />
<link rel="stylesheet" href="css/leaflet.css" />
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<link rel="stylesheet" href="css/Control.Geocoder.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.0/lodash.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="js/d3.min.js" charset="utf-8"></script>
<script src="js/leaflet.utfgrid.js" charset="utf-8"></script>
<script src="js/OTPAGraph.js" charset="utf-8"></script>
<script src="js/OTPAGraphBar.js" charset="utf-8"></script>
<script src="js/Control.Geocoder.js"></script>
<!--
<script src="js/OTPAGraphLine.js" charset="utf-8"></script>
<script src="js/OTPAGraphCircle.js" charset="utf-8"></script>
<script src="js/OTPAGraphTable.js" charset="utf-8"></script>
<script src="js/OTPAGraphDensity.js" charset="utf-8"></script>
-->
<script src="js/LocationLayer.js"></script>
<script src="js/OTPALayer.js"></script>
</head>
<body>
<ul class="nav nav-tabs" role="tablist" id="tab-switcher">
<li role="presentation" class="active"><a id="map-tab" href="#map" aria-controls="home" role="tab" data-toggle="tab">Map</a></li>
<li role="presentation"><a href="#box" aria-controls="home" role="tab" data-toggle="tab">Info</a></li>
</ul>
<div class="tab-content">
<div id="map" role="tabpanel" class="tab-pane fade in active"></div>
<div id="box" role="tabpanel" class="tab-pane fade">
<div class="info-container">
<h1>Transit Analyst <img src="images/ajax-loader.gif" id="spinner"></h1>
<p></p>
<h4>Locations reachable within <span id="minutes">30</span> minutes:</h4>
<table id="options">
<tr><td>Travel time:</td><td><div id="slider"></div></td>
<tr><td>Point set:</td><td><select id="pointsets"></select></td>
<tr hidden="true"><td>Indicators:</td><td><select id="indicators"></select></td>
</table>
<div id="graph"></div>
<hr>
<div id="block-info"></div>
<div id="info"></div>
</div>
</div>
</div>
<div class="modal fade" id="legend" tabindex="-1">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
<h4 class="modal-title">Transit Analyst</h4>
</div>
<div class="modal-body">
<h4>
Transit Analyst is an interactive tool for exploring transit accessibility to target resources from focus areas.
</h4>
<ul style="margin-left: 20px">
<li>The <img src="js/images/marker-icon.png" style="width: 15px"> marker shows the current location. The black points are those accesible from that point within the selected travel time, and the gray points are not accessible.</li>
<li class="help-small">Click or tap on the "Info" tab to see detailed information for the current location</li>
<li>Choose a pointset to explore, and hover over or tap the points to see information for that location.</li>
<li>Move the mouse over the map to show blocks, and click or tap on a block to see detailed information for that block.</li>
<li>The "Show Travelshed" button will bring up a travelshed centered on the marker. Each band of color represents an area that can be traveled to on public transit, in 15 minute increments.</li>
<li>Information on data sources, methodology, and source code available <a href="http://azavea.github.io/transit-analyst/">here</a>.</li>
</ul>
</div>
</div>
</div>
</div>
<script type="text/template" id="block-template">
<h4 style="font-weight: bold;">Census Block <%= geoid10 %></h4>
<button href="#" id='toggle-travelshed' data-lat="<%= lat %>" data-lng="<%= lng %>"><%= toggleMessage %></button>
<ul id="bools-list" class="clearfix">
<% _.each(bools, function(item) { %>
<li class="col-xs-4">
<span class="<%= item.key %> <% if (item.value) { %>active<% } %>" title="<%= item.label %>">
<%= item.shortLabel %>
</span>
</li>
<% }); %>
</ul>
<ul id="access-list">
<li class="row">
<div class="col-xs-10">
<strong><%= label %> Access Percentile:</strong>
</div>
<div class="col-xs-2">
<strong><%= percentile %>%</strong>
</div>
</li>
<li class="row">
<div class="col-xs-10">
<strong><%= label %> Access Index:</strong>
</div>
<div class="col-xs-2">
<strong><%= index %></strong>
</div>
</li>
<li class="row">
<div class="col-xs-10">
Daycare Access Percentile:
</div>
<div class="col-xs-2">
<%= daycare_pct %>%
</div>
</li>
<li class="row">
<div class="col-xs-10">
Daycare Access Index:
</div>
<div class="col-xs-2">
<%= daycare_index %>
</div>
</li>
<li class="row">
<div class="col-xs-10">
Park Access Percentile:
</div>
<div class="col-xs-2">
<%= parks_pct %>%
</div>
</li>
<li class="row">
<div class="col-xs-10">
Park Access Index:
</div>
<div class="col-xs-2">
<%= parks_index %>
</div>
</li>
</ul>
</div>
</script>
<script type="text/template" id="info-template">
<hr>
<h3>Location Details</h4>
<h4><%= label %></h4>
<label><%= description %></label>
</div>
</script>
<script>
var endpoint = 'http://otp.transitanalyst.com/otp/',
basemapUrl= 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=a8014652a24b4947afef2c30e4020b6d',
blockTileBaseUrl = 'http://transitanalyst.com/tiles/';
range = {
value: 30,
min: 1,
max: 90,
step: 1
},
philly = [39.948043, -75.174170], // ~ 20th & Spruce
graphTypes = [
{type: 'bar', label: 'Bar'},
{type: 'line', label: 'Line'},
{type: 'circle', label: 'Circle'},
{type: 'table', label: 'Table'},
{type: 'density', label: 'Density'}
],
pointset = 'headstart.geo',
indicator = 'placeholder',
indicators = {},
graphType = graphTypes[0].type,
visWidth = 200,
otpaGraph = d3.otpaGraph().type(graphType).width(visWidth),
dataLabels = [
['recap_10', 'Is a racially/ethnically-concentrated area of poverty', 'RECAP'],
['promise_neighborhood', 'Is a Promise Neighborhood', 'PROMISE'],
['choice_neighborhood', 'Is a Choice Neighborhood', 'CHOICE'],
],
datasetLabels = {
'cornerstores.geo': {
'percentile': 'cornerstore_access_pct_rank',
'label': 'Cornerstore',
'index': 'cornerstore_access'
},
'headstart.geo': {
'percentile': 'headstart_access_pct_rank',
'label': 'Headstart Center',
'index': 'headstart_access'
},
'healthcare.geo': {
'percentile': 'healthcare_access_pct_rank',
'label': 'Health Clinic',
'index': 'healthcare_access'
},
'rec.geo': {
'percentile': 'rec_access_pct_rank',
'label': 'Recreation Center',
'index': 'rec_access'
},
'playgrounds.geo': {
'percentile': 'playground_access_pct_rank',
'label': 'Playground',
'index': 'playground_access'
},
};
// Use location hash to specify URL of OTPA endpoint:
// http://localhost:8080/#192.168.1.103:8080/otp/
if (location.hash) {
endpoint = 'http://' + location.hash.slice(1);
}
// From http://codepen.io/gapcode/pen/vEJNZN
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (I-E 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
/**
* Set a session cookie so that subsequent GET requests can be pinned to the same server
* as the one used to generate the TimeSurface with the ID in use.
*/
function setCookie() {
// generate a UUID: http://stackoverflow.com/a/2117523
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
document.cookie = "transitanalyst=" + uuid + "; domain=transitanalyst.com; path=/";
}
setCookie();
var map = L.map('map').setView(philly, 12);
L.tileLayer(basemapUrl, {
maxZoom: 18,
attribution: 'Maps © Thunderforest, Data © OpenStreetMap contributors'
}).addTo(map);
var LegendControl = L.Control.extend({
options: {
position: 'bottomleft'
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar');
$(container).html('<a class="legend-button" data-toggle="modal" href="#legend" href="javascript:;" title="Legend">?</a>');
$('#legend').modal('show');
return container;
}
});
map.addControl(new LegendControl());
var blockTileLayer = null;
var gridLayer = null;
var highlightedPolygon = null;
var lastHoveredBlockData = null;
var clickedBlockData = null;
var clickedPolygon = L.geoJson([], {style: {color: '#ff3300'}}).addTo(map);
var geocoder = L.Control.geocoder({
position: "topleft",
}).addTo(map);
function moveHighlightedBlock(newPoint) {
if (gridLayer) {
// Get the object data and call the click event handler,
// which will highlight the block group for that lat/lng
var object = gridLayer._objectForEvent({latlng: newPoint});
if (object.data) {
lastHoveredBlockData = object.data;
handleHighlightedClick({latlng: newPoint});
}
}
}
geocoder.markGeocode = function(result) {
map.setView(result.center, 14);
moveHighlightedBlock(result.center);
otpaLayer.setLocation(result.center);
};
function handleHighlightedClick(e) {
clickedBlockData = lastHoveredBlockData;
clickedPolygon.clearLayers();
clickedPolygon.addData(JSON.parse(clickedBlockData.geojson));
updateBlockInfo();
}
function updateBlockTiles(pointset) {
var blockTileUrl = blockTileBaseUrl + pointset + '/{z}/{x}/{y}.png';
if (blockTileLayer != null) {
map.removeLayer(blockTileLayer);
}
blockTileLayer = L.tileLayer(blockTileUrl);
map.addLayer(blockTileLayer);
if (gridLayer != null) {
map.removeLayer(gridLayer);
}
if (highlightedPolygon != null) {
map.removeLayer(highlightedPolygon);
}
highlightedPolygon = L.geoJson([], {style: {color: '#ff5050'} }).addTo(map);
highlightedPolygon.on('click', function(e) {
handleHighlightedClick(e);
otpaLayer.setLocation(e.latlng);
});
gridLayer = new L.UtfGrid(blockTileBaseUrl + pointset + '/{z}/{x}/{y}.grid.json', {useJsonP: false});
gridLayer.on('mouseover', function(e) {
lastHoveredBlockData = e.data;
var geojson = JSON.parse(e.data.geojson);
highlightedPolygon.clearLayers();
highlightedPolygon.addData(geojson);
highlightedPolygon.bringToBack();
});
gridLayer.on('click', function(e) {
otpaLayer.setLocation(e.latlng);
moveHighlightedBlock(e.latlng);
});
map.addLayer(gridLayer);
}
function updateBlockInfo() {
if (clickedBlockData) {
var data = [];
var bools = [];
var center = clickedPolygon.getBounds().getCenter();
_.each(dataLabels, function(item) {
var itemValue = clickedBlockData[item[0]];
bools.push({
key: item[0],
label: item[1],
shortLabel: item[2],
value: itemValue
});
});
var pointsetLabels = datasetLabels[pointset];
$('#block-info').html(blockTemplate({
geoid10: clickedBlockData.geoid10,
lat: center.lat,
lng: center.lng,
toggleMessage: isochroneShowing ? "Hide Travelshed" : "Show Travelshed",
bools: bools,
label: pointsetLabels.label,
index: clickedBlockData[pointsetLabels.index],
percentile: clickedBlockData[pointsetLabels.percentile],
daycare_index: clickedBlockData.daycare_access,
daycare_pct: clickedBlockData.daycare_access_pct_rank,
parks_index: clickedBlockData.park_access,
parks_pct: clickedBlockData.park_access_pct_rank
}));
}
}
updateBlockTiles(pointset);
// The range input event is broken on IE <= 11, but the change event is *also* broken in that it behaves like
// input is supposed to, so we can use change on IE and input for all the other browsers
var ie = detectIE();
var sliderEvent = ie !== false && ie <= 11 ? 'change' : 'input';
d3.select('#slider').append('input')
.attr('type', 'range')
.attr('min', range.min)
.attr('max', range.max)
.attr('step', range.step)
.attr('value', range.value)
.on(sliderEvent, function() {
var value = d3.select(this).property('value');
range.value = value;
d3.select('#minutes').html(value); // replace span with # of minutes
// update the graph immediately
update(indicators, value * 60);
// tell the layer about the change
otpaLayer.updateTime(value);
});
d3.select('#graph-type').selectAll('option')
.data(graphTypes).enter()
.append('option')
.attr('value', function(d) { return d.type; })
.html(function(d) { return d.label; });
d3.select('#graph-type').on("change", function() {
graphType = d3.event.target.value;
d3.select('#graph').selectAll('.otpa-graph').remove();
otpaGraph = d3.otpaGraph().type(graphType).width(visWidth);
update(indicators, range.value * 60);
});
d3.select('#pointsets').on("change", function() {
pointset = d3.event.target.value;
otpaLayer.setPointset(pointset);
update(indicators, range.value * 60);
updateBlockTiles(pointset);
updateBlockInfo();
});
d3.select('#indicators').on("change", function() {
indicator = d3.event.target.value;
d3.select('#graph').selectAll('.otpa-graph').remove();
update(indicators, range.value * 60);
});
var infoTemplate = _.template($("#info-template").html());
var blockTemplate = _.template($("#block-template").html());
// // TODO: make sure order of on() and addTo() does not matter
var otpaLayer = L.otpaLayer(endpoint, {
cutoffMinutes: range.max,
isochroneMinutes: range.value,
spinner: '#spinner',
pointset: pointset,
location: philly
}).on('pointsets', function(e) {
d3.select('#pointsets').selectAll('option')
.data(e.data).enter()
.append('option')
.attr('value', function(d) { return d.id; })
.html(function(d) { return datasetLabels[d.id].label + "s"; });
}).on('change', function(e) {
indicators = e.data;
var indicatorKeys;
if (indicators) {
indicatorKeys = Object.keys(indicators.data);
}
var options = d3.select('#indicators').selectAll('option')
.data(indicatorKeys, function(d) { return d; })
options.enter()
.append('option')
.attr('value', function(d) { return d; })
.html(function(d) { return d; });
options.exit().remove();
indicator = d3.select('#indicators').property('value');
d3.select('#graph').selectAll('.otpa-graph').remove();
update(indicators, range.value * 60);
}).on('select', function(e) {
var selectedIndicator = d3.select('#indicators').property('value');
$('#info').html(infoTemplate({
label: e.data.label,
description: e.data.description,
indicator: selectedIndicator,
value: e.data.structured[selectedIndicator]
}));
}).on('unselect', function(e) {
$('#info').html('');
// make sure initally displayed selection matches start value
d3.select('#pointsets').property('value', pointset);
}).on('movedlocation', function(pt) {
moveHighlightedBlock(pt);
}).addTo(map);
var isochroneShowing = false;
$('#block-info').on('click', '#toggle-travelshed', function(e) {
e.preventDefault();
if (!isochroneShowing) {
otpaLayer.showIsochrone(true);
map.removeLayer(gridLayer);
highlightedPolygon.clearLayers();
$('#isochrone-details').show();
$('#toggle-travelshed').text('Hide Travelshed');
} else {
otpaLayer.showIsochrone(false);
map.addLayer(gridLayer);
$('#isochrone-details').hide();
$('#toggle-travelshed').text('Show Travelshed');
}
isochroneShowing = !isochroneShowing;
$('#map-tab').tab('show');
});
// update() calls otpaGraph with a DOM selection as a parameter.
// otpaGraph chains the call through to a specific graph type.
var attributes = {};
function update(data, seconds) {
var attributes = {};
if (data && data.data) {
attributes['Locations accessible'] = data.data[indicator].counts;
}
d3.select('#graph').selectAll('.otpa-graph')
.data([{seconds: seconds, attributes: attributes}])
.call(otpaGraph);
}
</script>
</body>
</html>