# GIS: The math to convert from EPSG:3857 to WGS-84

During my latest project, I was given a shapefile and I was told to make a web GIS interface to handle it. I found a Node.js plugin to open the file and load the GeoJSON data onto a Google Map, but then I noticed something was odd: the coordinates were very big numbers, definitely not longitude/latitude pairs.

After installing a GIS software and a lot of googling I found out about the existence of many standards for coordinate values.

The shapefile they gave me was following the EPSG:3857 standard (also known as SR-ORG:6864) to save the points’ coordinates, but I needed the data to be WGS-84 (also known as EPSG:4326) in order to have valid longitude/latitude values. All of this without using a GIS software to convert them because everything needed to be web-based.

I studied a bit and found out after a while that there is a mathematical formula to convert the coordinates.

From here on, I will be referring to the longitude as $\lambda$ (lambda) and to the latitude as $\phi$ (phi), as I found out this to be the standard.

There is a constant used in both conversions, which I will be referring to as $b$ for bound, that equals to 20037508.34. The interval $[-b, b]$ represents the possible values of the coordinates in the EPSG:3857 (as you can read here).

The conversion goes as follows:

$\lambda_{\text{wgs}}=\lambda\frac{180}{b}\\\phi_{\text{wgs}}=\arctan(e^{\phi\frac{\pi}{b}})\frac{360}{\pi}-90$

Don’t be scared by the sight of these equations, they’re actually pretty simple. Here it is a quick JavaScript implementation that cycles the features of a GeoJSON object and changes the coordinates:

You don’t even need to use GeoJSON to use this function. In fact, it is even simpler if you’re just using a nested array because you can leave behind all the GeoJSON’s nested objects structure and happily go on with your life.