From e00d00508817a0233efcb384c62537dee2805823 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 24 Nov 2021 10:53:55 +0100 Subject: [PATCH] Fix tiles and background handling --- src/ol/layer/MapboxVector.js | 22 ++++---- .../spec/ol/layer/MapboxVector.test.js | 50 +++++++++++++++++-- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src/ol/layer/MapboxVector.js b/src/ol/layer/MapboxVector.js index 7f1e417fdd..d8b59ac67f 100644 --- a/src/ol/layer/MapboxVector.js +++ b/src/ol/layer/MapboxVector.js @@ -405,10 +405,7 @@ class MapboxVectorLayer extends VectorTileLayer { } const source = this.getSource(); - if ( - styleSource.url.indexOf('mapbox://') === 0 || - styleSource.url.indexOf('{z}') !== -1 - ) { + if (styleSource.url && styleSource.url.indexOf('mapbox://') === 0) { // Tile source url, handle it directly source.setUrl( normalizeSourceUrl( @@ -433,11 +430,13 @@ class MapboxVectorLayer extends VectorTileLayer { } setupVectorSource( styleSource, - normalizeSourceUrl( - styleSource.url, - this.accessToken, - this.accessTokenParam_ - ) + styleSource.url + ? normalizeSourceUrl( + styleSource.url, + this.accessToken, + this.accessTokenParam_ + ) + : undefined ).then((source) => { applyStyle(this, style, sourceIdOrLayersList) .then(() => { @@ -470,9 +469,8 @@ class MapboxVectorLayer extends VectorTileLayer { (layer) => layer.type === 'background' ); if ( - !background || - !background.layout || - background.layout.visibility !== 'none' + background && + (!background.layout || background.layout.visibility !== 'none') ) { const style = new Style({ fill: new Fill(), diff --git a/test/browser/spec/ol/layer/MapboxVector.test.js b/test/browser/spec/ol/layer/MapboxVector.test.js index c80d2d454f..1e6a0e9d7f 100644 --- a/test/browser/spec/ol/layer/MapboxVector.test.js +++ b/test/browser/spec/ol/layer/MapboxVector.test.js @@ -141,7 +141,7 @@ describe('ol/layer/MapboxVector', () => { version: 8, sources: { 'foo': { - url: '/spec/ol/data/{z}-{x}-{y}.vector.pbf', + tiles: ['/spec/ol/data/{z}-{x}-{y}.vector.pbf'], type: 'vector', }, }, @@ -165,9 +165,6 @@ describe('ol/layer/MapboxVector', () => { source.getTile(14, 8938, 5680, 1, get('EPSG:3857')).load(); source.once('tileloadend', (event) => { const features = event.tile.getFeatures(); - if (!features) { - event.tile.setFeatures([]); - } expect(features[0].get('layer')).to.be('background'); expect( features[0].getStyleFunction()().getFill().getColor() @@ -177,6 +174,51 @@ describe('ol/layer/MapboxVector', () => { } }); }); + + it('works for styles without background', function (done) { + const layer = new MapboxVectorLayer({ + styleUrl: + 'data:,' + + encodeURIComponent( + JSON.stringify({ + version: 8, + sources: { + 'foo': { + tiles: ['/spec/ol/data/{z}-{x}-{y}.vector.pbf'], + type: 'vector', + }, + }, + layers: [ + { + id: 'landuse', + type: 'fill', + source: 'foo', + 'source-layer': 'landuse', + paint: { + 'fill-color': '#ff0000', + 'fill-opacity': 0.8, + }, + }, + ], + }) + ), + }); + const source = layer.getSource(); + const key = source.on('change', function () { + if (source.getState() === 'ready') { + unByKey(key); + source.getTile(14, 8938, 5680, 1, get('EPSG:3857')).load(); + source.once('tileloadend', (event) => { + const features = event.tile.getFeatures(); + expect(features[0].get('layer')).to.be('landuse'); + expect( + layer.getStyleFunction()(features[0])[0].getFill().getColor() + ).to.eql('rgba(255,0,0,0.8)'); + done(); + }); + } + }); + }); }); describe('Access token', function () {