-
Notifications
You must be signed in to change notification settings - Fork 0
/
HalDist.go
30 lines (24 loc) · 1.01 KB
/
HalDist.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
package gisUtils
import "math"
// Distance function returns the distance (in meters) between two points of
// a given longitude and latitude relatively accurately (using a spherical
// approximation of the Earth) through the Haversin Distance Formula for
// great arc distance on a sphere with accuracy for small distances
//
// point coordinates are supplied in degrees and converted into rad. in the func
//
// distance returned is METERS!!!!!!
// http://en.wikipedia.org/wiki/Haversine_formula
func Distance(lat1, lon1, lat2, lon2 float64) float64 {
const r = 6378137 // Earth radius in METERS https://en.wikipedia.org/wiki/Earth_radius
// Distance between lat and longs
dLat := (lat2 - lat1) * math.Pi / 180.0
dLon := (lon2 - lon1) * math.Pi / 180.0
// convert to radians
lat1 = lat1 * math.Pi / 180.0
lat2 = lat2 * math.Pi / 180.0
// apply formulae
a := (math.Pow(math.Sin(dLat/2), 2)) + math.Pow(math.Sin(dLon/2), 2)*math.Cos(lat1)*math.Cos(lat2)
c := 2 * math.Asin(math.Sqrt(a))
return r * c
}