forked from pd0mz/go-aprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.go
194 lines (163 loc) · 3.89 KB
/
position.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
package aprs
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
"github.com/pd0mz/go-maidenhead"
)
var (
ErrInvalidPosition = errors.New(`aprs: invalid position`)
// Position ambiguity replacement
disambiguation = []int{2, 3, 5, 6, 12, 13, 15, 16}
)
const gridChars = "ABCDEFGHIJKLMNOPQRSTUVWX0123456789"
type Position struct {
Latitude float64 // Degrees
Longitude float64 // Degrees
Ambiguity int
Symbol Symbol
Compressed bool
}
func (pos Position) String() string {
if pos.Ambiguity == 0 {
return fmt.Sprintf("{%f, %f}", pos.Latitude, pos.Longitude)
}
return fmt.Sprintf("{%f, %f}, ambiguity=%d", pos.Latitude, pos.Longitude, pos.Ambiguity)
}
func ParseUncompressedPosition(s string) (pos Position, txt string, err error) {
// APRS PROTOCOL REFERENCE 1.0.1 Chapter 8, page 32 (42 in PDF)
if len(s) < 18 {
err = ErrInvalidPosition
return
}
b := []byte(s)
for _, p := range disambiguation {
if b[p] == ' ' {
pos.Ambiguity++
b[p] = '0'
}
}
s = string(b)
var (
latDeg, latMin, latMinFrag uint64
lngDeg, lngMin, lngMinFrag uint64
latHemi, lngHemi byte
isSouth, isWest bool
)
/* 3210.70N/13132.15E# */
log.Printf("s: %q\n", s[:18])
if latDeg, err = strconv.ParseUint(s[0:2], 10, 8); err != nil {
return
}
if latMin, err = strconv.ParseUint(s[2:4], 10, 8); err != nil {
return
}
if latMinFrag, err = strconv.ParseUint(s[5:7], 10, 8); err != nil {
return
}
latHemi = s[7]
pos.Symbol[0] = s[8]
if lngDeg, err = strconv.ParseUint(s[9:12], 10, 8); err != nil {
return
}
if lngMin, err = strconv.ParseUint(s[12:14], 10, 8); err != nil {
return
}
if lngMinFrag, err = strconv.ParseUint(s[15:17], 10, 8); err != nil {
return
}
lngHemi = s[17]
pos.Symbol[1] = s[18]
if latHemi == 'S' || latHemi == 's' {
isSouth = true
} else if latHemi != 'N' && latHemi != 'n' {
err = ErrInvalidPosition
return
}
if lngHemi == 'W' || lngHemi == 'w' {
isWest = true
} else if lngHemi != 'E' && lngHemi != 'e' {
err = ErrInvalidPosition
return
}
if latDeg > 89 || lngDeg > 179 {
err = ErrInvalidPosition
return
}
pos.Latitude = float64(latDeg) + float64(latMin)/60.0 + float64(latMinFrag)/6000.0
pos.Longitude = float64(lngDeg) + float64(lngMin)/60.0 + float64(lngMinFrag)/6000.0
if isSouth {
pos.Latitude = 0.0 - pos.Latitude
}
if isWest {
pos.Longitude = 0.0 - pos.Longitude
}
if pos.Symbol[1] >= 'a' || pos.Symbol[1] <= 'k' {
pos.Symbol[1] -= 32
}
if len(s) > 19 {
txt = s[19:]
}
return
}
func ParseCompressedPosition(s string) (pos Position, txt string, err error) {
// APRS PROTOCOL REFERENCE 1.0.1 Chapter 9, page 36 (46 in PDF)
if len(s) < 10 {
err = ErrInvalidPosition
return
}
// Base-91 check
for _, c := range s[1:9] {
if c < 0x21 || c > 0x7b {
err = ErrInvalidPosition
return
}
}
var lat, lng int
if lat, err = base91Decode(s[1:5]); err != nil {
return
}
if lng, err = base91Decode(s[5:9]); err != nil {
return
}
pos.Latitude = 90.0 - float64(lat)/380926.0
pos.Longitude = -180.0 + float64(lng)/190463.0
pos.Compressed = true
txt = s[10:]
return
}
func ParsePositionGrid(s string) (pos Position, txt string, err error) {
var o int
for o = 0; o < len(s); o++ {
if strings.IndexByte(gridChars, s[o]) < 0 {
break
}
}
if o == 2 || o == 4 || o == 6 || o == 8 {
var p maidenhead.Point
if p, err = maidenhead.ParseLocator(s[:o]); err != nil {
return
}
pos.Latitude = p.Latitude
pos.Longitude = p.Longitude
}
if o < len(s) {
txt = s[o+1:]
}
return
}
func ParsePosition(s string, compressed bool) (Position, string, error) {
log.Printf("parse position %q, %t\n", s, compressed)
if compressed {
return ParseCompressedPosition(s)
}
return ParseUncompressedPosition(s)
}
func ParsePositionBoth(s string) (pos Position, txt string, err error) {
if pos, txt, err = ParseUncompressedPosition(s); err != nil {
pos, txt, err = ParseCompressedPosition(s)
}
return
}