-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.go
54 lines (49 loc) · 807 Bytes
/
util.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 geohash
import (
"math"
)
func round(val float64) float64 {
if val < 0 {
return math.Ceil(val - 0.5)
}
return math.Floor(val + 0.5)
}
func roundDecimal(val float64, dec int) float64 {
factor := math.Pow10(dec)
return round(val*factor) / factor
}
func normalize(lat, lon float64) (latOut, lonOut float64) {
if lat > 90 || lat < -90 {
lat = center360(lat)
invertLon := true
switch {
case lat < -90:
lat = -180 - lat
case lat > 90:
lat = 180 - lat
default:
invertLon = false
}
if invertLon {
if lon > 0 {
lon -= 180
} else {
lon += 180
}
}
}
if lon > 180 || lon <= -180 {
lon = center360(lon)
}
return lat, lon
}
func center360(v float64) float64 {
v = math.Mod(v, 360)
if v <= 0 {
v += 360
}
if v > 180 {
v -= 360
}
return v
}