forked from openlayers/ol-mapbox-style
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
534 lines (505 loc) · 18 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
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
528
529
530
531
532
533
534
/*
ol-mapbox-style - Use Mapbox Style objects with OpenLayers
Copyright 2016-present Boundless Spatial, Inc.
License: https://raw.githubusercontent.com/boundlessgeo/ol-mapbox-gl-style/master/LICENSE
*/
import {Color} from '@mapbox/mapbox-gl-style-spec';
import mb2css from 'mapbox-to-css-font';
import applyStyleFunction, {getValue} from './stylefunction';
import googleFonts from 'webfont-matcher/lib/fonts/google';
import {fromLonLat} from 'ol/proj';
import {createXYZ} from 'ol/tilegrid';
import Map from 'ol/Map';
import GeoJSON from 'ol/format/GeoJSON';
import MVT from 'ol/format/MVT';
import {unByKey} from 'ol/Observable';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import VectorTileLayer from 'ol/layer/VectorTile';
import TileJSON from 'ol/source/TileJSON';
import VectorSource from 'ol/source/Vector';
import VectorTileSource from 'ol/source/VectorTile';
import XYZ from 'ol/source/XYZ';
var availableFonts;
function loadFont(fonts) {
var i, ii;
if (!Array.isArray(fonts)) {
var stops = fonts.stops;
if (stops) {
for (i = 0, ii = stops.length; i < ii; ++i) {
loadFont(stops[i][1]);
}
}
return;
}
var googleFamilies = googleFonts.getNames();
var families = fonts.map(function(font) {
return mb2css(font, 1).split(' 1px ')[1].replace(/"/g, '');
});
for (i = 0, ii = families.length; i < ii; ++i) {
var family = families[i];
var font = fonts[i];
if (googleFamilies.indexOf(family) !== -1) {
if (!availableFonts) {
availableFonts = [];
}
if (availableFonts.indexOf(font) == -1) {
availableFonts.push(font);
var fontUrl = 'https://fonts.googleapis.com/css?family=' + family.replace(/ /g, '+');
if (!document.querySelector('link[href="' + fontUrl + '"]')) {
var markup = document.createElement('link');
markup.href = fontUrl;
markup.rel = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(markup);
}
}
break;
}
}
}
var defaultFont = ['Open Sans Regular', 'Arial Regular'];
function preprocess(layer) {
if ('layout' in layer && 'text-field' in layer.layout) {
loadFont(layer.layout['text-font'] || defaultFont);
}
}
var spriteRegEx = /^(.*)(\?.*)$/;
function withPath(url, path) {
if (path && url.indexOf('http') != 0) {
url = path + url;
}
return url;
}
function toSpriteUrl(url, path, extension) {
url = withPath(url, path);
var parts = url.match(spriteRegEx);
return parts ?
parts[1] + extension + (parts.length > 2 ? parts[2] : '') :
url + extension;
}
/**
* Applies a style function to an `ol.layer.VectorTile` or `ol.layer.Vector`
* with an `ol.source.VectorTile` or an `ol.source.Vector`. The style function
* will render all layers from the `glStyle` object that use the specified
* `source`, or a subset of layers from the same source. The source needs to be
* a `"type": "vector"`, `"type": "geojson"` or `"type": "raster"` source.
*
* @param {ol.layer.VectorTile} layer OpenLayers layer.
* @param {string|Object} glStyle Mapbox Style object.
* @param {string} source `source` key or an array of layer `id`s from the
* Mapbox Style object. When a `source` key is provided, all layers for the
* specified source will be included in the style function. When layer `id`s
* are provided, they must be from layers that use the same source.
* @param {string} [path=undefined] Path of the style file. Only required when
* a relative path is used with the `"sprite"` property of the style.
* @param {Array<number>} [resolutions=undefined] Resolutions for mapping resolution to zoom level.
* @return {Promise} Promise which will be resolved when the style can be used
* for rendering.
*/
export function applyStyle(layer, glStyle, source, path, resolutions) {
return new Promise(function(resolve, reject) {
if (typeof glStyle != 'object') {
glStyle = JSON.parse(glStyle);
}
if (glStyle.version != 8) {
reject(new Error('glStyle version 8 required.'));
}
var spriteScale, spriteData, spriteImageUrl;
if (glStyle.sprite) {
spriteScale = window.devicePixelRatio >= 1.5 ? 0.5 : 1;
var sizeFactor = spriteScale == 0.5 ? '@2x' : '';
var spriteUrl = toSpriteUrl(glStyle.sprite, path, sizeFactor + '.json');
fetch(spriteUrl, {credentials: 'same-origin'})
.then(function(response) {
// if the response is ready return the JSON promise
if (response.status === 200) {
return response.json();
} else if (sizeFactor !== '') {
// return the JSON promise for the low-resolution sprites.
sizeFactor = '';
spriteUrl = toSpriteUrl(glStyle.sprite, path, '.json');
return fetch(spriteUrl, {credentials: 'same-origin'}).then(r => r.json());
}
})
.then(function(spritesJson) {
if (spritesJson === undefined) {
throw 'No sprites found.';
}
spriteData = spritesJson;
spriteImageUrl = toSpriteUrl(glStyle.sprite, path, sizeFactor + '.png');
onChange();
})
.catch(function(err) {
console.error(err);
reject(new Error('Sprites cannot be loaded from ' + spriteUrl));
});
}
var style;
function onChange() {
if (!style && (!glStyle.sprite || spriteData) && (!availableFonts || availableFonts.length > 0)) {
style = applyStyleFunction(layer, glStyle, source, resolutions, spriteData, spriteImageUrl, availableFonts);
resolve();
} else if (style) {
layer.setStyle(style);
}
}
if (layer instanceof VectorTileLayer || layer instanceof VectorLayer) {
try {
var layers = glStyle.layers;
for (var i = 0, ii = layers.length; i < ii; ++i) {
if (typeof source == 'string' && layers[i].source == source || source.indexOf(layers[i].id) >= 0) {
preprocess(layers[i]);
}
}
onChange();
} catch (e) {
setTimeout(function() {
reject(e);
}, 0);
}
}
});
}
function setBackground(map, layer) {
const background = {
type: layer.type
};
function updateStyle() {
var element = map.getTargetElement();
if (!element) {
return;
}
var layout = layer.layout || {};
var paint = layer.paint || {};
background['paint'] = paint;
background.id = 'olms-bg-' + paint['background-opacity'] + paint['background-color'];
var zoom = map.getView().getZoom();
if (paint['background-color'] !== undefined) {
const bg = getValue(background, 'paint', 'background-color', zoom, {});
element.style.background = Color.parse(bg).toString();
}
if (paint['background-opacity'] !== undefined) {
element.style.opacity = getValue(background, 'paint', 'background-opacity', zoom, {});
}
if (layout.visibility == 'none') {
element.style.backgroundColor = '';
element.style.opacity = '';
}
}
if (map.getTargetElement()) {
updateStyle();
}
map.on(['change:resolution', 'change:target'], updateStyle);
}
/**
* Applies properties of the Mapbox Style's first `background` layer to the map.
* @param {ol.Map} map OpenLayers Map.
* @param {Object} glStyle Mapbox Style object.
*/
export function applyBackground(map, glStyle) {
glStyle.layers.some(function(l) {
if (l.type == 'background') {
setBackground(map, l);
return true;
}
});
}
function getSourceIdByRef(layers, ref) {
var sourceId;
layers.some(function(layer) {
if (layer.id == ref) {
sourceId = layer.source;
return true;
}
});
return sourceId;
}
function processStyle(glStyle, map, baseUrl, host, path, accessToken) {
var view = map.getView();
if ('center' in glStyle && !view.getCenter()) {
view.setCenter(fromLonLat(glStyle.center));
}
if ('zoom' in glStyle && view.getZoom() === undefined) {
view.setZoom(glStyle.zoom);
}
if (!view.getCenter() || view.getZoom() === undefined) {
view.fit(view.getProjection().getExtent(), {
nearest: true,
size: map.getSize()
});
}
if (glStyle.sprite) {
if (glStyle.sprite.indexOf('mapbox://') == 0) {
glStyle.sprite = baseUrl + '/sprite' + accessToken;
} else if (glStyle.sprite.indexOf('http') != 0) {
glStyle.sprite = (host ? (host + path) : '') + glStyle.sprite + accessToken;
}
}
var glLayers = glStyle.layers;
var geoJsonFormat = new GeoJSON();
var layerIds = [];
function finalizeLayer(layer) {
if (layerIds.length > 0) {
map.addLayer(layer);
var setStyle = function() {
applyStyle(layer, glStyle, layerIds, path).then(function() {
layer.setVisible(true);
}, function(e) {
/*eslint no-console: ["error", { allow: ["error"] }] */
console.error(e);
});
};
if (layer.getSource()) {
setStyle();
} else {
layer.once('change:source', setStyle);
}
}
}
var glLayer, glSource, glSourceId, id, layer, mapid, url;
for (var i = 0, ii = glLayers.length; i < ii; ++i) {
glLayer = glLayers[i];
if (glLayer.type == 'background') {
setBackground(map, glLayer);
} else {
id = glLayer.source || getSourceIdByRef(glLayers, glLayer.ref);
if (id != glSourceId) {
finalizeLayer(layer);
layerIds = [];
glSource = glStyle.sources[id];
url = glSource.url;
var tiles = glSource.tiles;
if (url) {
if (url.indexOf('mapbox://') == 0) {
mapid = url.replace('mapbox://', '');
tiles = ['a', 'b', 'c', 'd'].map(function(host) {
return 'https://' + host + '.tiles.mapbox.com/v4/' + mapid +
'/{z}/{x}/{y}.' +
(glSource.type == 'vector' ? 'vector.pbf' : 'png') +
accessToken;
});
}
}
if (glSource.type == 'vector') {
layer = tiles ? (function() {
var tileGrid = createXYZ({
tileSize: 512,
maxZoom: 'maxzoom' in glSource ? glSource.maxzoom : 22,
minZoom: glSource.minzoom
});
return new VectorTileLayer({
declutter: true,
maxResolution: tileGrid.getMinZoom() > 0 ?
tileGrid.getResolution(tileGrid.getMinZoom()) : undefined,
source: new VectorTileSource({
attributions: glSource.attribution,
format: new MVT(),
tileGrid: tileGrid,
urls: tiles
}),
visible: false,
zIndex: i
});
})() : (function() {
var layer = new VectorTileLayer({
declutter: true,
visible: false,
zIndex: i
});
var tilejson = new TileJSON({
url: url
});
var key = tilejson.on('change', function() {
if (tilejson.getState() == 'ready') {
var tileJSONDoc = tilejson.getTileJSON();
var tiles = Array.isArray(tileJSONDoc.tiles) ? tileJSONDoc.tiles : [tileJSONDoc.tiles];
for (var i = 0, ii = tiles.length; i < ii; ++i) {
var tile = tiles[i];
if (tile.indexOf('http') != 0) {
tiles[i] = glSource.url + tile;
}
}
var tileGrid = tilejson.getTileGrid();
layer.setSource(new VectorTileSource({
attributions: tilejson.getAttributions() || tileJSONDoc.attribution,
format: new MVT(),
tileGrid: createXYZ({
minZoom: tileGrid.getMinZoom(),
maxZoom: tileGrid.getMaxZoom(),
tileSize: 512
}),
urls: tiles
}));
if (tileGrid.getMinZoom() > 0) {
layer.setMaxResolution(
tileGrid.getResolution(tileGrid.getMinZoom()));
}
unByKey(key);
}
});
return layer;
})();
} else if (glSource.type == 'raster') {
var source;
if (!glSource.tiles) {
source = (function() {
return new TileJSON({
url: url,
crossOrigin: 'anonymous'
});
})();
} else {
source = new XYZ({
attributions: glSource.attribution,
minZoom: glSource.minzoom,
maxZoom: 'maxzoom' in glSource ? glSource.maxzoom : 22,
tileSize: glSource.tileSize || 512,
url: url,
urls: glSource.tiles,
crossOrigin: 'anonymous'
});
}
source.setTileLoadFunction(function(tile, src) {
if (src.indexOf('{bbox-epsg-3857}') != -1) {
var bbox = source.getTileGrid().getTileCoordExtent(tile.getTileCoord());
src = src.replace('{bbox-epsg-3857}', bbox.toString());
}
tile.getImage().src = src;
});
layer = new TileLayer({
source: source,
visible: glLayer.layout ? glLayer.layout.visibility !== 'none' : true
});
} else if (glSource.type == 'geojson') {
var data = glSource.data;
var features, geoJsonUrl;
if (typeof data == 'string') {
geoJsonUrl = withPath(data, path);
} else {
features = geoJsonFormat.readFeatures(data, {featureProjection: 'EPSG:3857'});
}
layer = new VectorLayer({
source: new VectorSource({
attributions: glSource.attribution,
features: features,
format: geoJsonFormat,
url: geoJsonUrl
}),
visible: false,
zIndex: i
});
}
glSourceId = id;
}
layerIds.push(glLayer.id);
}
}
finalizeLayer(layer);
map.set('mapbox-style', glStyle);
}
/**
* Loads and applies a Mapbox Style object to an OpenLayers Map. This includes
* the map background, the layers, the center and the zoom.
*
* The center and zoom will only be set if present in the Mapbox Style document,
* and if not already set on the OpenLayers map.
*
* Layers will be added to the OpenLayers map, without affecting any layers that
* might already be set on the map.
*
* Layers added by `apply()` will have two additional properties:
*
* * `mapbox-source`: The `id` of the Mapbox Style document's source that the
* OpenLayers layer was created from. Usually `apply()` creates one
* OpenLayers layer per Mapbox Style source, unless the layer stack has
* layers from different sources in between.
* * `mapbox-layers`: The `id`s of the Mapbox Style document's layers that are
* included in the OpenLayers layer.
*
* The map returned by this function will have an additional `mapbox-style`
* property which holds the Mapbox Style object.
*
* @param {ol.Map|HTMLElement|string} map Either an existing OpenLayers Map
* instance, or a HTML element, or the id of a HTML element that will be the
* target of a new OpenLayers Map.
* @param {string|Object} style JSON style object or style url pointing to a
* Mapbox Style object. When using Mapbox APIs, the url must contain an access
* token and look like
* `https://api.mapbox.com/styles/v1/mapbox/bright-v9?access_token=[your_access_token_here]`.
* When passed as JSON style object, all OpenLayers layers created by `apply()`
* will be immediately available, but they may not have a source yet (i.e. when
* they are defined by a TileJSON url in the Mapbox Style document). When passed
* as style url, layers will be added to the map when the Mapbox Style document
* is loaded and parsed.
* @return {ol.Map} The OpenLayers Map instance that will be populated with the
* contents described in the Mapbox Style object.
*/
export function apply(map, style) {
var accessToken, baseUrl, host, path;
accessToken = baseUrl = host = path = '';
if (!(map instanceof Map)) {
map = new Map({
target: map
});
}
if (typeof style === 'string') {
var parts = style.match(spriteRegEx);
if (parts) {
baseUrl = parts[1];
accessToken = parts.length > 2 ? parts[2] : '';
}
fetch(style, {
credentials: 'same-origin'
})
.then(function(response) {
return response.json();
})
.then(function(glStyle) {
var a = document.createElement('A');
a.href = style;
path = a.pathname.split('/').slice(0, -1).join('/') + '/';
host = style.substr(0, style.indexOf(path));
processStyle(glStyle, map, baseUrl, host, path, accessToken);
})
.catch(function(err) {
console.error(err);
throw new Error('Could not load ' + style);
});
} else {
setTimeout(function() {
processStyle(style, map);
}, 0);
}
return map;
}
/**
* Get the OpenLayers layer instance that contains the provided Mapbox Style
* `layer`. Note that multiple Mapbox Style layers are combined in a single
* OpenLayers layer instance when they use the same Mapbox Style `source`.
* @param {ol.Map} map OpenLayers Map.
* @param {string} layerId Mapbox Style layer id.
* @return {ol.layer.Layer} layer OpenLayers layer instance.
*/
export function getLayer(map, layerId) {
const layers = map.getLayers().getArray();
for (let i = 0, ii = layers.length; i < ii; ++i) {
if (layers[i].get('mapbox-layers').indexOf(layerId) !== -1) {
return layers[i];
}
}
}
/**
* Get the OpenLayers source instance for the provided Mapbox Style `source`.
* @param {ol.Map} map OpenLayers Map.
* @param {string} sourceId Mapbox Style source id.
* @return {ol.layer.Layer} layer OpenLayers layer instance.
*/
export function getSource(map, sourceId) {
const layers = map.getLayers().getArray();
for (let i = 0, ii = layers.length; i < ii; ++i) {
const source = layers[i].getSource();
if (layers[i].get('mapbox-source').indexOf(sourceId) !== -1) {
return source;
}
}
}