-
Notifications
You must be signed in to change notification settings - Fork 13
/
positionreport.go
251 lines (200 loc) · 7.98 KB
/
positionreport.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2015, Marios Andreopoulos.
//
// This file is part of aislib.
//
// Aislib is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Aislib is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with aislib. If not, see <http://www.gnu.org/licenses/>.
package aislib
import (
"errors"
"math"
)
// A PositionReport is the generic structure of a Position Report, containing the common fields
// between Class A and B reports.
type PositionReport struct {
Type uint8
Repeat uint8
MMSI uint32
Speed float32 // speed over ground - SOG (sc U3)
Accuracy bool // position accuracy
Lon float64 // (sc I4)
Lat float64 // (sc I4)
Course float32 //course over ground - COG (sc U1)
Heading uint16 // true heading - HDG
Second uint8 // timestamp
}
// A ClassAPositionReport is a decoded AIS position message (messages of type 1, 2 or 3).
// Please have a look at http://catb.org/gpsd/AIVDM.html and at
// http://www.navcen.uscg.gov/?pageName=AISMessagesA
type ClassAPositionReport struct {
PositionReport
RAIM bool // RAIM flag
Radio uint32 // Radio status
Status uint8 // navigation status (enumerated type)
Turn float32 // rate of turn - ROT (sc - Special Calc I3)
Maneuver uint8 // maneuver indicator (enumerated)
}
// A ClassBPositionReport is a decoded AIS position message (type 18).
type ClassBPositionReport struct {
PositionReport
RAIM bool // RAIM flag
Radio uint32 // Radio status
CSUnit bool
Display bool
DSC bool
Band bool
Msg22 bool
Assigned bool
}
// A ExtendedClassBPositionReport is a decoded AIS position message (type 19).
type ExtendedClassBPositionReport struct {
PositionReport
VesselName string
ShipType uint8
ToBow uint16 // Dimension to bow
ToStern uint16 // Dimension to stern
ToPort uint8 // Dimension to port
ToStarboard uint8 // Dimension to starboard
EPFD uint8 // Position Fix Type (enumeration declared at basestationreport.go)
RAIM bool // RAIM flag
}
// Navigation status codes
var NavigationStatusCodes = [...]string{
"Under way using engine", "At anchor", "Not under command", "Restricted maneuverability",
"Constrained by her draught", "Moored", "Aground", "Engaged in fishing", "Under way sailing",
"status code reserved", "status code reserved", "status code reserved",
"status code reserved", "status code reserved", "AIS-SART is active", "Not defined",
}
// DecodeClassAPositionReport decodes [the payload of] an AIS position message (type 1/2/3)
func DecodeClassAPositionReport(payload string) (ClassAPositionReport, error) {
data := []byte(payload)
var m ClassAPositionReport
m.Type = decodeAisChar(data[0])
if m.Type != 1 && m.Type != 2 && m.Type != 3 {
return m, errors.New("Message isn't Class A Position Report (type 1, 2 or 3).")
}
// !!! This is the first decoding function written. Original decoding
// routines are left here as comments, in order to help anyone diving
// into binary field decoding.
//m.Repeat = decodeAisChar(data[1]) >> 4
m.Repeat = uint8(bitsToInt(6, 7, data))
//m.MMSI = uint32(decodeAisChar(data[1]))<<28>>2 | uint32(decodeAisChar(data[2]))<<20 |
// uint32(decodeAisChar(data[3]))<<14 | uint32(decodeAisChar(data[4]))<<8 |
// uint32(decodeAisChar(data[5]))<<2 | uint32(decodeAisChar(data[6]))>>4
m.MMSI = bitsToInt(8, 37, data)
//m.Status = (decodeAisChar(data[6]) << 4) >> 4
m.Status = uint8(bitsToInt(38, 41, data))
//m.Turn = float32(int8(decodeAisChar(data[7])<<2 | decodeAisChar(data[8])>>4))
m.Turn = float32(int8(bitsToInt(42, 49, data)))
if m.Turn != 0 && m.Turn <= 126 && m.Turn >= -126 {
sign := float32(1)
if math.Signbit(float64(m.Turn)) {
sign = -1
}
m.Turn = sign * (m.Turn / 4.733) * (m.Turn / 4.733)
}
//m.Speed = float32(uint16(decodeAisChar(data[8]))<<12>>6 | uint16(decodeAisChar(data[9])))
m.Speed = cbnSpeed(50, data)
//m.Accuracy = false
//if decodeAisChar(data[10])>>5 == 1 {
// m.Accuracy = true
//}
m.Accuracy = cbnBool(60, data)
// Old method 1
//m.Lon = float64((int32(decodeAisChar(data[10]))<<27 | int32(decodeAisChar(data[11]))<<21 |
// int32(decodeAisChar(data[12]))<<15 | int32(decodeAisChar(data[13]))<<9 |
// int32(decodeAisChar(data[14]))>>1<<4)) / 16
//m.Lat = float64((int32(decodeAisChar(data[14]))<<31 | int32(decodeAisChar(data[15]))<<25 |
// int32(decodeAisChar(data[16]))<<19 | int32(decodeAisChar(data[17]))<<13 |
// int32(decodeAisChar(data[18]))<<7 | int32(decodeAisChar(data[19]))>>4<<5)) / 32
// Old method 2
//m.Lon = float64((int32(bitsToInt(61, 88, data)) << 4)) / 16
//m.Lat = float64((int32(bitsToInt(89, 115, data)) << 5)) / 32
// Finish or both old methods
//m.Lon, m.Lat = CoordinatesMin2Deg(m.Lon, m.Lat)
m.Lon, m.Lat = cbnCoordinates(61, data)
//m.Course = float32(uint16(decodeAisChar(data[19]))<<12>>4|uint16(decodeAisChar(data[20]))<<2|
// uint16(decodeAisChar(data[21]))>>4) / 10
m.Course = float32(bitsToInt(116, 127, data)) / 10
//m.Heading = uint16(decodeAisChar(data[21]))<<12>>7 | uint16(decodeAisChar(data[22]))>>1
m.Heading = uint16(bitsToInt(128, 136, data))
//m.Second = decodeAisChar(data[22])<<7>>2 | decodeAisChar(data[23])>>1
m.Second = uint8(bitsToInt(137, 142, data))
//m.Maneuver = decodeAisChar(data[23])<<7>>6 | decodeAisChar(data[24])>>5
m.Maneuver = uint8(bitsToInt(143, 144, data))
//m.RAIM = false
//if decodeAisChar(data[24])<<6>>7 == 1 {
// m.RAIM = true
//}
m.RAIM = cbnBool(148, data)
m.Radio = bitsToInt(149, 167, data)
return m, nil
}
// DecodeClassBPositionReport decodes [the payload of] an AIS position message (type 18)
func DecodeClassBPositionReport(payload string) (ClassBPositionReport, error) {
data := []byte(payload)
var m ClassBPositionReport
m.Type = decodeAisChar(data[0])
if m.Type != 18 {
return m, errors.New("Message isn't Class B Position Report (type 18).")
}
m.Repeat = uint8(bitsToInt(6, 7, data))
m.MMSI = bitsToInt(8, 37, data)
m.Speed = cbnSpeed(46, data)
m.Accuracy = cbnBool(56, data)
m.Lon, m.Lat = cbnCoordinates(57, data)
m.Course = float32(bitsToInt(112, 123, data)) / 10
m.Heading = uint16(bitsToInt(124, 132, data))
m.Second = uint8(bitsToInt(133, 138, data))
m.CSUnit = cbnBool(141, data)
m.Display = cbnBool(142, data)
m.DSC = cbnBool(143, data)
m.Band = cbnBool(144, data)
m.Msg22 = cbnBool(145, data)
m.Assigned = cbnBool(146, data)
m.RAIM = cbnBool(147, data)
if decodeAisChar(data[24])<<6>>7 == 1 {
m.RAIM = true
}
m.Radio = bitsToInt(148, 167, data)
return m, nil
}
// DecodeExtendedClassBPositionReport decodes [the payload of] an AIS extendedposition message (type 19)
func DecodeExtendedClassBPositionReport(payload string) (ExtendedClassBPositionReport, error) {
data := []byte(payload)
var m ExtendedClassBPositionReport
m.Type = decodeAisChar(data[0])
if m.Type != 19 {
return m, errors.New("Message isn't Extended Class B Position Report (type 18).")
}
m.Repeat = uint8(bitsToInt(6, 7, data))
m.MMSI = bitsToInt(8, 37, data)
m.Speed = cbnSpeed(46, data)
m.Accuracy = cbnBool(56, data)
m.Lon, m.Lat = cbnCoordinates(57, data)
m.Course = float32(bitsToInt(112, 123, data)) / 10
m.Heading = uint16(bitsToInt(124, 132, data))
m.Second = uint8(bitsToInt(133, 138, data))
m.VesselName = bitsToString(143, 262, data)
m.ShipType = uint8(bitsToInt(263, 270, data))
m.ToBow = uint16(bitsToInt(271, 279, data))
m.ToStern = uint16(bitsToInt(280, 288, data))
m.ToPort = uint8(bitsToInt(289, 294, data))
m.ToStarboard = uint8(bitsToInt(295, 300, data))
m.EPFD = uint8(bitsToInt(301, 304, data))
m.RAIM = cbnBool(147, data)
if decodeAisChar(data[24])<<6>>7 == 1 {
m.RAIM = true
}
return m, nil
}