Skip to content

Commit

Permalink
Implemented better CRS handling for "unknown" EPSG codes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jun 26, 2020
1 parent bade229 commit 8ab4a4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"ajv": "^6.12.2",
"axios": "^0.19.2",
"check-disk-space": "^2.1.0",
"epsg-index": "^1.0.0",
"fs-extra": "^8.0.1",
"nedb": "^1.8.0",
"object-hash": "^1.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/processes/anomaly.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Computes anomalies based on normals for a specific temporal frequency.",
"categories": [
"climatology",
"cube",
"cubes",
"math"
],
"parameters": [
Expand Down
32 changes: 18 additions & 14 deletions src/processgraph/datacube.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,27 @@ module.exports = class DataCube {
return coords;
}

/* Inspiration from: https://github.com/geotiffjs/cog-explorer/blob/master/src/components/mapview.jsx
const epsg = `EPSG:${code}`;
if (!proj4.defs(epsg)) {
const response = await fetch(`//epsg.io/${code}.proj4`);
proj4.defs(epsg, await response.text());
this.loadCrsDef(fromCrs);
this.loadCrsDef(toCrs);

return proj4(fromCrs, toCrs, coords);
}

loadCrsDef(crs) {
if (proj4.defs(crs)) {
return; // CRS already available
}
if (!crs.startsWith('EPSG:')) {
throw new Error("CRS " + crs + " not supported");
}
*/

// ToDo: Check this is correct; maybe find better way to convert 180/90 to Web Mercator
if (fromCrs === 'EPSG:4326' && toCrs === 'EPSG:3857' && Math.abs(coords[1]) > 85.051129) {
return [
coords[0],
Math.sign(coords[1]) * 85.051129
];
try {
let code = crs.substring(5);
const def = require('epsg-index/s/' + code + '.json');
proj4.defs(crs, def.proj4);
} catch (error) {
throw new Error("CRS " + crs + " not available for reprojection");
}

return proj4(fromCrs, toCrs, coords);
}

setSpatialExtent(extent) {
Expand Down

0 comments on commit 8ab4a4f

Please sign in to comment.