Wgs to Mercator

199 Views Asked by At

We have this function we have been using to convert Wgs coordinates to Mercator. The goal is to have a thin split in latitude towards the poles, and a large one close to the equator to make the 3d match the imagery of the texture, all in an equirectangular projection.

Currently our function looks like this:

WgsToMercator(coord)
{
           yRadian = coord.y * Math.PI / 180.0;
           sinLat = Math.Sin(yRadian);
           y = 0.5 - Math.Log((1 + sinLat) / (1 - sinLat)) / (Math.PI * 4); /// valeur entre 0 et 1, 1 correspondant a -90 degres et 0 a 90 degres
           return y;
}

Current result is:

WgsToMercator(90) = 0;
WgsToMercator(45) = 0.36
WgsToMercator(0) = 0.5;
WgsToMercator(-45) = 0.64
WgsToMercator(-90) = 1;

Expected result would be:

WgsToMercator(90) = 0;
WgsToMercator(45) = 0.14
WgsToMercator(0) = 0.5;
WgsToMercator(-45) = 0.86
WgsToMercator(-90) = 1;

My math are rusty and can't find a way to get the expected result. Thanks a lot by advance

1

There are 1 best solutions below

1
Chuck On

I can get approximately your numbers with sine squared, after subtracting your input from 90, taking half of the result, and converting to radians.

Something like (in Octave/Matlab):

function output = WgsToMercator(coord)
  rebased_angle = 90 - coord;
  half_angle = 0.5 * rebased_angle;
  angle_radians = half_angle * (3.1415/180.0);
  output = sin(angle_radians)*sin(angle_radians);
end

Which gets:

>> WgsToMercator(90)
ans = 0
>> WgsToMercator(45)
ans =  0.14644
>> WgsToMercator(0)
ans =  0.49998
>> WgsToMercator(-45)
ans =  0.85353
>> WgsToMercator(-90)
ans =  1.00000

In C# it's:

public float WgsToMercator(float coord)
{
    var rebasedAngle = 90.0f - coord;
    var halfAngle = 0.5f * rebasedAngle;
    var angleRadians = halfAngle * (Mathf.PI / 180.0f);
    return Mathf.Sin(angleRadians) * Mathf.Sin(angleRadians);
}

:EDIT:

Here's a plot of the function I provided (in blue) and the points you've given (red circles).

plotted output