-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapbox-gl-overpass.js
290 lines (255 loc) · 7.63 KB
/
mapbox-gl-overpass.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
/**
* A [Mapbox GL JS plugin](https://www.mapbox.com/blog/build-mapbox-gl-js-plugins/) that allows you to
* query OpenStreetMap data using the Overpass API and visualize it on your Mapbox map.
* @constructor
* @param {object} options - Options to configure the plugin.
*/
var QueryOverpass = require('query-overpass');
// Mapbox Overpass
// based on github.com/mapbox/mapbox-gl-traffic/blob/master/mapbox-gl-traffic.js
function MapboxOverpass(options) {
if (!(this instanceof MapboxOverpass)) {
throw new Error('MapboxOverpass needs to be called with the new keyword');
}
this.options = Object.assign({
enabled: false,
query: null,
style: {
label: '{name}',
labelSize: 10,
color: '#ff00ed',
size: 5,
opacity: 0.5,
layers: null
},
showButton: true,
QueryOverpass: {
overpassUrl: 'https://overpass-api.de/api/interpreter',
flatProperties: true
}
}, options);
this.toggle = this.toggle.bind(this);
this.render = this.render.bind(this);
this._hide = this._hide.bind(this);
this._show = this._show.bind(this);
this._hasSource = this._hasSource.bind(this);
this._updateMap = this._updateMap.bind(this);
this._toggle = new pluginButton({show: this.options.showButton, onToggle: this.toggle.bind(this)});
}
MapboxOverpass.prototype.onAdd = function(map) {
this._map = map;
map.on('load', this.render);
map.on('moveend', this._updateMap);
return this._toggle.elem;
};
MapboxOverpass.prototype.onRemove = function() {
this._map.off('load', this.render);
var elem = this._toggle.elem;
elem.parentNode.removeChild(elem);
this._map = undefined;
};
/**
* Toggle the plugin
*/
MapboxOverpass.prototype.toggle = function() {
this.options.enabled = !this.options.enabled;
this.render();
};
/**
* Render the plugin elements
*/
MapboxOverpass.prototype.render = function() {
// Add the source and style layers if not already added
if (!this._hasSource()) {
this._map.addSource('overpass', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": []
}
});
// Compute where to insert the additional style layers
var roadLayers = this._map.getStyle().layers.filter(function(layer) {
return layer['source-layer'] === 'road';
});
var topRoadLayer = roadLayers[roadLayers.length - 1].id;
// Build the style layers for the data
if (!this.options.style.layers) {
this.options.style.layers = buildStyleLayers(this.options.style);
}
// Add the style layers
var style = this._map.getStyle();
var mapStyle = addStyleLayers(style, this.options.style.layers, topRoadLayer);
this._map.setStyle(mapStyle);
this._toggle._input.onkeypress = (e) => {
// On hitting return in the query input
if (e.key === 'Enter') {
this.options.query = this._toggle._input.value;
this._updateMap();
return true;
}
}
}
// Change plugin icon based on state
if (this.options.enabled) {
this._show();
this._toggle.setMapIcon();
} else {
this._hide();
this._toggle.setPluginIcon();
}
};
// Update the map view with the Overpass results
MapboxOverpass.prototype._updateMap = function() {
if (this.options.enabled && this.options.query) {
var bbox = this._map.getBounds();
var data;
var _this = this;
var overpassQL = this._toggle._input.value.replace(/{{bbox}}/g, [bbox._sw.lat, bbox._sw.lng, bbox._ne.lat, bbox._ne.lng].join()); // Replace {{bbox}} token with map bounds
QueryOverpass(overpassQL, function(e, geojson) {
_this._map.getSource('overpass').setData(geojson);
}, this.options.QueryOverpass);
}
}
// UI controls
// Create a button element
function button() {
var btn = document.createElement('button');
btn.className = 'mapboxgl-ctrl-icon mapboxgl-ctrl-overpass';
btn.type = 'button';
btn['aria-label'] = 'Inspect';
return btn;
}
// Create an input text box element
function textInput() {
var ti = document.createElement('input');
ti.id = 'overpass';
ti.type = 'text';
ti.placeholder = 'Overpass QL';
ti.style.display = 'none'
return ti;
}
// Plugin controls container
function container(button, input, show) {
var container = document.createElement('div');
container.className = 'mapboxgl-ctrl mapboxgl-ctrl-group';
container.appendChild(button);
container.appendChild(input);
if (!show) {
container.style.display = 'none';
}
return container;
}
// Create the plugin control
function pluginButton(options) {
options = Object.assign({
show: true,
onToggle: function() {}
}, options);
this._btn = button(); // Plugin toggle button
this._btn.onclick = options.onToggle;
this._input = textInput(); // Plugin text input
this.elem = container(this._btn, this._input, options.show);
}
pluginButton.prototype.setPluginIcon = function() {
this._btn.className = 'mapboxgl-ctrl-icon mapboxgl-ctrl-overpass';
};
pluginButton.prototype.setMapIcon = function() {
this._btn.className = 'mapboxgl-ctrl-icon mapboxgl-ctrl-map';
};
// Show layers
MapboxOverpass.prototype._show = function() {
var style = this._map.getStyle();
var source = /overpass/;
style.layers.forEach(function(layer) {
if (source.test(layer['source'])) {
layer['layout'] = layer['layout'] || {};
layer['layout']['visibility'] = 'visible';
}
});
this._map.setStyle(style);
this._toggle._input.style.display = 'inline';
};
// Hide layers that have the target source
MapboxOverpass.prototype._hide = function() {
var style = this._map.getStyle();
var source = /overpass/;
style.layers.forEach(function(layer) {
if (source.test(layer['source'])) {
layer['layout'] = layer['layout'] || {};
layer['layout']['visibility'] = 'none';
}
});
this._map.setStyle(style);
this._toggle._input.style.display = 'none';
};
// Return true if source layers has been added already on first run
MapboxOverpass.prototype._hasSource = function() {
var style = this._map.getStyle();
var source = /overpass/;
return Object.keys(style.sources).filter(function(sourceName) {
return source.test(sourceName);
}).length > 0;
};
/**
* Define layers
*/
function buildStyleLayers(options) {
var styleLayers = [
{
'id': 'overpass fill',
'type': 'fill',
'source': 'overpass',
'paint': {
'fill-color': options.color,
'fill-opacity': options.opacity
},
'filter': ["==", "$type", "Polygon"]
}, {
'id': 'overpass line',
'type': 'line',
'source': 'overpass',
'paint': {
'line-color': options.color,
'line-width': options.size,
'line-opacity': options.opacity
}
}, {
'id': 'overpass circle',
'type': 'circle',
'source': 'overpass',
'paint': {
'circle-color': options.color,
'circle-radius': options.size,
'circle-opacity': options.opacity
}
}, {
'id': 'overpass symbol',
'type': 'symbol',
'source': 'overpass',
'layout': {
'text-field': options.label,
'text-size': options.labelSize,
"text-font": [
"Open Sans Semibold", "Arial Unicode MS Bold"
],
'text-anchor': 'top'
}
}
];
return styleLayers;
}
// Add style layers to the map
function addStyleLayers(style, layers, before) {
// Replace text-field property
for (var i = 0; i < style.layers.length; i++) {
var layer = style.layers[i];
if (before === layer.id) {
var newLayers = style.layers.slice(0, i).concat(layers).concat(style.layers.slice(i));
return Object.assign({}, style, {layers: newLayers});
}
}
return style;
}
// Export plugin
module.exports = MapboxOverpass;