Take the 2-minute tour ×

I have a Mercator projection map as a JPEG and I would like to know how to relate a given x, y coordinate to its latitude and longitude. I've looked at the Gudermannian function but I honestly don't understand how to take that function and apply it. Namely, what input is it expecting? The implementation I found (JavaScript) seems to take a range between -PI and PI, but what's the correlation between my y-value in pixels and that range?

Also, I found this function which takes a latitude and returns the tile for Google Maps, which also uses Mercator. It would seem that if I knew how to inverse this function, I'd be pretty close to having my answer.

/*<summary>Get the vertical tile number from a latitude
using Mercator projection formula</summary>*/

    private int getMercatorLatitude(double lati)
        double maxlat = Math.PI;

        double lat = lati;

        if (lat > 90) lat = lat - 180;
        if (lat < -90) lat = lat + 180;

        // conversion degre=>radians
        double phi = Math.PI * lat / 180;

        double res;
        //double temp = Math.Tan(Math.PI / 4 - phi / 2);
        //res = Math.Log(temp);
        res = 0.5 * Math.Log((1 + Math.Sin(phi)) / (1 - Math.Sin(phi)));
        double maxTileY = Math.Pow(2, zoom);
        int result = (int)(((1 - res / maxlat) / 2) * (maxTileY));

        return (result);
share|improve this question
If I remember correctly, google uses a equirectangular projection, not the mercator. –  Stefano Borini Jul 22 '09 at 15:23
Both Virtual Earth and Google use Mercator. –  Erich Mirabal Jul 22 '09 at 15:28
Also, the max useful latitude when using Mercator is not +-90 degrees - it is approximately +-85.05112878 degrees. The value is infinity at the poles, so you have to cap it off and ignore the poles. –  Erich Mirabal Jul 22 '09 at 15:35
To really complete the problem, you also need to know the zoom level when working with tiles. –  Erich Mirabal Jul 22 '09 at 15:42
I'm a little unclear on this. Do you want to do (x, y) -> (lat, long) or (lat, long) -> (x,y)? –  ntownsend Jul 30 '09 at 2:09

3 Answers 3

Here is some code for you... Let me know if you need more explanation.

    /// <summary>
    /// Calculates the Y-value (inverse Gudermannian function) for a latitude. 
    /// <para><see cref="http://en.wikipedia.org/wiki/Gudermannian_function"/></para>
    /// </summary>
    /// <param name="latitude">The latitude in degrees to use for calculating the Y-value.</param>
    /// <returns>The Y-value for the given latitude.</returns>
    public static double GudermannianInv(double latitude)
        double sign = Math.Sign(latitude);
        double sin = Math.Sin(latitude * RADIANS_PER_DEGREE * sign);
        return sign * (Math.Log((1.0 + sin) / (1.0 - sin)) / 2.0);

    /// <summary>
    /// Returns the Latitude in degrees for a given Y.
    /// </summary>
    /// <param name="y">Y is in the range of +PI to -PI.</param>
    /// <returns>Latitude in degrees.</returns>
    public static double Gudermannian(double y)
        return Math.Atan(Math.Sinh(y)) * DEGREES_PER_RADIAN;
share|improve this answer
Thank you! Trying this out right now. –  Matthew Wensing Jul 22 '09 at 15:41
So, if I have a mercator map image 1588 pixels tall, and I want to know the latitude where y = 677, I would figure out 677 in terms of +PI to -PI and call Gudermannian(y_in_terms_of_pi)? I realize that's wrong, but you can see where I am stuck mentally here ... –  Matthew Wensing Jul 22 '09 at 18:56
For example, on a 1588 pixel tall mercator map, 30.0N is 615 pixels from the top. But if I express 615 in terms of a linear range from PI (0) to -PI (1588), I get 615 -> 0.70824318. And calling the above Gudermannian(0.70824318) yields 37.5587, not 30.0. –  Matthew Wensing Jul 22 '09 at 19:14
Obviously the problem is the 'expressing 615 in terms of a linear range'. So how do I do this? –  Matthew Wensing Jul 22 '09 at 19:15
You basically have to do something like this: lat = Gudermannian(Ymax - ((y / Height) * (Ymax - Ymin))); where Ymax and Ymin are given by taking the inverse Gudermannian of +-85.05112878 (or whatever the bounds max and min latitudes of your image are) and Height is the size of your image. If you are tiling, this will also work as long as you replace the Ymax and Ymin with the tile's bounds and Height with the tile's size. Does that make sense? –  Erich Mirabal Jul 22 '09 at 19:54

Google, etc., use "spherical Mercator", the Mercator projection using a spherical Earth model rather than the slower and more complex elliptical equations.

The transformations are available as part of the OpenLayers code:


share|improve this answer

I've done something similiar. Especially if you have an image from a part of the world. A cropped map or just not a complete world map: http://stackoverflow.com/a/10401734/730823

share|improve this answer

Your Answer


