Skip to content

Commit

Permalink
Fixed ellipsoid being used instead of a sphere in EPSG:3857 (Web Merc…
Browse files Browse the repository at this point in the history
…ator)

Fixes #7
  • Loading branch information
matafokka committed Apr 13, 2024
1 parent ed8c0aa commit 088edd3
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 106 deletions.
14 changes: 12 additions & 2 deletions EPSG/data/Overrides.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* Some parameters must be overridden or transformed in some way. This function performs all necessary transforms.
* @param tokens
* @param tokens {Record<string, string | undefined | null>} Proj4 string tokens
* @param geoKeys {Record<string, number>} Geokeys
*/
module.exports = function override(tokens) {
module.exports = function override(tokens, geoKeys) {
let proj = tokens["+proj"], a = tokens["+a"], b = tokens["+b"];

// Some geokeys should be mapped to different Proj4 parameters than specified in PCSKeys.js
Expand All @@ -17,6 +18,15 @@ module.exports = function override(tokens) {
delete tokens["+lat_0"];
}

const crsCode = geoKeys.ProjectedCRSGeoKey || geoKeys.ProjectedCSTypeGeoKey;

// Web Mercator requires a sphere.
// Original CRS defines an ellipsoid for some reason, it also should be replaced with a sphere.
// See: https://github.com/matafokka/geotiff-geokeys-to-proj4/issues/7
if (crsCode === 3857 && tokens["+a"]) {
tokens["+b"] = tokens["+a"];
}

// These projections doesn't work with spheres, proj4 requires +approx parameter in this case
if (a === b && a !== undefined && (proj === "tmerc" || proj === "utm" || proj === "etmerc"))
tokens["+approx"] = null;
Expand Down
108 changes: 54 additions & 54 deletions main-dist-iife.js

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions main-dist.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions main-dist.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ module.exports = {
tokens[kvArr[0].trim()] = kvArr[1].trim();
}

override(tokens); // Apply all necessary overrides
override(tokens, geoKeys); // Apply all necessary overrides

// Build final string

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "geotiff-geokeys-to-proj4",
"version": "2024.04.12",
"version": "2024.04.13",
"description": "A library that converts GeoTIFFs geokeys to Proj4 string",
"main": "main-dist.js",
"module": "main-dist.mjs",
Expand Down
13 changes: 12 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,18 @@ let testKeys = [
ProjLinearUnitsGeoKey: 9001,
VerticalCSTypeGeoKey: 8051,
proj4: "+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs",
}
},
{
name: "From #7",
GTModelTypeGeoKey: 1,
GTRasterTypeGeoKey: 1,
GTCitationGeoKey: "WGS 84 / Pseudo-Mercator",
GeogCitationGeoKey: "WGS 84",
GeogAngularUnitsGeoKey: 9102,
ProjectedCSTypeGeoKey: 3857,
ProjLinearUnitsGeoKey: 9001,
proj4: "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs",
}
];

// Test runner
Expand Down

0 comments on commit 088edd3

Please sign in to comment.