# Convert a point on a flat 2D web-mercator map image to a Coordinate

I have an image of a map and a point marked onto it. I have those coordinates (as a percentage) inside the image of the point. Now I'd like to know the coordinate (latitude/longitude) that this point would have.

For better understanding what I mean I made a little scribble inside a map. To my point for example is at 10% East and 30% North. Getting the longitude is easy as I just need to take 10% of 180° and I have my 18° East. Great. But as the latitude is a projected ellipsoid it's not that easy any more.

I found an other question here that pointed to this e-book here: https://pubs.usgs.gov/pp/1395/report.pdf On page 44 there should be the explanation and on page 267 a sample, but I just can't find the correct coordinates using that equation.

I would be super happy if anybody could give me pseudo code or something on how to calculate a lat/lng coordinate from my percentage coordinates.

See the Inverse transformations section of the spherical Mercator projection Wikipedia's page.

It says: `φ = 2 * tan⁻¹[exp(y/R)] - π/2`

`φ` is the latitude in radians (you can multiply by 180/π at the end, to convert to decimal degrees).
`tan⁻¹` is the inverse tangent function.
`exp` is the exponential function.
`y` is your y coordinate (0.3 in the question case).
`R` is the radius of the sphere. Your circumference (length of the Equator) is 2, because x coordinates go from -1 to 1, so your radius is 1 / π. You can simplify `y/R` as `y*π`.

Python code:

``````import math

y = 0.3

# C = 2*pi*R = 2
# R = 2/(2*pi) = 1/pi
# y/R = y*pi

# lat = 2 * arctan(exp(y/R)) - pi/2

lat = (2 * math.atan( math.exp( y * math.pi)) - math.pi / 2) * 180 / math.pi

print(lat)
``````

Prints: `47.42214099287614`

• If this Formula is correct, why does it result in `85.05112877980659` for `y = 1`? One would expect `90`. Commented Oct 9, 2023 at 17:31
• Nobody expects to see the poles in the Mercator projection, since they are projected to infinity. Commented Oct 9, 2023 at 22:08