Skip to content

Commit

Permalink
refactor(core): Clean up DataSourceProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Sep 12, 2019
1 parent 6bf4137 commit bb32196
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
66 changes: 22 additions & 44 deletions src/Provider/DataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import GeoJsonParser from 'Parser/GeoJsonParser';
import VectorTileParser from 'Parser/VectorTileParser';
import Fetcher from 'Provider/Fetcher';
import Cache from 'Core/Scheduler/Cache';
import CancelledCommandException from 'Core/Scheduler/CancelledCommandException';

export const supportedFetchers = new Map([
['image/x-bil;bits=32', Fetcher.textureFloat],
Expand All @@ -23,16 +22,12 @@ function isValidData(data, extentDestination, validFn) {
}
}

const error = (err, url, source) => {
source.handlingError(err, url);
const error = (err, source) => {
source.handlingError(err);
throw err;
};

function convertSourceData(data, extDest, layer) {
return layer.convert(data, extDest, layer);
}

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

Expand All @@ -51,55 +46,34 @@ function parseSourceData(data, extSrc, extDest, layer) {
withAltitude: layer.isGeometryLayer,
};

data.coords = extSrc;

return parser(data, options).then((parsedFile) => {
if (source.isFileSource) {
source.parsedData = parsedFile;
if (parsedFile.extent && parsedFile.extent.crs != 'EPSG:4978') {
source.extent = parsedFile.extent;
}
}

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

function fetchSourceData(url, layer) {
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);
return fetcher(url, source.networkOptions).then((f) => {
f.coords = extSrc;
return f;
});
}

export default {
executeCommand(command) {
const promises = [];
const layer = command.layer;
const source = layer.source;
const requester = command.requester;
const extentsSource = command.extentsSource;
const extentsDestination = command.extentsDestination || extentsSource;
const parsedData = command.parsedData || [];

// TODO: Find best place to cancel Command
if (requester &&
!requester.material) {
// request has been deleted
return Promise.reject(new CancelledCommandException(command));
}

for (let i = 0, max = extentsSource.length; i < max; i++) {
const extSource = extentsSource[i];
const extDest = extentsDestination[i];

// If source, we must fetch and convert data
// URL of the resource you want to fetch
const url = source.urlFromExtent(extSource);

// Already fetched and parsed data that can be used
const validedParsedData = isValidData(parsedData[i], extDest, layer.isValidData) || source.parsedData;

// Tag to Cache data
const tag = `${source.uid}-${extSource.toString('-')}`;
Expand All @@ -109,18 +83,22 @@ export default {

// If data isn't in cache
if (!convertedSourceData) {
const extDest = extentsDestination[i];

// Already fetched and parsed data that can be used
const validedParsedData = isValidData(parsedData[i], extDest, layer.isValidData) || source.parsedData;
if (validedParsedData) {
// Convert
convertedSourceData = convertSourceData(validedParsedData, extDest, layer);
convertedSourceData = layer.convert(validedParsedData, extDest, layer);
} else if (source.fetchedData) {
// Parse and convert
convertedSourceData = parseSourceData(source.fetchedData, extSource, extDest, layer)
.then(parsedData => convertSourceData(parsedData, extDest, layer), err => error(err, url, source));
convertedSourceData = parseSourceData(source.fetchedData, extDest, layer)
.then(parsedData => layer.convert(parsedData, extDest, layer), err => error(err, source));
} else {
// Fetch, parse and convert
convertedSourceData = fetchSourceData(url, layer)
.then(fetchedData => parseSourceData(fetchedData, extSource, extDest, layer), err => error(err, url, source))
.then(parsedData => convertSourceData(parsedData, extDest, layer), err => error(err, url, source));
convertedSourceData = fetchSourceData(extSource, layer)
.then(fetchedData => parseSourceData(fetchedData, extDest, layer), err => error(err, source))
.then(parsedData => layer.convert(parsedData, extDest, layer), err => error(err, source));
}
// Put converted data in cache
Cache.set(tag, convertedSourceData, Cache.POLICIES.TEXTURE);
Expand Down
8 changes: 8 additions & 0 deletions src/Source/FileSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ class FileSource extends Source {
return this.url;
}

onParsedFile(parsedFile) {
this.parsedData = parsedFile;
if (parsedFile.extent && parsedFile.extent.crs != 'EPSG:4978') {
this.extent = parsedFile.extent;
}
return parsedFile;
}

extentInsideLimit(extent) {
// Fix me => may be not
const localExtent = this.extent.crs == extent.crs ? extent : extent.as(this.extent.crs, ext);
Expand Down
4 changes: 4 additions & 0 deletions src/Source/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class Source {
}
return true;
}

onParsedFile(parsedFile) {
return parsedFile;
}
}

export default Source;
4 changes: 2 additions & 2 deletions src/Source/WFSSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class WFSSource extends Source {
}
}

handlingError(err, url) {
handlingError(err) {
if (err.response && err.response.status == 400) {
return err.response.text().then((text) => {
const getCapUrl = `${this.url}SERVICE=WFS&REQUEST=GetCapabilities&VERSION=${this.version}`;
Expand All @@ -140,7 +140,7 @@ class WFSSource extends Source {
throw err;
});
} else {
console.error(`Source ${this.typeName}: error while trying to fetch/parse/convert WFS data. Url was ${url}.`, err);
console.error(`Source ${this.typeName}: error while trying to fetch/parse/convert WFS data.`, err);
throw err;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('Sources', function () {
// Mute console error
console.error = () => {};
const source = new WFSSource(paramsWFS);
assert.throws(() => source.handlingError(new Error('error'), 'http://test'));
assert.throws(() => source.handlingError(new Error('error')));
console.error = bce;
});
});
Expand Down

0 comments on commit bb32196

Please sign in to comment.