Web Mercator projection - p-lr/MapComposeMP GitHub Wiki

The Web Mercator projection (also known as EPSG:3857, WGS 84 Web Mercator, or Spherical Mercator) is a widely used projection for online mapping services like Google Maps, OpenStreetMap, and Bing Maps. It's a variation of the standard Mercator projection that simplifies computations by treating the Earth as a sphere for projection purposes, even though the underlying geographic coordinates are based on the WGS84 ellipsoid.

Here are the formulas to convert latitude ($\phi$) and longitude ($\lambda$) (in degrees) to Web Mercator projected coordinates ($X$ and $Y$ in meters):

Constants:

  • Earth's equatorial radius (R): This is typically taken as the semi-major axis of the WGS84 ellipsoid, which is approximately $R = 6,378,137$ meters.
  • $\pi$ (Pi): Approximately $3.14159265359$

Formulas:

  1. Convert Latitude and Longitude to Radians: First, you need to convert your latitude and longitude values from degrees to radians, as the trigonometric functions in the formulas require radian input.

    • $\phi_{rad} = \phi \times \frac{\pi}{180}$
    • $\lambda_{rad} = \lambda \times \frac{\pi}{180}$
  2. Calculate X (Easting) Coordinate: The X coordinate is directly proportional to the longitude:

    • $X = R \times \lambda_{rad}$
  3. Calculate Y (Northing) Coordinate: The Y coordinate is more complex and involves a logarithmic function:

    • $Y = R \times \ln\left(\tan\left(\frac{\pi}{4} + \frac{\phi_{rad}}{2}\right)\right)$
      • Where $\ln$ is the natural logarithm (base $e$).
      • $\tan$ is the tangent function.

Important Considerations:

  • Latitude Limit: The Mercator projection projects the poles to infinity. Therefore, Web Mercator typically cuts off at approximately $\pm 85.05112878$ degrees latitude, as beyond this, the distortion becomes extreme and the projected coordinates become very large.
  • Units: The resulting $X$ and $Y$ coordinates will be in meters.
  • Origin: The origin (0,0) of the Web Mercator coordinate system is at the intersection of the equator and the prime meridian (0° latitude, 0° longitude).
  • Coordinate System (EPSG:3857): When working with GIS software or libraries, you'll often refer to this projection by its EPSG code, which is 3857.

Example (using approximated values):

Let's say you have a point at:

  • Latitude ($\phi$) = 50 degrees North
  • Longitude ($\lambda$) = 10 degrees East
  1. Convert to radians:

    • $\phi_{rad} = 50 \times (\pi / 180) \approx 0.87266$ radians
    • $\lambda_{rad} = 10 \times (\pi / 180) \approx 0.17453$ radians
  2. Calculate X:

    • $X = 6,378,137 \times 0.17453 \approx 1,110,676.8$ meters
  3. Calculate Y:

    • $Y = 6,378,137 \times \ln(\tan(\pi/4 + 0.87266/2))$
    • $Y = 6,378,137 \times \ln(\tan(0.78539 + 0.43633))$
    • $Y = 6,378,137 \times \ln(\tan(1.22172))$
    • $Y = 6,378,137 \times \ln(2.78306)$
    • $Y = 6,378,137 \times 1.02384 \approx 6,530,950.5$ meters

So, a point at 50°N, 10°E would be approximately at $(1,110,676.8, 6,530,950.5)$ in Web Mercator coordinates.