forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
58 lines (48 loc) · 1.74 KB
/
geo.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
55
56
57
58
package miniredis
import (
"fmt"
"math"
"github.com/alicebob/miniredis/v2/geohash"
)
func toGeohash(long, lat float64) uint64 {
return geohash.EncodeIntWithPrecision(lat, long, 52)
}
func fromGeohash(score uint64) (float64, float64) {
lat, long := geohash.DecodeIntWithPrecision(score, 52)
return long, lat
}
// FormatGeo format a longitude or latitude as a string, used in replies.
//
// Redis dumps the raw floating point, but we are off by a little from that
// number so we don't. The first 5 digits do match with Redis, which will do:
// https://www.xkcd.com/2170/
func formatGeo(longlat float64) string {
return fmt.Sprintf("%.5f", longlat)
}
// haversin(θ) function
func hsin(theta float64) float64 {
return math.Pow(math.Sin(theta/2), 2)
}
// 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
// Source: https://gist.github.com/cdipaolo/d3f8db3848278b49db68
func distance(lat1, lon1, lat2, lon2 float64) float64 {
// convert to radians
// must cast radius as float to multiply later
var la1, lo1, la2, lo2 float64
la1 = lat1 * math.Pi / 180
lo1 = lon1 * math.Pi / 180
la2 = lat2 * math.Pi / 180
lo2 = lon2 * math.Pi / 180
earth := 6372797.560856 // Earth radius in METERS, according to src/geohash_helper.c
// calculate
h := hsin(la2-la1) + math.Cos(la1)*math.Cos(la2)*hsin(lo2-lo1)
return 2 * earth * math.Asin(math.Sqrt(h))
}