diff --git a/src/Parser/VectorTileParser.js b/src/Parser/VectorTileParser.js index 8a6369fc39..62162a9d30 100644 --- a/src/Parser/VectorTileParser.js +++ b/src/Parser/VectorTileParser.js @@ -90,6 +90,10 @@ function vtFeatureToFeatureGeometry(vtFeature, feature, classify = false) { feature.updateExtent(geometry); } +export function getStyle(styles, id, zoom) { + return styles[id].find(s => s.zoom.min <= zoom && s.zoom.max > zoom); +} + function readPBF(file, options) { const vectorTile = new VectorTile(new Protobuf(file)); const sourceLayers = Object.keys(vectorTile.layers); @@ -135,17 +139,20 @@ function readPBF(file, options) { feature = collection.requestFeatureById(layer.id, vtFeature.type - 1); feature.id = layer.id; feature.order = layer.order; + feature.style = getStyle(options.styles, feature.id, z); vtFeatureToFeatureGeometry(vtFeature, feature); } else if (!collection.features.find(f => f.id === layer.id)) { feature = collection.newFeatureByReference(feature); feature.id = layer.id; feature.order = layer.order; + feature.style = getStyle(options.styles, feature.id, z); } } } }); collection.removeEmptyFeature(); + // TODO Some vector tiles are already sorted collection.features.sort((a, b) => a.order - b.order); // TODO verify if is needed to updateExtent for previous features. collection.updateExtent(); @@ -172,6 +179,8 @@ export default { * @param {boolean} [options.withNormal=true] - If true each coordinate normal is computed * @param {boolean} [options.withAltitude=true] - If true each coordinate altitude is kept * @param {function=} options.filter - Filter function to remove features. + * @param {Object} options.Styles - Object containing subobject with + * informations on a specific style layer. Styles available is by `layer.id` and by zoom. * @param {Object} options.layers - Object containing subobject with * informations on a specific layer. Informations available is `id`, * `filterExpression`, `zoom.min` and `zoom.max`. See {@link diff --git a/src/Provider/DataSourceProvider.js b/src/Provider/DataSourceProvider.js index 1b64608a19..db047eb1da 100644 --- a/src/Provider/DataSourceProvider.js +++ b/src/Provider/DataSourceProvider.js @@ -26,6 +26,7 @@ export function parseSourceData(data, extDest, layer) { withNormal: layer.isGeometryLayer !== undefined, withAltitude: layer.isGeometryLayer !== undefined, layers: source.layers, + styles: source.styles, style: layer.style, }; diff --git a/src/Source/VectorTilesSource.js b/src/Source/VectorTilesSource.js index 26ffe9171d..41af6bdb7f 100644 --- a/src/Source/VectorTilesSource.js +++ b/src/Source/VectorTilesSource.js @@ -142,17 +142,6 @@ class VectorTilesSource extends TMSSource { } }); } - - onParsedFile(collection) { - collection.features.forEach((feature) => { - feature.style = this.getStyleFromIdZoom(feature.id, collection.extent.zoom); - }); - return collection; - } - - getStyleFromIdZoom(id, zoom) { - return this.styles[id].find(s => s.zoom.min <= zoom && s.zoom.max > zoom); - } } export default VectorTilesSource; diff --git a/test/unit/vectortiles.js b/test/unit/vectortiles.js index 5719e91c21..dceb75a38c 100644 --- a/test/unit/vectortiles.js +++ b/test/unit/vectortiles.js @@ -1,7 +1,7 @@ import fs from 'fs'; import assert from 'assert'; import HttpsProxyAgent from 'https-proxy-agent'; -import VectorTileParser from 'Parser/VectorTileParser'; +import VectorTileParser, { getStyle } from 'Parser/VectorTileParser'; import VectorTilesSource from 'Source/VectorTilesSource'; import Extent from 'Core/Geographic/Extent'; @@ -13,9 +13,8 @@ describe('Vector tiles', function () { function parse(pbf, layers) { return VectorTileParser.parse(pbf, { - crsIn: 'EPSG:4326', - crsOut: 'EPSG:3857', layers, + styles: [[]], }); } @@ -136,9 +135,9 @@ describe('Vector tiles', function () { }); source.whenReady.then(() => { assert.equal(source.styles.land.length, 2); - assert.deepEqual(source.getStyleFromIdZoom('land', 3), source.styles.land[0]); - assert.deepEqual(source.getStyleFromIdZoom('land', 5), source.styles.land[1]); - assert.deepEqual(source.getStyleFromIdZoom('land', 8), source.styles.land[1]); + assert.deepEqual(getStyle(source.styles, 'land', 3), source.styles.land[0]); + assert.deepEqual(getStyle(source.styles, 'land', 5), source.styles.land[1]); + assert.deepEqual(getStyle(source.styles, 'land', 8), source.styles.land[1]); done(); }); });