Skip to content

Commit

Permalink
refactor(VectorTileParser): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Nov 4, 2024
1 parent 518f149 commit 3b370a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
30 changes: 15 additions & 15 deletions src/Parser/VectorTileParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ function vtFeatureToFeatureGeometry(vtFeature, feature, classify = false) {
function readPBF(file, options) {
options.out = options.out || {};
const vectorTile = new VectorTile(new Protobuf(file));
const sourceLayers = Object.keys(vectorTile.layers);
const vtLayerNames = Object.keys(vectorTile.layers);

if (sourceLayers.length < 1) {
return;
const collection = new FeatureCollection(options.out);
if (vtLayerNames.length < 1) {
return Promise.resolve(collection);
}

// x,y,z tile coordinates
Expand All @@ -130,26 +131,25 @@ function readPBF(file, options) {
// Only if the layer.origin is top
const y = options.in.isInverted ? options.extent.row : (1 << z) - options.extent.row - 1;

const collection = new FeatureCollection(options.out);

const vFeature = vectorTile.layers[sourceLayers[0]];
// TODO: verify if size is correct because is computed with only one feature (vFeature).
const size = vFeature.extent * 2 ** z;
const vFeature0 = vectorTile.layers[vtLayerNames[0]];
// TODO: verify if size is correct because is computed with only one feature (vFeature0).
const size = vFeature0.extent * 2 ** z;
const center = -0.5 * size;

collection.scale.set(globalExtent.x / size, -globalExtent.y / size, 1);
collection.position.set(vFeature.extent * x + center, vFeature.extent * y + center, 0).multiply(collection.scale);
collection.position.set(vFeature0.extent * x + center, vFeature0.extent * y + center, 0).multiply(collection.scale);
collection.updateMatrixWorld();

sourceLayers.forEach((layer_id) => {
if (!options.in.layers[layer_id]) { return; }
vtLayerNames.forEach((vtLayerName) => {
if (!options.in.layers[vtLayerName]) { return Promise.resolve(collection); }

const sourceLayer = vectorTile.layers[layer_id];
const vectorTileLayer = vectorTile.layers[vtLayerName];

for (let i = sourceLayer.length - 1; i >= 0; i--) {
const vtFeature = sourceLayer.feature(i);
for (let i = vectorTileLayer.length - 1; i >= 0; i--) {
const vtFeature = vectorTileLayer.feature(i);
vtFeature.tileNumbers = { x, y: options.extent.row, z };
const layers = options.in.layers[layer_id].filter(l => l.filterExpression.filter({ zoom: z }, vtFeature) && z >= l.zoom.min && z < l.zoom.max);
const layers = options.in.layers[vtLayerName]
.filter(l => l.filterExpression.filter({ zoom: z }, vtFeature) && z >= l.zoom.min && z < l.zoom.max);
let feature;

for (const layer of layers) {
Expand Down
6 changes: 4 additions & 2 deletions test/unit/vectortiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ describe('Vector tiles', function () {
}).catch(done);
});

it('returns nothing', (done) => {
it('returns an empty collection', (done) => {
parse(null).then((collection) => {
assert.equal(collection, undefined);
assert.ok(collection.isFeatureCollection);
assert.equal(collection.features.length, 0);
done();
}).catch(done);
});

it('filters all features out', (done) => {
parse(multipolygon, {}).then((collection) => {
assert.ok(collection.isFeatureCollection);
assert.equal(collection.features.length, 0);
done();
}).catch(done);
Expand Down

0 comments on commit 3b370a5

Please sign in to comment.