-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompress.go
More file actions
62 lines (50 loc) · 1.39 KB
/
Copy pathcompress.go
File metadata and controls
62 lines (50 loc) · 1.39 KB
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
59
60
61
62
package maidenhead
// http://www.w8bh.net/grid_squares.pdf
import (
"errors"
"math"
"strings"
)
// Locator returns the Maidenhead locator for the given latitude and longitude and requested precision.
func Locator(lat, lng float64, precision int) (string, error) {
if math.Abs(lat) >= 90 {
return "", errors.New("invalid latitude: allowed values are between -90 and 90")
}
if math.Abs(lng) > 180 {
return "", errors.New("invalid longitude: allowed values are between -180 and 180")
}
if precision <= 0 {
return "", errors.New("precision must be greater than zero")
}
if precision%2 != 0 {
return "", errors.New("precision must be even")
}
if precision/2 > maxSteps {
return "", errors.New("precision must be less or equal to 10")
}
lat += latSouthPole
lng += lngEastwardGreenwich
var buf strings.Builder
buf.Grow(precision)
steps := precision / 2
for step := 0; step < steps; step++ {
latIdx := int(math.Trunc(lat / latDivider[step]))
lngIdx := int(math.Trunc(lng / lngDivider[step]))
if step%2 == 0 {
lngChar := alphabet[lngIdx]
latChar := alphabet[latIdx]
if step == 2 {
lngChar |= 0x20
latChar |= 0x20
}
buf.WriteByte(lngChar)
buf.WriteByte(latChar)
} else {
buf.WriteByte(digits[lngIdx])
buf.WriteByte(digits[latIdx])
}
lat -= float64(latIdx) * latDivider[step]
lng -= float64(lngIdx) * lngDivider[step]
}
return buf.String(), nil
}