Skip to content

Instantly share code, notes, and snippets.

@wroge
Last active February 19, 2021 04:34
Show Gist options
  • Select an option

  • Save wroge/e2160c1483a083997accf49009e7b08a to your computer and use it in GitHub Desktop.

Select an option

Save wroge/e2160c1483a083997accf49009e7b08a to your computer and use it in GitHub Desktop.
Calculate EPSG-Code
package main
import (
"fmt"
"math"
"github.com/wroge/wgs84"
)
func main() {
// Unknown coordinates
east := 3.5687498033587863e+06
north := 5.763376666923903e+06
h1 := -44.092825065366924
// WGS84 coordinates
longitude := 10.0
latitude := 52.0
h2 := 0.0
score := math.MaxFloat64
result := 0
epsg := wgs84.EPSG()
codes := epsg.CodesCover(longitude, latitude)
for _, code := range codes {
crs := epsg.Code(code)
lon, lat, h := wgs84.Transform(crs, wgs84.LonLat())(east, north, h1)
s := math.Abs(longitude-lon) + math.Abs(latitude-lat) + math.Abs(h2-h)
if s < score {
score = s
result = code
}
}
fmt.Printf("EPSG-Code: %d, Score: %v\n", result, score)
// EPSG-Code: 31467, Score: 0.0042770297423793124
}
@wroge
Copy link
Author

wroge commented Feb 19, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment