-
Notifications
You must be signed in to change notification settings - Fork 0
/
haversine.go
54 lines (42 loc) · 1002 Bytes
/
haversine.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package haversine
import "math"
// Coord ...
type Coord struct {
Lat float64
Lon float64
}
// Metres ...
type Metres float64
const (
// Earth Radius in Metres
earthRadiusMetres = 6371000
)
// degreesToRadians ...
func degreesToRadians(degrees float64) float64 {
return degrees * math.Pi / 180
}
// toRadians ...
func (coord Coord) toRadians() Coord {
return Coord{
Lat: degreesToRadians(coord.Lat),
Lon: degreesToRadians(coord.Lon),
}
}
// Delta ...
func (origin Coord) Delta(point Coord) Coord {
return Coord{
Lat: point.Lat - origin.Lat,
Lon: point.Lon - origin.Lon,
}
}
// Distance ...
func Distance(origin, position Coord) Metres {
origin = origin.toRadians()
position = position.toRadians()
change := origin.Delta(position)
a := math.Sin(change.Lat/2)*math.Sin(change.Lat/2) +
math.Cos(origin.Lat)*math.Cos(position.Lat)*
math.Sin(change.Lon/2)*math.Sin(change.Lon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
return Metres(c * earthRadiusMetres)
}