Skip to content

Instantly share code, notes, and snippets.

@bertt
Last active July 21, 2023 11:10
Show Gist options
  • Save bertt/a9d5272f81df03fcb329d76a069cd844 to your computer and use it in GitHub Desktop.
Save bertt/a9d5272f81df03fcb329d76a069cd844 to your computer and use it in GitHub Desktop.
Convert from Longitude/Latitude (4326) to 3857 (Spherical Mercator)
public static double[] ToSphericalMercatorFromWgs84(double Longitude, double Latitude)
{
var x = Longitude * 20037508.34 / 180;
var y = Math.Log(Math.Tan((90 + Latitude) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
return new double[] { x, y };
}
public static double[] ToWgs84FromSphericalMercator(double x, double y)
{
var lon = x * 180 / 20037508.34;
var lat = Math.Atan(Math.Exp(y * Math.PI / 20037508.34)) * 360 / Math.PI - 90;
return new double[] { lon, lat };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment