Skip to content

Commit

Permalink
refactor(source): supp supportedFetchers and add Fetcher.get(format)
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Mar 20, 2024
1 parent 1240db6 commit 9fa4cde
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 63 deletions.
31 changes: 31 additions & 0 deletions src/Provider/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,35 @@ export default {
return Promise.resolve(all);
});
},

get(format = '') {
const [type, subtype] = format.split('/');
switch (type) {
case 'application':
switch (subtype) {
case 'geo+json':
case 'json':
return this.json;
case 'kml':
case 'gpx':
return this.xml;
case 'x-protobuf;type=mapbox-vector':
case 'gtx':
return this.arrayBuffer;
case 'isg':
case 'gdf':
default:
return this.text;
}
case 'image':
switch (subtype) {
case 'x-bil;bits=32':
return this.textureFloat;
default:
return this.texture;
}
default:
return this.texture;
}
},
};
19 changes: 2 additions & 17 deletions src/Source/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,8 @@ import Fetcher from 'Provider/Fetcher';
import Cache from 'Core/Scheduler/Cache';
import CRS from 'Core/Geographic/Crs';

export const supportedFetchers = new Map([
['geojson', Fetcher.json],
['application/json', Fetcher.json],
['application/kml', Fetcher.xml],
['application/gpx', Fetcher.xml],
['application/x-protobuf;type=mapbox-vector', Fetcher.arrayBuffer],
['application/gtx', Fetcher.arrayBuffer],
['application/isg', Fetcher.text],
['application/gdf', Fetcher.text],
['image/x-bil;bits=32', Fetcher.textureFloat],
['image/jpeg', Fetcher.texture],
['image/png', Fetcher.texture],
[undefined, Fetcher.texture],
]);

export const supportedParsers = new Map([
['geojson', GeoJsonParser.parse],
['application/geo+json', GeoJsonParser.parse],
['application/json', GeoJsonParser.parse],
['application/kml', KMLParser.parse],
['application/gpx', GpxParser.parse],
Expand Down Expand Up @@ -134,7 +119,7 @@ class Source extends InformationsData {

this.url = source.url;
this.format = source.format;
this.fetcher = source.fetcher || supportedFetchers.get(source.format);
this.fetcher = source.fetcher || Fetcher.get(source.format);
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' };
Expand Down
19 changes: 9 additions & 10 deletions test/unit/dataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TileMesh from 'Core/TileMesh';
import Extent, { globalExtentTMS } from 'Core/Geographic/Extent';
import OBB from 'Renderer/OBB';
import DataSourceProvider from 'Provider/DataSourceProvider';
import { supportedFetchers } from 'Source/Source';
import Fetcher from 'Provider/Fetcher';
import TileProvider from 'Provider/TileProvider';
import WMTSSource from 'Source/WMTSSource';
import WMSSource from 'Source/WMSSource';
Expand All @@ -24,11 +24,6 @@ import sinon from 'sinon';

import holes from '../data/geojson/holesPoints.geojson';

const stubSupportedFetchers = new Map([
['application/json', () => Promise.resolve(JSON.parse(holes))],
['image/png', () => Promise.resolve(new THREE.Texture())],
]);

describe('Provide in Sources', function () {
// TODO We should mock the creation of all layers creation.

Expand All @@ -41,7 +36,8 @@ describe('Provide in Sources', function () {
const sizeTile = globalExtent.planarDimensions().x / 2 ** zoom;
const extent = new Extent('EPSG:3857', 0, sizeTile, 0, sizeTile);

let stub;
let stubFetcherJson;
let stubFetcherTexture;
let planarlayer;
let elevationlayer;
let colorlayer;
Expand All @@ -66,8 +62,10 @@ describe('Provide in Sources', function () {
};

before(function () {
stub = sinon.stub(supportedFetchers, 'get')
.callsFake(format => stubSupportedFetchers.get(format));
stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake(() => Promise.resolve(JSON.parse(holes)));
stubFetcherTexture = sinon.stub(Fetcher, 'texture')
.callsFake(() => Promise.resolve(new THREE.Texture()));

planarlayer = new PlanarLayer('globe', globalExtent, new THREE.Group());
colorlayer = new ColorLayer('color', { crs: 'EPSG:3857', source: false });
Expand Down Expand Up @@ -117,7 +115,8 @@ describe('Provide in Sources', function () {
});

after(function () {
stub.restore();
stubFetcherJson.restore();
stubFetcherTexture.restore();
});


Expand Down
12 changes: 5 additions & 7 deletions test/unit/demutils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from 'three';
import ElevationLayer from 'Layer/ElevationLayer';
import WMTSSource from 'Source/WMTSSource';
import { supportedFetchers } from 'Source/Source';
import Fetcher from 'Provider/Fetcher';
import assert from 'assert';
import GlobeView from 'Core/Prefab/GlobeView';
import Coordinates from 'Core/Geographic/Coordinates';
Expand All @@ -28,19 +28,17 @@ describe('DemUtils', function () {

let elevationlayer;
let context;
let stubSuppFetcher;
let stubFetcherTextFloat;
const ELEVATION = 300;

before(function () {
stubSuppFetcher = sinon.stub(supportedFetchers, 'get');
stubSuppFetcher.withArgs('image/x-bil;bits=32')
stubFetcherTextFloat = sinon.stub(Fetcher, 'textureFloat')
.callsFake(() => {
const floatArray = createBilData(ELEVATION);

const texture = new THREE.DataTexture(floatArray, 256, 256, THREE.RedFormat, THREE.FloatType);
texture.internalFormat = 'R32F';
texture.needsUpdate = false;
return () => Promise.resolve(texture);
return Promise.resolve(texture);
});

const source = new WMTSSource({
Expand All @@ -67,7 +65,7 @@ describe('DemUtils', function () {
});

after(() => {
stubSuppFetcher.restore();
stubFetcherTextFloat.restore();
});

it('add elevation layer', (done) => {
Expand Down
11 changes: 5 additions & 6 deletions test/unit/featuregeometrylayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'assert';
import GlobeView from 'Core/Prefab/GlobeView';
import FeatureGeometryLayer from 'Layer/FeatureGeometryLayer';
import FileSource from 'Source/FileSource';
import { supportedFetchers } from 'Source/Source';
import Fetcher from 'Provider/Fetcher';
import Extent from 'Core/Geographic/Extent';
import Coordinates from 'Core/Geographic/Coordinates';
import OBB from 'Renderer/OBB';
Expand All @@ -22,12 +22,11 @@ describe('Layer with Feature process', function () {
let ariegeNoProj4;
let tile;
let context;
let stubSuppFetcher;
let stubFetcherJson;

before(function () {
stubSuppFetcher = sinon.stub(supportedFetchers, 'get');
stubSuppFetcher.withArgs('application/json')
.callsFake(() => () => Promise.resolve(JSON.parse(feature)));
stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake(() => Promise.resolve(JSON.parse(feature)));

const source = new FileSource({
url: 'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements/09-ariege/departement-09-ariege.geojson',
Expand Down Expand Up @@ -75,7 +74,7 @@ describe('Layer with Feature process', function () {
});

after(function () {
stubSuppFetcher.restore();
stubFetcherJson.restore();
});

it('add layer', function (done) {
Expand Down
11 changes: 5 additions & 6 deletions test/unit/geoidlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as THREE from 'three';
import assert from 'assert';
import GeoidLayer from 'Layer/GeoidLayer';
import FileSource from 'Source/FileSource';
import { supportedFetchers } from 'Source/Source';
import Fetcher from 'Provider/Fetcher';
import Coordinates from 'Core/Geographic/Coordinates';
import GlobeView from 'Core/Prefab/GlobeView';
import Extent from 'Core/Geographic/Extent';
Expand All @@ -22,13 +22,12 @@ describe('GlobeView', function () {
let geoidLayer;
let context;
let tile;
let stubSuppFetcher;
let stubFetcherArrayBuf;

before(function () {
const buffer = createGtxBuffer(ELEVATION);
stubSuppFetcher = sinon.stub(supportedFetchers, 'get');
stubSuppFetcher.withArgs('application/gtx')
.callsFake(() => () => Promise.resolve(buffer));
stubFetcherArrayBuf = sinon.stub(Fetcher, 'arrayBuffer')
.callsFake(() => Promise.resolve(buffer));

const url = 'https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/' +
'altitude-conversion-grids/RAF20_float.gtx';
Expand Down Expand Up @@ -57,7 +56,7 @@ describe('GlobeView', function () {
});

after(function () {
stubSuppFetcher.restore();
stubFetcherArrayBuf.restore();
});

it('add geoid layer', function (done) {
Expand Down
11 changes: 5 additions & 6 deletions test/unit/source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Matrix4 } from 'three';
import assert from 'assert';
import Source, { supportedFetchers } from 'Source/Source';
import Source from 'Source/Source';
import Layer from 'Layer/Layer';
import WFSSource from 'Source/WFSSource';
import WMTSSource from 'Source/WMTSSource';
Expand Down Expand Up @@ -213,15 +213,14 @@ describe('Sources', function () {
let fetchedData;

describe('FileSource', function () {
let stubSuppFetcher;
let stubFetcherJson;
before(function () {
stubSuppFetcher = sinon.stub(supportedFetchers, 'get');
stubSuppFetcher.withArgs('application/json')
.callsFake(() => () => Promise.resolve(JSON.parse(fileSource)));
stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake(() => Promise.resolve(JSON.parse(fileSource)));
});

after(function () {
stubSuppFetcher.restore();
stubFetcherJson.restore();
});

it('should instance FileSource with no source.fetchedData', function _it(done) {
Expand Down
18 changes: 7 additions & 11 deletions test/unit/vectortiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import VectorTileParser from 'Parser/VectorTileParser';
import VectorTilesSource from 'Source/VectorTilesSource';
import Extent from 'Core/Geographic/Extent';
import urlParser from 'Parser/MapBoxUrlParser';
import { supportedFetchers } from 'Source/Source';
import Fetcher from 'Provider/Fetcher';
import sinon from 'sinon';

Expand Down Expand Up @@ -84,24 +83,21 @@ describe('Vector tiles', function () {
});

describe('VectorTilesSource', function () {
let stubFetcherjson;
let stub;
let stubFetcherJson;
let stubFetcherArrayBuf;
before(function () {
stubFetcherjson = sinon.stub(Fetcher, 'json')
stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake((url) => {
url = url.split('?')[0];
return Promise.resolve(JSON.parse(resources[url]));
});
const multipolygon = fs.readFileSync('test/data/pbf/multipolygon.pbf');
const stubSupportedFetchers = new Map([
['application/x-protobuf;type=mapbox-vector', () => Promise.resolve(multipolygon)],
]);
stub = sinon.stub(supportedFetchers, 'get')
.callsFake(format => stubSupportedFetchers.get(format));
stubFetcherArrayBuf = sinon.stub(Fetcher, 'arrayBuffer')
.callsFake(() => Promise.resolve(multipolygon));
});
after(function () {
stubFetcherjson.restore();
stub.restore();
stubFetcherJson.restore();
stubFetcherArrayBuf.restore();
});

it('throws an error because no style was provided', () => {
Expand Down

0 comments on commit 9fa4cde

Please sign in to comment.