# Mercator map coordinates transformation formula

I have a third-party application with a city map of size `131072x131072`.

There are data on this map which contains x and y coordinates. I want to transform these coordinates to latitude and longitude. I know this map is some sort of Mercator projection but I don't know what exactly.

There is the distance coefficient:

`k=0.75990358305`

I've tried formulas from wiki (https://en.wikipedia.org/wiki/Mercator_projection) but with no success.

Can someone help to find out transformation formula?

Here are few coordinates which I know: ``` (lat,lng) - (x,y) (50.370186,30.458433) - (53655,86502) (50.404793,30.613958) - (68149,81437) (50.458076,30.604239) - (67240,73634) (50.488285,30.526358) - (59988,69221) (50.9333,29.903046) - (1920,3775) (50.114937,31.281656) - (130375,123690) ```

• Do you know the coordinates of the corners of your map? – FSimardGIS Oct 11 '18 at 21:41
• @FSimardGIS if you talking about x-y they are `(0, 0)` - `(131072, 131072)` – Slava Oct 11 '18 at 22:22
• I meant their corresponding Latitude and Longitude. – FSimardGIS Oct 12 '18 at 1:42
• @FSimardGIS `(0, 0) - (50.958968564,29.881995201)`, `(131072, 131072) - (50.092603202,31.242498527)` – Slava Oct 12 '18 at 10:35
• I tried to map the coordinates using Mercator and an affine transformation. However, the residuals are 10-20 meters and somehow the coordinate of the lower right corner does not match at all with the rest. How accurate do you expect the data to be? – FSimardGIS Oct 12 '18 at 16:28

Here is a possible methodology for referencing your map points in Latitude and Longitude. I assumed a sphere-based Mercator Projection for simplicity.

First, calculate the scale factor by dividing the distance between two points in Mercator and their distance in your map (based on your map coordinates). Ideally choose two points far away from each other.

``````a = 6378137 (Equatorial radius)
Mercator_x = a * Longitude
Mercator_y = a * ln(tan(pi / 4 + Latitude / 2)
``````

(calculate the above for both points)

``````Distance_Mercator = Sqrt((Mercator_x2 - Mercator_x1) ^ 2 + (Mercator_y2 - Mercator_y1) ^ 2)
Distance_Map = Sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
Scale_factor = Distance_Mercator / Distance_Map
``````

Then, calculate the Mercator coordinates of the origin point (0,0) from its LatLon:

``````Mercator_x0 = a * Longitude_origin
Mercator_y0 = a * ln(tan(pi / 4 + Latitude_origin / 2)
``````

Plug these values in the Mercator inverse formulas to calculate the LatLon of any point:

``````Latitude = 2 * atan(exp((Scale_factor * -y + Mercator_y0) / a)) - pi / 2
Longitude = (Scale_factor * x + Mercator_x0) / a
``````

The accuracy of the results can also depend on the quality and accuracy of the data in your application. With your example points it seems good within a few tens of meters.