-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.go
181 lines (159 loc) · 3.41 KB
/
grid.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gridlocator
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
)
// Convert converts the specified decimanl longitude and latitude into the six
// digit Maidenhead grid locator.
func Convert(latitude, longitude float64) (string, error) {
lat := latitude + 90
lng := longitude + 180
// Field
lat = (lat / 10) // + 0.0000001;
lng = (lng / 20) // + 0.0000001;
val, err := upperN2L(int(math.Floor(lng)))
if err != nil {
return "", fmt.Errorf("field longitude: %w", err)
}
locator := val
val, err = upperN2L(int(math.Floor(lat)))
if err != nil {
return "", fmt.Errorf("field latitude: %w", err)
}
locator += val
// Square
lat = 10 * (lat - math.Floor(lat))
lng = 10 * (lng - math.Floor(lng))
locator += strconv.Itoa(int(math.Floor(lng)))
locator += strconv.Itoa(int(math.Floor(lat)))
// Subsquare
lat = 24 * (lat - math.Floor(lat))
lng = 24 * (lng - math.Floor(lng))
val, err = n2l(int(math.Floor(lng)))
if err != nil {
return "", fmt.Errorf("subsquare longitude: %w", err)
}
locator += val
val, err = n2l(int(math.Floor(lat)))
if err != nil {
return "", fmt.Errorf("subsquare latitude: %w", err)
}
locator += val
return locator, nil
}
// ConvertGridLocation converts a string grid location into latitude and longitude.
func ConvertGridLocation(location string) (float64, float64, error) {
if len(location) != 4 && len(location) != 6 {
return 0, 0, errors.New("grid location must be either 4 or 6 digits")
}
location = strings.ToLower(location)
//lng = (($l[0] * 20) + ($l[2] * 2) + ($l[4]/12) - 180);
l := make([]int, 6)
// Field
var err error
l[0], err = l2n(string(location[0]))
if err != nil {
return 0, 0, fmt.Errorf("longitude field value: %w", err)
}
l[1], err = l2n(string(location[1]))
if err != nil {
return 0, 0, fmt.Errorf("latitude field value: %w", err)
}
// Square
val, err := strconv.ParseInt(string(location[2]), 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("longitude sqare value: %w", err)
}
l[2] = int(val)
val, err = strconv.ParseInt(string(location[3]), 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("latitude sqare value: %w", err)
}
l[3] = int(val)
if len(location) == 6 {
// Subsquare
l[4], err = l2n(string(location[4]))
if err != nil {
return 0, 0, fmt.Errorf("longitude subsquare value: %w", err)
}
l[5], err = l2n(string(location[5]))
if err != nil {
return 0, 0, fmt.Errorf("latitude subsquare value: %w", err)
}
}
long := (float64(l[0]) * 20) + (float64(l[2]) * 2) + (float64(l[4]) / 12) - 180
lat := (float64(l[1]) * 10) + float64(l[3]) + (float64(l[5]) / 24) - 90
return lat, long, nil
}
var num2let = []string{
`a`,
`b`,
`c`,
`d`,
`e`,
`f`,
`g`,
`h`,
`i`,
`j`,
`k`,
`l`,
`m`,
`n`,
`o`,
`p`,
`q`,
`r`,
`s`,
`t`,
`u`,
`v`,
`w`,
`x`,
}
func n2l(number int) (string, error) {
if number > (len(num2let) - 1) {
return "", errors.New("number out of bounds")
}
return num2let[number], nil
}
func upperN2L(number int) (string, error) {
val, err := n2l(number)
return strings.ToUpper(val), err
}
var let2num = map[string]int{
`a`: 0,
`b`: 1,
`c`: 2,
`d`: 3,
`e`: 4,
`f`: 5,
`g`: 6,
`h`: 7,
`i`: 8,
`j`: 9,
`k`: 10,
`l`: 11,
`m`: 12,
`n`: 13,
`o`: 14,
`p`: 15,
`q`: 16,
`r`: 17,
`s`: 18,
`t`: 19,
`u`: 20,
`v`: 21,
`w`: 22,
`x`: 23,
}
func l2n(letter string) (int, error) {
val, ok := let2num[letter]
if !ok {
return 0, errors.New("illegal character")
}
return val, nil
}