-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapbox-test.html
More file actions
181 lines (162 loc) · 4.94 KB
/
mapbox-test.html
File metadata and controls
181 lines (162 loc) · 4.94 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.20.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.20.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:10%; width:90%; }
</style>
</head>
<body>
<style>
.map-overlay {
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
position: absolute;
width: 25%;
top: 0;
left: 0;
padding: 10px;
}
.map-overlay .map-overlay-inner {
background-color: #fff;
box-shadow:0 1px 2px rgba(0, 0, 0, 0.20);
border-radius: 3px;
padding: 10px;
margin-bottom: 10px;
}
.map-overlay h2 {
line-height: 24px;
display: block;
margin: 0 0 10px;
}
.map-overlay .legend .bar {
height: 10px;
display: inline-block;
}
.map-overlay input {
background-color: transparent;
display: inline-block;
width: 100%;
position: relative;
margin: 0;
cursor: ew-resize;
}
</style>
<div id='map'></div>
<div class='map-overlay top'>
<div class='map-overlay-inner'>
<h2>Speed Traveled by Time</h2>
<label id='hour'></label>
<input id='slider' type='range' min='0' max='24' step='1' value='12' />
</div>
<div class='map-overlay-inner'>
<div id='legend' class='legend'>
<div>Speed (mph)</div>
</div>
</div>
</div>
<script>
/**
* Prototyping congestion/speed data with Mapbox GL as alternative to CartoDB
*
* Reference examples:
* https://www.mapbox.com/mapbox-gl-js/example/data-driven-circle-colors/
* https://www.mapbox.com/mapbox-gl-js/example/timeline-animation/
* https://ryanbaumann.squarespace.com/blog/2016/1/23/mapbox-gl-create-data-driven-styles
*/
// public key per mapbox account
mapboxgl.accessToken = 'pk.eyJ1IjoiamVzc2ljYW1jaW5jaGFrIiwiYSI6ImNpcTB4Y2h2ODAwa3BmaG5uYW0xOHBlOGoifQ.VSD_IOgj6S1oVmrSamgZvw';
// draw map
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v9', //stylesheet location
center: [-122.4, 37.8], // starting position - longfrancisco
zoom: 8 // starting zoom
});
// draw slider and legend
var hourLabel = document.getElementById('hour');
var legend = document.getElementById('legend');
// set global vars for filtering and coloring speed data
var breaks = [25, 35, 50, 60, 61];
var colors = ['#ec7429', '#ea9e77', '#d9b305', '#9dbf88', '#62a60a'];
var layers = [];
var filters = [];
// calculate speed ranges
// logic: is speed value <= 25, > 25 and <= 35 etc or >= 61
function getSpeedRanges(breaks, param) {
filters = [];
for (var p = 0; p < breaks.length; p++) {
if (p <= 0) {
filters.push([ 'all',
[ '<=', param, breaks[p] ]
])
} else if (p < breaks.length - 1) {
filters.push([ 'all',
[ '>', param, breaks[p -1] ],
[ '<=', param, breaks[p] ]
])
} else {
filters.push([ 'all',
[ '>=', param, breaks[p] ]
])
}
}
}
// create map layer with speed ranges
function getSpeedLayers(filters, colors) {
layers = [];
for (var p = 0; p < breaks.length; p++) {
layers.push({
id: 'points-' + p,
type: 'line',
source: 'speed-merged',
paint: {
"line-color": colors[p]
},
filter: filters[p]
})
}
}
// load and process the map data
map.on('style.load', function() {
map.addSource("speed-merged", {
"type": "geojson",
// use cartodb sql api call, change default json format to geojson
"data": "https://mtc.cartodb.com:443/api/v2/sql?format=GeoJSON&q=SELECT * FROM public.speed_data_merged"
});
// add zoom and rotation controls
map.addControl(new mapboxgl.Navigation());
// add layer of all data, color by travel speed categories
map.addLayer({
"id": "speed-merged",
"type": "line",
"source": "speed-merged",
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-width": 2
// "line-color": {
// "property": "speed",
// "type": "categorical",
// // need to calculate ranges for speed categories
// "stops": [
// [filters[0], '#ec7429'],
// [filters[1], '#ea9e77'],
// [filters[2], '#d9b305'],
// [filters[3], '#9dbf88'],
// [filters[4], '#62a60a']
// ]
// }
}
});
getSpeedRanges(breaks, 'speed'); // get the ranges based on data param
getSpeedLayers(filters, colors); //
});
</script>
</body>
</html>