We have an old mapping system we are needing to convert some data to and from.
We need to convert from Lng/lat to XY and from XY to Lng/Lat.
We can convert from Lng/Lat to XY Using the following:
MapWidth and MapHeight = 8192
x = (LngX + 180) * (mapWidth / 360)
y = (mapHeight / 2) - (mapWidth * Math.Log(Math.Tan((Math.PI / 4) + ((LatY * Math.PI / 180) / 2))) / (2 * Math.PI))
What we can't get right is the conversion back, we have the X right:
lng = (X - (mapWidth / 2)) / (mapWidth / 360)
But the Y is incorrect,
lat = (Math.Exp(-(Y - (mapHeight / 2)) / mapWidth * (2 * Math.PI)) - Math.Tan((Math.PI / 4)) * 2) / (Math.PI / 180)
Can't spot the issue with the final equation but reverse engineering the y = equation will work. Doing that you end up with the following: