-
Notifications
You must be signed in to change notification settings - Fork 1
/
distance.go
52 lines (40 loc) · 1.34 KB
/
distance.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
package latlong
import (
"math"
)
// R is the mean radius of Earth (6,371km)
const R = 6371300
// toRadians converts a position from degrees to radians
func toRadians(pos float64) float64 {
return (math.Pi * pos / 180)
}
// EquirectangularApproxDistance computes the distance between two points on Earth using
// EquirectangularApprox formula.
// More details: https://www.movable-type.co.uk/scripts/latlong.html
func EquirectangularApproxDistance(lat1, long1, lat2, long2 float64) float64 {
φ1 := toRadians(lat1)
φ2 := toRadians(lat2)
Δφ := toRadians(lat2 - lat1)
Δλ := toRadians(long2 - long1)
x := Δλ * math.Cos(float64(φ1+φ2)/2.0)
y := Δφ
d := R * math.Sqrt((x*x)+(y*y))
return d
}
// HaversineDistance computes the distance between two points on Earth using
// HaversineDistance formula.
// More details: https://www.movable-type.co.uk/scripts/latlong.html
func HaversineDistance(lat1, long1, lat2, long2 float64) float64 {
φ1 := toRadians(lat1)
φ2 := toRadians(lat2)
Δφ := toRadians(math.Abs(lat2 - lat1))
Δλ := toRadians(math.Abs(long2 - long1))
// a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
a := math.Sin(Δφ/2)*math.Sin(Δφ/2) +
math.Cos(φ1)*math.Cos(φ2)*
math.Sin(Δλ/2)*math.Sin(Δλ/2)
// c = 2 ⋅ asin( min(1, √a) )
c := 2 * math.Asin(math.Min(1, math.Sqrt(a)))
d := R * c
return d
}