Skip to content

Instantly share code, notes, and snippets.

@monkeybutter
Last active December 21, 2016 21:31
Show Gist options
  • Save monkeybutter/f6d80278b585b79540b636013a08050d to your computer and use it in GitHub Desktop.
Save monkeybutter/f6d80278b585b79540b636013a08050d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func toWGS84(xLon, yLat float64) (lon, lat float64) {
// Check if coordinate out of range for Web Mercator
// 20037508.3427892 is full extent of Web Mercator
if (math.Abs(xLon) > 20037508.3427892) || (math.Abs(yLat) > 20037508.3427892) {
return
}
semimajorAxis := 6378137.0 // WGS84 spheriod semimajor axis
lat = (1.5707963267948966 - (2.0 * math.Atan(math.Exp((-1.0 * yLat) / semimajorAxis)))) * (180/math.Pi)
lon = ((xLon / semimajorAxis) * 57.295779513082323) - ((math.Floor((((xLon / semimajorAxis) * 57.295779513082323) + 180.0) / 360.0)) * 360.0)
return
}
func toWebMercator(xLon, yLat float64) (easting, northing float64) {
// Check if coordinate out of range for Latitude/Longitude
if (math.Abs(xLon) > 180) && (math.Abs(yLat) > 90) {
return
}
semimajorAxis := 6378137.0 // WGS84 spheriod semimajor axis
east := xLon * 0.017453292519943295
north := yLat * 0.017453292519943295
northing = (semimajorAxis / 2) * math.Log((1.0 + math.Sin(north)) / (1.0 - math.Sin(north)))
easting = semimajorAxis * east
return
}
func main() {
fmt.Println(toWebMercator(-105.816001, 40.067633))
fmt.Println(toWGS84(-11779383.349100526, 4875775.395628653))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment