Last active
July 21, 2023 11:10
-
-
Save bertt/a9d5272f81df03fcb329d76a069cd844 to your computer and use it in GitHub Desktop.
Convert from Longitude/Latitude (4326) to 3857 (Spherical Mercator)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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