-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhaversine.go
More file actions
30 lines (23 loc) · 790 Bytes
/
Copy pathhaversine.go
File metadata and controls
30 lines (23 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package zrange
import "math"
// Haversine returns the great circle distance between two latitude/longitude
// points in kilometers.
//
// Derived from: https://web.archive.org/web/20180528040024/https://community.esri.com/groups/coordinate-reference-systems/blog/2017/10/05/haversine-formula
//
func Haversine(lat1, lng1, lat2, lng2 float64) float64 {
phi1 := degToRad(lat1)
phi2 := degToRad(lat2)
deltaPhi := degToRad(lat2 - lat1)
deltaLambda := degToRad(lng2 - lng1)
a := hav(deltaPhi) + math.Cos(phi1)*math.Cos(phi2)*hav(deltaLambda)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
return c * earthSemiMajorAxis
}
func degToRad(x float64) float64 {
const degToRad = math.Pi / 180
return x * degToRad
}
func hav(x float64) float64 {
return math.Pow(math.Sin(x/2), 2)
}