-
Notifications
You must be signed in to change notification settings - Fork 26
/
viz_dynamic.html
243 lines (207 loc) · 7.62 KB
/
viz_dynamic.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
<!DOCTYPE html>
<html>
<head>
<title>mapboxgl-jupyter viz</title>
<meta charset='UTF-8' />
<meta name='viewport'
content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script type='text/javascript'
src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script>
<link type='text/css'
href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' />
<style type='text/css'>
body { margin:0; padding:0; }
.map { position:absolute; top:0; bottom:0; width:100%; }
.legend {
background-color: white;
color: black;
border-radius: 3px;
bottom: 50px;
width: 100px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.10);
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
padding: 12px;
position: absolute;
right: 10px;
z-index: 1;
}
.legend h4 { margin: 0 0 10px; }
.legend-title {
margin: 6px;
padding: 6px:
font-weight: bold;
font-size: 14px;
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
}
.legend div span {
border-radius: 50%;
display: inline-block;
height: 10px;
margin-right: 5px;
width: 10px;
}
</style>
</head>
<body>
<div id='map' class='map'></div>
<div id='legend' class='legend'></div>
<script type='text/javascript'>
function calcCircleColorLegend(myColorStops, title) {
//Calculate a legend element on a Mapbox GL Style Spec property function stops array
var mytitle = document.createElement('div');
mytitle.textContent = title;
mytitle.className = 'legend-title'
var legend = document.getElementById('legend');
legend.appendChild(mytitle);
for (p = 0; p < myColorStops.length; p++) {
if (!!document.getElementById('legend-points-value-' + p)) {
//update the legend if it already exists
document.getElementById('legend-points-value-' + p).textContent = myColorStops[p][0];
document.getElementById('legend-points-id-' + p).style.backgroundColor = myColorStops[p][1];
} else {
//create the legend if it doesn't yet exist
var item = document.createElement('div');
var key = document.createElement('span');
key.className = 'legend-key';
var value = document.createElement('span');
key.id = 'legend-points-id-' + p;
key.style.backgroundColor = myColorStops[p][1];
value.id = 'legend-points-value-' + p;
item.appendChild(key);
item.appendChild(value);
legend.appendChild(item);
data = document.getElementById('legend-points-value-' + p)
data.textContent = myColorStops[p][0];
}
}
}
function generateInterpolateExpression(propertyValue, stops) {
var expression;
if (propertyValue == 'zoom') {
expression = ['interpolate', ['linear'], ['zoom']]
}
else if (propertyValue == 'heatmap-density') {
expression = ['interpolate', ['linear'], ['heatmap-density']]
}
else {
expression = ['interpolate', ['linear'], ['get', propertyValue]]
}
for (var i=0; i<stops.length; i++) {
expression.push(stops[i][0], stops[i][1])
}
return expression
}
function generateMatchExpression(propertyValue, stops, defaultValue) {
var expression;
expression = ['match', ['get', propertyValue]]
for (var i=0; i<stops.length; i++) {
expression.push(stops[i][0], stops[i][1])
}
expression.push(defaultValue)
return expression
}
function generatePropertyExpression(expressionType, propertyValue, stops, defaultValue) {
var expression;
if (expressionType == 'match') {
expression = generateMatchExpression(propertyValue, stops, defaultValue)
}
else {
expression = generateInterpolateExpression(propertyValue, stops)
}
return expression
}
</script>
<!-- main map creation code, extended by mapboxgl/templates/circle.html -->
<script type="text/javascript">
mapboxgl.accessToken = 'pk.eyJ1Ijoic2V2YW1vbyIsImEiOiJjamUzM2ZqdjUyaXM4MzRzNm94ZHJmZTF5In0.dH8Wmm09-Co8yDk3XdRV_A';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9?optimize=true',
center: [8.541694, 46.8],
zoom: 7,
transformRequest: (url, resourceType)=> {
if ( url.slice(0,22) == 'https://api.mapbox.com' ) {
//Add Python Plugin identifier for Mapbox API traffic
return {
url: [url.slice(0, url.indexOf("?")+1), "pluginName=PythonMapboxgl&", url.slice(url.indexOf("?")+1)].join('')
}
}
else {
//Do not transform URL for non Mapbox GET requests
return {url: url}
}
}
});
map.addControl(new mapboxgl.NavigationControl());
calcCircleColorLegend([[1540.0, 'rgb(255,255,204)'], [1815.0, 'rgb(255,237,160)'], [1931.0, 'rgb(254,217,118)'], [1969.0, 'rgb(254,178,76)'], [1985.0, 'rgb(253,141,60)'], [2000.0, 'rgb(252,78,42)'], [2011.0, 'rgb(227,26,28)'], [9999.0, 'rgb(177,0,38)']], "Year built");
map.on('style.load', function() {
map.addSource("data", {
"type": "geojson",
"data": "points1.geojson",
"buffer": 1,
"maxzoom": 14
});
map.addLayer({
"id": "label",
"source": "data",
"type": "symbol",
"maxzoom": 24,
"minzoom": 0,
"layout": {
"text-field": "{test1}",
"text-size" : generateInterpolateExpression('zoom', [[0,8],[22,16]] ),
"text-offset": [0,-1]
},
"paint": {
"text-halo-color": "white",
"text-halo-width": 1
}
}, "waterway-label" )
map.addLayer({
"id": "circle",
"source": "data",
"type": "circle",
"maxzoom": 24,
"minzoom": 0,
"paint": {
"circle-color": generatePropertyExpression("interpolate", "Year built", [[1540.0, 'rgb(255,255,204)'], [1815.0, 'rgb(255,237,160)'], [1931.0, 'rgb(254,217,118)'], [1969.0, 'rgb(254,178,76)'], [1985.0, 'rgb(253,141,60)'], [2000.0, 'rgb(252,78,42)'], [2011.0, 'rgb(227,26,28)'], [9999.0, 'rgb(177,0,38)']], "grey" ),
"circle-radius" : generatePropertyExpression('interpolate', 'zoom', [[0,1], [18,10]]),
"circle-stroke-color": "white",
"circle-stroke-width": generatePropertyExpression('interpolate', 'zoom', [[0,0.01], [18,1]]),
"circle-opacity" : 1
}
}, "label");
// Create a popup
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
// Show the popup on mouseover
map.on('mousemove', 'circle', function(e) {
map.getCanvas().style.cursor = 'pointer';
let f = e.features[0];
let popup_html = '<div><li><b>Location</b>: ' + f.geometry.coordinates[0].toPrecision(6) +
', ' + f.geometry.coordinates[1].toPrecision(6) + '</li>';
for (key in f.properties) {
popup_html += '<li><b> ' + key + '</b>: ' + f.properties[key] + ' </li>'
}
popup_html += '</div>'
popup.setLngLat(e.features[0].geometry.coordinates)
.setHTML(popup_html)
.addTo(map);
});
map.on('mouseleave', 'circle', function() {
map.getCanvas().style.cursor = '';
popup.remove();
});
// Fly to on click
map.on('click', 'circle', function(e) {
map.easeTo({
center: e.features[0].geometry.coordinates,
zoom: map.getZoom() + 1
});
});
});
</script>
</body>
</html>