2021-11-22 18:18:41 +01:00
|
|
|
package geo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2023-09-20 16:56:38 +02:00
|
|
|
const (
|
2023-09-20 18:37:41 +02:00
|
|
|
DistLimit float64 = 5000
|
|
|
|
ScopeDistLimit float64 = 50
|
|
|
|
DefaultDist float64 = 2
|
2023-09-20 16:56:38 +02:00
|
|
|
)
|
|
|
|
|
2023-09-20 17:42:38 +02:00
|
|
|
// Deg returns the approximate distance in decimal degrees,
|
|
|
|
// see https://en.wikipedia.org/wiki/Decimal_degrees.
|
|
|
|
func Deg(km float64) float64 {
|
|
|
|
return 0.009009009 * km
|
2023-09-20 16:56:38 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:18:41 +01:00
|
|
|
// DegToRad converts a value from degrees to radians.
|
|
|
|
func DegToRad(d float64) float64 {
|
|
|
|
return d * math.Pi / 180
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
// Km returns the shortest path between two positions in km.
|
|
|
|
func Km(p, q Position) (km float64) {
|
2021-11-22 18:18:41 +01:00
|
|
|
if p.Lat == q.Lat && p.Lng == q.Lng {
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
lat1 := DegToRad(p.Lat)
|
|
|
|
lng1 := DegToRad(p.Lng)
|
|
|
|
lat2 := DegToRad(q.Lat)
|
|
|
|
lng2 := DegToRad(q.Lng)
|
|
|
|
|
|
|
|
diffLat := lat2 - lat1
|
|
|
|
diffLng := lng2 - lng1
|
|
|
|
|
|
|
|
a := math.Pow(math.Sin(diffLat/2), 2) + math.Cos(lat1)*math.Cos(lat2)*
|
|
|
|
math.Pow(math.Sin(diffLng/2), 2)
|
|
|
|
|
|
|
|
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
|
|
|
|
|
|
|
return c * EarthRadiusKm
|
|
|
|
}
|