Skip to content

Commit

Permalink
fix(CRS): more robust parameter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jailln authored and gchoqueux committed Jun 27, 2023
1 parent 195bef3 commit a2e0f5c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/Core/Geographic/Crs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@ import proj4 from 'proj4';

proj4.defs('EPSG:4978', '+proj=geocent +datum=WGS84 +units=m +no_defs');

function isString(s) {
return typeof s === 'string' || s instanceof String;
}

function mustBeString(crs) {
if (!isString(crs)) {
throw new Error(`Crs parameter value must be a string: '${crs}'`);
}
}

function isTms(crs) {
return crs.startsWith('TMS');
return isString(crs) && crs.startsWith('TMS');
}

function isEpsg(crs) {
return crs.startsWith('EPSG');
return isString(crs) && crs.startsWith('EPSG');
}

function formatToTms(crs) {
mustBeString(crs);
return isTms(crs) ? crs : `TMS:${crs.match(/\d+/)[0]}`;
}

function formatToEPSG(crs) {
mustBeString(crs);
return isEpsg(crs) ? crs : `EPSG:${crs.match(/\d+/)[0]}`;
}

Expand All @@ -28,6 +40,7 @@ function is4326(crs) {
}

function isGeocentric(crs) {
mustBeString(crs);
const projection = proj4.defs(crs);
return !projection ? false : projection.projName == 'geocent';
}
Expand All @@ -43,6 +56,7 @@ function _unitFromProj4Unit(projunit) {
}

function toUnit(crs) {
mustBeString(crs);
switch (crs) {
case 'EPSG:4326' : return UNIT.DEGREE;
case 'EPSG:4978' : return UNIT.METER;
Expand All @@ -57,9 +71,10 @@ function toUnit(crs) {
}

function toUnitWithError(crs) {
mustBeString(crs);
const u = toUnit(crs);
if (crs === undefined || u === undefined) {
throw new Error(`Invalid crs parameter value '${crs}'`);
if (u === undefined) {
throw new Error(`No unit found for crs: '${crs}'`);
}
return u;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Source/TMSSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const extent = new Extent(CRS.tms_4326, 0, 0, 0);
* @extends Source
*
* @property {boolean} isTMSSource - Used to checkout whether this source is a
* TMSSource. Default is tue. You should not change this, as it is used
* TMSSource. Default is true. You should not change this, as it is used
* internally for optimisation.
* @property {boolean} isInverted - The isInverted property is to be set to the
* correct value, true or false (default being false) if the computation of the
Expand Down

0 comments on commit a2e0f5c

Please sign in to comment.