Skip to content

Commit

Permalink
refactor(Fetcher): supp extent in parsed file
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Feb 5, 2024
1 parent 119f24e commit daba543
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/Parser/GeoJsonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export default {

if (out.filteringExtent) {
if (typeof out.filteringExtent == 'boolean') {
out.filterExtent = json.extent.as(_in.crs);
out.filterExtent = options.extent.as(_in.crs);
} else if (out.filteringExtent.isExtent) {
out.filterExtent = out.filteringExtent;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Parser/VectorTileParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ function readPBF(file, options) {
}

// x,y,z tile coordinates
const x = file.extent.col;
const z = file.extent.zoom;
const x = options.extent.col;
const z = options.extent.zoom;
// We need to move from TMS to Google/Bing/OSM coordinates
// https://alastaira.wordpress.com/2011/07/06/converting-tms-tile-coordinates-to-googlebingosm-tile-coordinates/
// Only if the layer.origin is top
const y = options.in.isInverted ? file.extent.row : (1 << z) - file.extent.row - 1;
const y = options.in.isInverted ? options.extent.row : (1 << z) - options.extent.row - 1;

const collection = new FeatureCollection(options.out);

Expand All @@ -148,7 +148,7 @@ function readPBF(file, options) {

for (let i = sourceLayer.length - 1; i >= 0; i--) {
const vtFeature = sourceLayer.feature(i);
vtFeature.tileNumbers = { x, y: file.extent.row, z };
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);
let feature;

Expand All @@ -174,7 +174,7 @@ function readPBF(file, options) {
collection.features.sort((a, b) => a.order - b.order);
// TODO verify if is needed to updateExtent for previous features.
collection.updateExtent();
collection.extent = file.extent;
collection.extent = options.extent;
collection.isInverted = options.in.isInverted;
return Promise.resolve(collection);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/RasterTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class RasterTile extends THREE.EventDispatcher {
}

setTexture(index, texture, offsetScale) {
this.level = (texture && (index == 0)) ? texture.extent.zoom : this.level;
this.level = (texture && texture.extent && (index == 0)) ? texture.extent.zoom : this.level;
this.textures[index] = texture || null;
this.offsetScales[index] = offsetScale;
this.material.layersNeedUpdate = true;
Expand Down
19 changes: 7 additions & 12 deletions src/Source/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ class InformationsData {
// eslint-disable-next-line
class /* istanbul ignore next */ ParsingOptions {}

function fetchSourceData(source, extent) {
const url = source.urlFromExtent(extent);
return source.fetcher(url, source.networkOptions).then((f) => {
f.extent = extent;
return f;
}, err => source.handlingError(err));
}

let uid = 0;

/**
Expand Down Expand Up @@ -140,7 +132,7 @@ class Source extends InformationsData {
this.url = source.url;
this.format = source.format;
this.fetcher = source.fetcher || supportedFetchers.get(source.format) || Fetcher.texture;
this.parser = source.parser || supportedParsers.get(source.format) || (d => d);
this.parser = source.parser || supportedParsers.get(source.format) || ((d, opt) => { d.extent = opt.extent; return d; });
this.isVectorSource = (source.parser || supportedParsers.get(source.format)) != undefined;
this.networkOptions = source.networkOptions || { crossOrigin: 'anonymous' };
this.attribution = source.attribution;
Expand Down Expand Up @@ -189,9 +181,12 @@ class Source extends InformationsData {
let features = cache.getByArray(key);
if (!features) {
// otherwise fetch/parse the data
features = cache.setByArray(fetchSourceData(this, extent)
.then(file => this.parser(file, { out, in: this }),
err => this.handlingError(err)), key);
features = cache.setByArray(
this.fetcher(this.urlFromExtent(extent), this.networkOptions)
.then(file => this.parser(file, { out, in: this, extent }))
.catch(err => this.handlingError(err)),
key);

/* istanbul ignore next */
if (this.onParsedFile) {
features.then((feat) => {
Expand Down
1 change: 0 additions & 1 deletion src/Source/TMSSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class TMSSource extends Source {
this.zoom = source.zoom;

this.isInverted = source.isInverted || false;
this.url = source.url;
this.crs = CRS.formatToTms(source.crs);
this.tileMatrixSetLimits = source.tileMatrixSetLimits;
this.extentSetlimits = {};
Expand Down
42 changes: 15 additions & 27 deletions src/Source/VectorTilesSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ function toTMSUrl(url) {
return url.replace(/\{/g, '${');
}

function fetchSourceData(source, url) {
return source.fetcher(url, source.networkOptions)
.then(f => f, err => source.handlingError(err));
}

function mergeCollections(collections) {
const collection = collections[0];
collections.forEach((col, index) => {
Expand Down Expand Up @@ -70,6 +65,7 @@ class VectorTilesSource extends TMSSource {
source.url = source.url || '.';
super(source);
const ffilter = source.filter || (() => true);
this.urls = [];
this.layers = {};
this.styles = {};
let promise;
Expand Down Expand Up @@ -127,35 +123,29 @@ class VectorTilesSource extends TMSSource {
});

if (this.url == '.') {
const TMSUrlList = Object.values(style.sources).map((source) => {
if (source.url) {
const urlSource = urlParser.normalizeSourceURL(source.url, this.accessToken);
const TMSUrlList = Object.values(style.sources).map((sourceVT) => {
if (sourceVT.url) {
const urlSource = urlParser.normalizeSourceURL(sourceVT.url, this.accessToken);
return Fetcher.json(urlSource, this.networkOptions).then((tileJSON) => {
if (tileJSON.tiles[0]) {
return toTMSUrl(tileJSON.tiles[0]);
}
});
} else if (source.tiles) {
return Promise.resolve(toTMSUrl(source.tiles[0]));
} else if (sourceVT.tiles) {
return Promise.resolve(toTMSUrl(sourceVT.tiles[0]));
}
return Promise.reject();
});
return Promise.all(TMSUrlList);
}
return (Promise.resolve([this.url]));
}).then((TMSUrlList) => {
this.url = new Set(TMSUrlList);
this.urls = new Set(TMSUrlList);
});
}

urlFromExtent(extent) {
return this.url.map((url) => {
const options = {
tileMatrixCallback: this.tileMatrixCallback,
url,
};
return URLBuilder.xyz(extent, options);
});
urlFromExtent(extent, url) {
return URLBuilder.xyz(extent, { tileMatrixCallback: this.tileMatrixCallback, url });
}

onLayerAdded(options) {
Expand All @@ -176,14 +166,12 @@ class VectorTilesSource extends TMSSource {
if (!features) {
// otherwise fetch/parse the data
features = cache.setByArray(
Promise.all(this.urlFromExtent(extent).map(url =>
fetchSourceData(this, url)
.then((file) => {
file.extent = extent;
return this.parser(file, { out, in: this });
}),
)).then(collections => mergeCollections(collections),
err => this.handlingError(err)), key);
Promise.all(this.urls.map(url =>
this.fetcher(this.urlFromExtent(extent, url), this.networkOptions)
.then(file => this.parser(file, { out, in: this, extent }))))
.then(collections => mergeCollections(collections))
.catch(err => this.handlingError(err)),
key);

/* istanbul ignore next */
if (this.onParsedFile) {
Expand Down
26 changes: 12 additions & 14 deletions test/unit/dataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ describe('Provide in Sources', function () {
updateLayeredMaterialNodeImagery(context, colorlayer, tile, tile.parent);
DataSourceProvider.executeCommand(context.scheduler.commands[0])
.then((textures) => {
assert.equal(textures[0].extent.zoom, zoom);
assert.equal(textures[0].extent.row, 511);
assert.equal(textures[0].extent.col, 512);
assert.equal(textures.length, 1);
assert.equal(textures[0].isTexture, true);
done();
}).catch(done);
});
Expand Down Expand Up @@ -163,12 +162,12 @@ describe('Provide in Sources', function () {
updateLayeredMaterialNodeElevation(context, elevationlayer, tile, tile.parent);
DataSourceProvider.executeCommand(context.scheduler.commands[0])
.then((textures) => {
assert.equal(textures[0].extent.zoom, zoom);
assert.equal(textures[0].extent.row, 511);
assert.equal(textures[0].extent.col, 512);
assert.equal(textures.length, 1);
assert.equal(textures[0].isTexture, true);
done();
}).catch(done);
});

it('should get wms texture with DataSourceProvider', (done) => {
colorlayer.source = new WMSSource({
url: 'http://',
Expand All @@ -193,15 +192,12 @@ describe('Provide in Sources', function () {
updateLayeredMaterialNodeImagery(context, colorlayer, tile, tile.parent);
DataSourceProvider.executeCommand(context.scheduler.commands[0])
.then((textures) => {
const e = textures[0].extent.as(tile.extent.crs);
assert.equal(e.zoom, zoom);
assert.equal(e.west, tile.extent.west);
assert.equal(e.east, tile.extent.east);
assert.equal(e.north, tile.extent.north);
assert.equal(e.south, tile.extent.south);
assert.equal(textures.length, 1);
assert.equal(textures[0].isTexture, true);
done();
}).catch(done);
});

it('should get 4 TileMesh from TileProvider', (done) => {
const tile = new TileMesh(geom, material, planarlayer, extent, zoom);
material.visible = true;
Expand All @@ -219,6 +215,7 @@ describe('Provide in Sources', function () {
done();
}).catch(done);
});

it('should get 3 meshs with WFS source and DataSourceProvider', (done) => {
const tile = new TileMesh(geom, material, planarlayer, extent, featureLayer.zoom.min);
material.visible = true;
Expand Down Expand Up @@ -262,6 +259,7 @@ describe('Provide in Sources', function () {
done();
}).catch(done);
});

it('should get 1 texture with WFS source and DataSourceProvider', (done) => {
const tile = new TileMesh(
geom,
Expand Down Expand Up @@ -327,9 +325,9 @@ describe('Provide in Sources', function () {
.then((result) => {
tile.material.setSequence([colorlayer.id]);
tile.material.getLayer(colorlayer.id).setTextures(result, [new THREE.Vector4()]);
assert.equal(tile.material.uniforms.colorTextures.value[0].extent, undefined);
assert.equal(tile.material.uniforms.colorTextures.value[0].anisotropy, 1);
tile.material.updateLayersUniforms();
assert.equal(tile.material.uniforms.colorTextures.value[0].extent.zoom, 10);
assert.equal(tile.material.uniforms.colorTextures.value[0].anisotropy, 16);
done();
}).catch(done);
});
Expand Down
17 changes: 9 additions & 8 deletions test/unit/vectortiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Vector tiles', function () {
// this PBF file comes from https://github.com/mapbox/vector-tile-js
// it contains two square polygons
const multipolygon = fs.readFileSync('test/data/pbf/multipolygon.pbf');
multipolygon.extent = new Extent('TMS', 1, 1, 1);
const extent = new Extent('TMS', 1, 1, 1);

function parse(pbf, layers) {
return VectorTileParser.parse(pbf, {
Expand All @@ -37,6 +37,7 @@ describe('Vector tiles', function () {
out: {
crs: 'EPSG:3857',
},
extent,
});
}

Expand Down Expand Up @@ -114,9 +115,9 @@ describe('VectorTilesSource', function () {
},
});
source.whenReady.then(() => {
assert.equal(source.url.size, 1);
assert.equal(source.urls.size, 1);
// eslint-disable-next-line no-template-curly-in-string
assert.ok(source.url.has('http://server.geo/${z}/${x}/${y}.pbf'));
assert.ok(source.urls.has('http://server.geo/${z}/${x}/${y}.pbf'));
done();
})
.catch(done);
Expand Down Expand Up @@ -307,11 +308,11 @@ describe('VectorTilesSource', function () {
});
source.whenReady
.then(() => {
assert.equal(source.url.size, 2);
assert.equal(source.urls.size, 2);
// eslint-disable-next-line no-template-curly-in-string
assert.ok(source.url.has('http://server.geo/${z}/${x}/${y}.pbf'));
assert.ok(source.urls.has('http://server.geo/${z}/${x}/${y}.pbf'));
// eslint-disable-next-line no-template-curly-in-string
assert.ok(source.url.has('http://server.geo2/${z}/${x}/${y}.pbf'));
assert.ok(source.urls.has('http://server.geo2/${z}/${x}/${y}.pbf'));
done();
})
.catch(done);
Expand All @@ -334,9 +335,9 @@ describe('VectorTilesSource', function () {
});
source.whenReady
.then(() => {
assert.equal(source.url.size, 1);
assert.equal(source.urls.size, 1);
// eslint-disable-next-line no-template-curly-in-string
assert.ok(source.url.has('http://server.geo/${z}/${x}/${y}.pbf'));
assert.ok(source.urls.has('http://server.geo/${z}/${x}/${y}.pbf'));
done();
})
.catch(done);
Expand Down

0 comments on commit daba543

Please sign in to comment.