-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
307 lines (278 loc) · 8.69 KB
/
index.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import debounce from 'debounce';
import arrayEqual from 'array-equal';
import findAllLevels from './levels';
import LevelControl from './level_control';
import { layers } from './layers';
import loadSprite from './sprite';
class GeoJSONSource {
constructor(map, options = {}) {
const defaultOpts = { layers, geojson: {} };
const opts = { ...defaultOpts, ...options };
this.map = map;
this.geojson = opts.geojson;
this.layers = opts.layers;
this.baseSourceId = 'indoorequal';
this.sourceId = `${this.baseSourceId}_area`;
}
addSource() {
Object.keys(this.geojson).forEach((layerName) => {
this.map.addSource(`${this.baseSourceId}_${layerName}`, {
type: 'geojson',
data: this.geojson[layerName]
});
});
}
addLayers() {
const sourceLayers = Object.keys(this.geojson);
const layers = this.layers;
this.layers = layers.filter((layer) => {
return sourceLayers.includes(layer['source-layer']);
});
this.layers.forEach((layer) => {
this.map.addLayer({
source: `${this.baseSourceId}_${layer['source-layer']}`,
...layer,
'source-layer': ''
});
});
}
remove() {
this.layers.forEach((layer) => {
this.map.removeLayer(layer.id);
});
Object.keys(this.geojson).forEach((layerName) => {
this.map.removeSource(`${this.baseSourceId}_${layerName}`);
});
}
}
class VectorTileSource {
constructor(map, options = {}) {
const defaultOpts = { url: 'https://tiles.indoorequal.org/', layers };
const opts = { ...defaultOpts, ...options };
if (opts.url === defaultOpts.url && !opts.apiKey) {
throw 'You must register your apiKey at https://indoorequal.com before and set it as apiKey param.';
}
this.map = map;
this.url = opts.url;
this.apiKey = opts.apiKey;
this.layers = opts.layers;
this.sourceId = 'indoorequal';
}
addSource() {
const queryParams = this.apiKey ? `?key=${this.apiKey}` : '';
this.map.addSource(this.sourceId, {
type: 'vector',
url: `${this.url}${queryParams}`
});
}
addLayers() {
this.layers.forEach((layer) => {
this.map.addLayer({
source: this.sourceId,
...layer
});
});
}
remove() {
this.layers.forEach((layer) => {
this.map.removeLayer(layer.id);
});
this.map.removeSource(this.sourceId);
}
}
/**
* Load the indoor= source and layers in your map.
* @param {object} map the maplibre-gl instance of the map
* @param {object} options
* @param {string} [options.url] Override the default tiles URL (https://tiles.indoorequal.org/).
* @param {object} [options.geojson] GeoJSON data with with key as layer name and value with geojson features
* @param {string} [options.apiKey] The API key if you use the default tile URL (get your free key at [indoorequal.com](https://indoorequal.com)).
* @param {array} [options.layers] The layers to be used to style indoor= tiles. Take a look a the [layers.js file](https://github.com/indoorequal/maplibre-gl-indoorequal/blob/master/src/layers.js) file and the [vector schema](https://indoorequal.com/schema)
* @param {boolean} [options.heatmap] Should the heatmap layer be visible at start (true : visible, false : hidden). Defaults to true/visible.
* @property {string} level The current level displayed
* @property {array} levels The levels that can be displayed in the current view
* @fires IndoorEqual#levelschange
* @fires IndoorEqual#levelchange
* @return {IndoorEqual} `this`
*/
export default class IndoorEqual {
constructor(map, options = {}) {
const SourceKlass = options.geojson ? GeoJSONSource : VectorTileSource;
const defaultOpts = { heatmap: true };
const opts = { ...defaultOpts, ...options };
this.source = new SourceKlass(map, options);
this.map = map;
this.levels = [];
this.level = '0';
this.events = {};
if (this.map.isStyleLoaded()) {
this._init();
this.setHeatmapVisible(opts.heatmap);
} else {
this.map.once('load', () => {
this._init();
this.setHeatmapVisible(opts.heatmap);
});
}
}
/**
* Remove any layers, source and listeners and controls
*/
remove(fromRemoveEvent) {
if (!fromRemoveEvent) {
this.source.remove();
}
this._updateLevelsDebounce.clear();
this.map.off('load', this._updateLevelsDebounce);
this.map.off('data', this._updateLevelsDebounce);
this.map.off('move', this._updateLevelsDebounce);
if (this._control) {
this.onRemove();
}
}
/**
* Add an event listener
* @param {string} name the name of the event
* @param {function} fn the function to be called when the event is emitted
*/
on(name, fn) {
if (!this.events[name]) {
this.events[name] = [];
}
this.events[name].push(fn);
}
/**
* Remove an event listener.
* @param {string} name the name of the event
* @param {function} fn the same function when on() was called
*/
off(name, fn) {
if (!this.events[name]) {
this.events[name] = [];
}
this.events[name] = this.events[name].filter(cb => cb !== fn);
}
/**
* Add the level control to the map
* Used when adding the control via the map instance: map.addControl(indoorEqual)
*/
onAdd() {
this._control = new LevelControl(this);
return this._control.$el;
}
/**
* Remove the level control
* Used when removing the control via the map instance: map.removeControl(indoorEqual)
*/
onRemove() {
this._control.destroy();
this._control = null;
}
/**
* Set the displayed level.
* @param {string} level the level to be displayed
* @fires IndoorEqual#levelchange
*/
setLevel(level) {
this.level = level;
this._updateFilters();
this._emitLevelChange();
}
/**
* Set the displayed level.
* @deprecated Use setLevel instead
* @param {string} level the level to be displayed
* @fires IndoorEqual#levelchange
*/
updateLevel(level) {
console.log('The updateLevel method is deprecated. Please use setLevel instead.');
this.setLevel(level);
}
/**
* Load a sprite and add all images to the map
* @param {string} baseUrl the baseUrl where to load the sprite
* @param {object} options
* @param {boolean} [options.update] Update existing image (default false)
* @return {Promise} It resolves an hash of images.
*/
loadSprite(baseUrl, options = {}) {
const opts = { update: false, ...options };
return loadSprite(baseUrl)
.then((sprite) => {
for (const id in sprite) {
const { data, ...options } = sprite[id];
if (!this.map.hasImage(id)) {
this.map.addImage(id, data, options);
} else if (opts.update) {
this.map.updateImage(id, data);
}
}
return sprite;
});
}
/**
* Change the heatmap layer visibility
* @param {boolean} visible True to make it visible, false to hide it
*/
setHeatmapVisible(visible) {
if (this.map.getLayer('indoor-heat')) {
this.map.setLayoutProperty('indoor-heat', 'visibility', visible ? 'visible' : 'none');
}
}
_init() {
this.source.addSource();
this.source.addLayers();
this._updateFilters();
this._updateLevelsDebounce = debounce(this._updateLevels.bind(this), 1000);
this.map.on('load', this._updateLevelsDebounce);
this.map.on('data', this._updateLevelsDebounce);
this.map.on('move', this._updateLevelsDebounce);
this.map.on('remove', () => {
this.remove(true);
});
}
_updateFilters() {
this.source.layers
.filter(layer => layer.type !== 'heatmap')
.forEach((layer) => {
this.map.setFilter(layer.id, [ ...layer.filter || ['all'], ['==', 'level', this.level]]);
});
}
_refreshAfterLevelsUpdate() {
if (!this.levels.includes(this.level)) {
this.setLevel('0');
}
}
_updateLevels() {
if (this.map.isSourceLoaded(this.source.sourceId)) {
const features = this.map.querySourceFeatures(this.source.sourceId, { sourceLayer: 'area' });
const levels = findAllLevels(features);
if (!arrayEqual(levels, this.levels)) {
this.levels = levels;
this._emitLevelsChange();
this._refreshAfterLevelsUpdate();
}
}
}
_emitLevelsChange() {
this._emitEvent('levelschange', this.levels);
}
_emitLevelChange() {
this._emitEvent('levelchange', this.level);
}
_emitEvent(eventName, ...args) {
(this.events[eventName] || []).forEach(fn => fn(...args));
}
}
/**
* Emitted when the list of available levels has been updated
*
* @event IndoorEqual#levelschange
* @type {array}
*/
/**
* Emitted when the current level has been updated
*
* @event IndoorEqual#levelchange
* @type {string} always emitted when the level displayed has changed
*/