Skip to content

Commit

Permalink
refactor(Vector Tiles): set style in vector tiles parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Jul 2, 2020
1 parent 532a687 commit cd0756f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/Parser/VectorTileParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Provider/DataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
11 changes: 0 additions & 11 deletions src/Source/VectorTilesSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 5 additions & 6 deletions test/unit/vectortiles.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,9 +13,8 @@ describe('Vector tiles', function () {

function parse(pbf, layers) {
return VectorTileParser.parse(pbf, {
crsIn: 'EPSG:4326',
crsOut: 'EPSG:3857',
layers,
styles: [[]],
});
}

Expand Down Expand Up @@ -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();
});
});
Expand Down

0 comments on commit cd0756f

Please sign in to comment.