-
Notifications
You must be signed in to change notification settings - Fork 6
/
coordinates.go
197 lines (156 loc) · 4.51 KB
/
coordinates.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package locodedb
import (
"fmt"
"strings"
"github.com/nspcc-dev/locode-db/pkg/locodedb"
)
const (
minutesDigits = 2
hemisphereSymbols = 1
)
const (
latDegDigits = 2
lngDegDigits = 3
)
type coordinateCode struct {
degDigits int
value []uint8
}
// LongitudeCode represents the value of the longitude
// of the location conforming to UN/LOCODE specification.
type LongitudeCode coordinateCode
// LongitudeHemisphere represents the hemisphere of the earth
// // along the Greenwich meridian.
type LongitudeHemisphere [hemisphereSymbols]uint8
// LatitudeCode represents the value of the latitude
// of the location conforming to UN/LOCODE specification.
type LatitudeCode coordinateCode
// LatitudeHemisphere represents the hemisphere of the earth
// along the equator.
type LatitudeHemisphere [hemisphereSymbols]uint8
func coordinateFromString(s string, degDigits int, hemisphereAlphabet []uint8) (*coordinateCode, error) {
if len(s) != degDigits+minutesDigits+hemisphereSymbols {
return nil, locodedb.ErrInvalidString
}
for i := range s[:degDigits+minutesDigits] {
if !isDigit(s[i]) {
return nil, locodedb.ErrInvalidString
}
}
loop:
for _, sym := range s[degDigits+minutesDigits:] {
for j := range hemisphereAlphabet {
if hemisphereAlphabet[j] == uint8(sym) {
continue loop
}
}
return nil, locodedb.ErrInvalidString
}
return &coordinateCode{
degDigits: degDigits,
value: []uint8(s),
}, nil
}
func isDigit(sym uint8) bool {
return sym >= '0' && sym <= '9'
}
// LongitudeFromString parses a string and returns the location's longitude.
func LongitudeFromString(s string) (*LongitudeCode, error) {
cc, err := coordinateFromString(s, lngDegDigits, []uint8{'W', 'E'})
if err != nil {
return nil, err
}
return (*LongitudeCode)(cc), nil
}
// LatitudeFromString parses a string and returns the location's latitude.
func LatitudeFromString(s string) (*LatitudeCode, error) {
cc, err := coordinateFromString(s, latDegDigits, []uint8{'N', 'S'})
if err != nil {
return nil, err
}
return (*LatitudeCode)(cc), nil
}
func (cc *coordinateCode) degrees() []uint8 {
return cc.value[:cc.degDigits]
}
// Degrees returns the longitude's degrees.
func (lc *LongitudeCode) Degrees() (l [lngDegDigits]uint8) {
copy(l[:], (*coordinateCode)(lc).degrees())
return
}
// Degrees returns the latitude's degrees.
func (lc *LatitudeCode) Degrees() (l [latDegDigits]uint8) {
copy(l[:], (*coordinateCode)(lc).degrees())
return
}
func (cc *coordinateCode) minutes() (mnt [minutesDigits]uint8) {
for i := 0; i < minutesDigits; i++ {
mnt[i] = cc.value[cc.degDigits+i]
}
return
}
// Minutes returns the longitude's minutes.
func (lc *LongitudeCode) Minutes() [minutesDigits]uint8 {
return (*coordinateCode)(lc).minutes()
}
// Minutes returns the latitude's minutes.
func (lc *LatitudeCode) Minutes() [minutesDigits]uint8 {
return (*coordinateCode)(lc).minutes()
}
// Hemisphere returns the longitude's hemisphere code.
func (lc *LongitudeCode) Hemisphere() LongitudeHemisphere {
return (*coordinateCode)(lc).hemisphere()
}
// Hemisphere returns the latitude's hemisphere code.
func (lc *LatitudeCode) Hemisphere() LatitudeHemisphere {
return (*coordinateCode)(lc).hemisphere()
}
func (cc *coordinateCode) hemisphere() (h [hemisphereSymbols]uint8) {
for i := 0; i < hemisphereSymbols; i++ {
h[i] = cc.value[cc.degDigits+minutesDigits+i]
}
return h
}
// North returns true for the northern hemisphere.
func (h LatitudeHemisphere) North() bool {
return h[0] == 'N'
}
// East returns true for the eastern hemisphere.
func (h LongitudeHemisphere) East() bool {
return h[0] == 'E'
}
// Coordinates represents the coordinates of the location from UN/LOCODE table.
type Coordinates struct {
lat *LatitudeCode
lng *LongitudeCode
}
// Latitude returns the location's latitude.
func (c *Coordinates) Latitude() *LatitudeCode {
return c.lat
}
// Longitude returns the location's longitude.
func (c *Coordinates) Longitude() *LongitudeCode {
return c.lng
}
// CoordinatesFromString parses a string and returns the location's coordinates.
func CoordinatesFromString(s string) (*Coordinates, error) {
if len(s) == 0 {
return nil, nil
}
strs := strings.Split(s, " ")
if len(strs) != 2 {
return nil, locodedb.ErrInvalidString
}
lat, err := LatitudeFromString(strs[0])
if err != nil {
return nil, fmt.Errorf("could not parse latitude: %w", err)
}
lng, err := LongitudeFromString(strs[1])
if err != nil {
return nil, fmt.Errorf("could not parse longitude: %w", err)
}
return &Coordinates{
lat: lat,
lng: lng,
}, nil
}