Fix tiles and background handling

This commit is contained in:
Andreas Hocevar
2021-11-24 10:53:55 +01:00
parent 87d37937c5
commit e00d005088
2 changed files with 56 additions and 16 deletions
+10 -12
View File
@@ -405,10 +405,7 @@ class MapboxVectorLayer extends VectorTileLayer {
} }
const source = this.getSource(); const source = this.getSource();
if ( if (styleSource.url && styleSource.url.indexOf('mapbox://') === 0) {
styleSource.url.indexOf('mapbox://') === 0 ||
styleSource.url.indexOf('{z}') !== -1
) {
// Tile source url, handle it directly // Tile source url, handle it directly
source.setUrl( source.setUrl(
normalizeSourceUrl( normalizeSourceUrl(
@@ -433,11 +430,13 @@ class MapboxVectorLayer extends VectorTileLayer {
} }
setupVectorSource( setupVectorSource(
styleSource, styleSource,
normalizeSourceUrl( styleSource.url
styleSource.url, ? normalizeSourceUrl(
this.accessToken, styleSource.url,
this.accessTokenParam_ this.accessToken,
) this.accessTokenParam_
)
: undefined
).then((source) => { ).then((source) => {
applyStyle(this, style, sourceIdOrLayersList) applyStyle(this, style, sourceIdOrLayersList)
.then(() => { .then(() => {
@@ -470,9 +469,8 @@ class MapboxVectorLayer extends VectorTileLayer {
(layer) => layer.type === 'background' (layer) => layer.type === 'background'
); );
if ( if (
!background || background &&
!background.layout || (!background.layout || background.layout.visibility !== 'none')
background.layout.visibility !== 'none'
) { ) {
const style = new Style({ const style = new Style({
fill: new Fill(), fill: new Fill(),
@@ -141,7 +141,7 @@ describe('ol/layer/MapboxVector', () => {
version: 8, version: 8,
sources: { sources: {
'foo': { 'foo': {
url: '/spec/ol/data/{z}-{x}-{y}.vector.pbf', tiles: ['/spec/ol/data/{z}-{x}-{y}.vector.pbf'],
type: 'vector', type: 'vector',
}, },
}, },
@@ -165,9 +165,6 @@ describe('ol/layer/MapboxVector', () => {
source.getTile(14, 8938, 5680, 1, get('EPSG:3857')).load(); source.getTile(14, 8938, 5680, 1, get('EPSG:3857')).load();
source.once('tileloadend', (event) => { source.once('tileloadend', (event) => {
const features = event.tile.getFeatures(); const features = event.tile.getFeatures();
if (!features) {
event.tile.setFeatures([]);
}
expect(features[0].get('layer')).to.be('background'); expect(features[0].get('layer')).to.be('background');
expect( expect(
features[0].getStyleFunction()().getFill().getColor() 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 () { describe('Access token', function () {