d3 / d3-geo

Geographic projections, spherical shapes and spherical trigonometry.

Home Page:https://d3js.org/d3-geo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mercator with rotate. Incorrect conversion from projection to geographic coordinates

dowhileforeach opened this issue · comments

I create mercator converter with rotation by lambda -50 degrees:

const geoToProj = geoMercator().rotate([-50, 0, 0]);

Direct conversion from geographic coordinates of the point [200, 0] to a projection works fine:

geoToProj([200,0]) -> [880.4166666666667, 250]

But the inverse conversion of the same projection point [880.4166666666667, 250] leads to an incorrect result:

geoToProj.invert([880.4166666666667, 250]) -> [-159.99999999999997, -1.2722218725854067e-14]

shouldn't the result of the inverse conversion be close to the point [200, 0]?

Check It. Stackblitz

The expected range of longitude is ±180, so 200 is the same as -160.

Surprisingly, proj4js has exactly the same bug:

const proj4 = require('proj4');

const proj = proj4('+proj=merc +lon_0=50 +ellps=WGS84 +datum=WGS84');

console.log(proj.forward([200,0])); // --> [16697923.618991036, 0]
console.log(proj.inverse([16697923.618991036, 0])); // --> [-160, 0]

Check It. Stackblitz

Apparently, there is some technical problem to take into account rotation by lambda when converting proToGeo...

It’s not a bug. That’s the expected behavior as I described due to the nature of spherical/cylindrical coordinates.