Skip to content

Commit

Permalink
refactor(core): move handling fetcher/parser to Source class.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Sep 12, 2019
1 parent bb32196 commit ab20f4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
22 changes: 2 additions & 20 deletions src/Provider/DataSourceProvider.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import GeoJsonParser from 'Parser/GeoJsonParser';
import VectorTileParser from 'Parser/VectorTileParser';
import Fetcher from 'Provider/Fetcher';
import Cache from 'Core/Scheduler/Cache';

export const supportedFetchers = new Map([
['image/x-bil;bits=32', Fetcher.textureFloat],
['geojson', Fetcher.json],
['application/json', Fetcher.json],
['application/x-protobuf;type=mapbox-vector', Fetcher.arrayBuffer],
]);

const supportedParsers = new Map([
['geojson', GeoJsonParser.parse],
['application/json', GeoJsonParser.parse],
['application/x-protobuf;type=mapbox-vector', VectorTileParser.parse],
]);

function isValidData(data, extentDestination, validFn) {
if (data && (!validFn || validFn(data, extentDestination))) {
return data;
Expand All @@ -29,7 +13,6 @@ const error = (err, source) => {

function parseSourceData(data, extDest, layer) {
const source = layer.source;
const parser = source.parser || supportedParsers.get(source.format) || (d => Promise.resolve(d));

const options = {
buildExtent: source.isFileSource || !layer.isGeometryLayer,
Expand All @@ -46,18 +29,17 @@ function parseSourceData(data, extDest, layer) {
withAltitude: layer.isGeometryLayer,
};

return parser(data, options).then(parsedFile => source.onParsedFile(parsedFile));
return source.parser(data, options).then(parsedFile => source.onParsedFile(parsedFile));
}

function fetchSourceData(extSrc, layer) {
const source = layer.source;
const fetcher = source.fetcher || supportedFetchers.get(source.format) || Fetcher.texture;
// If source, we must fetch and convert data
// URL of the resource you want to fetch
const url = source.urlFromExtent(extSrc);

// Fetch data
return fetcher(url, source.networkOptions).then((f) => {
return source.fetcher(url, source.networkOptions).then((f) => {
f.coords = extSrc;
return f;
});
Expand Down
26 changes: 24 additions & 2 deletions src/Source/Source.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import Extent from 'Core/Geographic/Extent';
import GeoJsonParser from 'Parser/GeoJsonParser';
import KMLParser from 'Parser/KMLParser';
import GpxParser from 'Parser/GpxParser';
import VectorTileParser from 'Parser/VectorTileParser';
import Fetcher from 'Provider/Fetcher';

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

const supportedParsers = new Map([
['geojson', GeoJsonParser.parse],
['application/json', GeoJsonParser.parse],
['application/kml', KMLParser.parse],
['application/gpx', GpxParser.parse],
['application/x-protobuf;type=mapbox-vector', VectorTileParser.parse],
]);

let uid = 0;

Expand Down Expand Up @@ -82,8 +104,8 @@ class Source {

this.url = source.url;
this.format = source.format;
this.fetcher = source.fetcher;
this.parser = source.parser;
this.fetcher = source.fetcher || supportedFetchers.get(source.format) || Fetcher.texture;
this.parser = source.parser || supportedParsers.get(source.format) || (d => Promise.resolve(d));
this.networkOptions = source.networkOptions || { crossOrigin: 'anonymous' };
this.projection = source.projection;
this.attribution = source.attribution;
Expand Down
3 changes: 2 additions & 1 deletion test/unit/dataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import FeatureProcessing from 'Process/FeatureProcessing';
import TileMesh from 'Core/TileMesh';
import Extent, { globalExtentTMS } from 'Core/Geographic/Extent';
import OBB from 'Renderer/OBB';
import DataSourceProvider, { supportedFetchers } from 'Provider/DataSourceProvider';
import DataSourceProvider from 'Provider/DataSourceProvider';
import { supportedFetchers } from 'Source/Source';
import TileProvider from 'Provider/TileProvider';
import WMTSSource from 'Source/WMTSSource';
import WMSSource from 'Source/WMSSource';
Expand Down

0 comments on commit ab20f4c

Please sign in to comment.