| 1 | // 8:367:22 Defined by an email from Greg Johnson representing the |
| 2 | // USCG, Fall 2012. Breaks from the RTCM and IMO Circular 289. |
| 3 | // "Area Notice Message Release Version: 1" 13 Aug 2012 |
| 4 | // |
| 5 | // http://www.e-navigation.nl/content/geographic-notice |
| 6 | // http://www.e-navigation.nl/sites/default/files/asm_files/GN%20Release%20Version%201b.pdf |
| 7 | |
| 8 | #include <cmath> |
| 9 | |
| 10 | #include "ais.h" |
| 11 | |
| 12 | namespace libais { |
| 13 | |
| 14 | const size_t SUB_AREA_BITS = 96; |
| 15 | |
| 16 | static int scale_multipliers[4] = {1, 10, 100, 1000}; |
| 17 | |
| 18 | Ais8_367_22_Circle::Ais8_367_22_Circle( |
| 19 | const AisBitset &bits, const size_t offset) |
| 20 | : precision(0), radius_m(0), spare(0) { |
| 21 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 22 | position = bits.ToAisPoint(offset + 2, 55); |
| 23 | precision = bits.ToUnsignedInt(offset + 57, 3); |
| 24 | radius_m = |
| 25 | bits.ToUnsignedInt(offset + 60, 12) * scale_multipliers[scale_factor]; |
| 26 | spare = bits.ToUnsignedInt(offset + 72, 21); |
| 27 | } |
| 28 | |
| 29 | Ais8_367_22_Rect::Ais8_367_22_Rect(const AisBitset &bits, const size_t offset) |
| 30 | : precision(0), e_dim_m(0), n_dim_m(0), orient_deg(0), spare(0) { |
| 31 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 32 | position = bits.ToAisPoint(offset + 2, 55); |
| 33 | precision = bits.ToUnsignedInt(offset + 57, 3); |
| 34 | e_dim_m = |
| 35 | bits.ToUnsignedInt(offset + 60, 8) * scale_multipliers[scale_factor]; |
| 36 | n_dim_m = |
| 37 | bits.ToUnsignedInt(offset + 68, 8) * scale_multipliers[scale_factor]; |
| 38 | orient_deg = bits.ToUnsignedInt(offset + 76, 9); |
| 39 | spare = bits.ToUnsignedInt(offset + 85, 8); |
| 40 | } |
| 41 | |
| 42 | Ais8_367_22_Sector::Ais8_367_22_Sector( |
| 43 | const AisBitset &bits, const size_t offset) |
| 44 | : precision(0), |
| 45 | radius_m(0), |
| 46 | left_bound_deg(0), |
| 47 | right_bound_deg(0), |
| 48 | spare(0) { |
| 49 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 50 | position = bits.ToAisPoint(offset + 2, 55); |
| 51 | precision = bits.ToUnsignedInt(offset + 57, 3); |
| 52 | radius_m = |
| 53 | bits.ToUnsignedInt(offset + 60, 12) * scale_multipliers[scale_factor]; |
| 54 | left_bound_deg = bits.ToUnsignedInt(offset + 72, 9); |
| 55 | right_bound_deg = bits.ToUnsignedInt(offset + 81, 9); |
| 56 | spare = bits.ToUnsignedInt(offset + 90, 3); |
| 57 | } |
| 58 | |
| 59 | // Polyline or polygon. |
| 60 | Ais8_367_22_Poly::Ais8_367_22_Poly(const AisBitset &bits, const size_t offset, |
| 61 | Ais8_366_22_AreaShapeEnum area_shape) |
| 62 | : shape(area_shape), precision(0), spare(0) { |
| 63 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 64 | size_t poly_offset = offset + 2; |
| 65 | for (size_t i = 0; i < 4; i++) { |
| 66 | const int angle = bits.ToUnsignedInt(poly_offset, 10); |
| 67 | poly_offset += 10; |
| 68 | const int dist = bits.ToUnsignedInt(poly_offset, 11) * |
| 69 | scale_multipliers[scale_factor]; |
| 70 | poly_offset += 11; |
| 71 | if (dist == 0) { |
| 72 | break; |
| 73 | } |
| 74 | angles.push_back(angle); |
| 75 | dists_m.push_back(dist); |
| 76 | } |
| 77 | spare = bits.ToUnsignedInt(offset + 86, 7); |
| 78 | } |
| 79 | |
| 80 | Ais8_367_22_Text::Ais8_367_22_Text(const AisBitset &bits, const size_t offset) { |
| 81 | text = string(bits.ToString(offset, 90)); |
| 82 | spare = bits.ToUnsignedInt(offset + 90, 3); |
| 83 | } |
| 84 | |
| 85 | Ais8_367_22_SubArea *ais8_367_22_subarea_factory(const AisBitset &bits, |
| 86 | const size_t offset) { |
| 87 | const Ais8_366_22_AreaShapeEnum area_shape = |
| 88 | static_cast<Ais8_366_22_AreaShapeEnum>(bits.ToUnsignedInt(offset, 3)); |
| 89 | |
| 90 | switch (area_shape) { |
| 91 | case AIS8_366_22_SHAPE_CIRCLE: |
| 92 | return new Ais8_367_22_Circle(bits, offset + 3); |
| 93 | case AIS8_366_22_SHAPE_RECT: |
| 94 | return new Ais8_367_22_Rect(bits, offset + 3); |
| 95 | case AIS8_366_22_SHAPE_SECTOR: |
| 96 | return new Ais8_367_22_Sector(bits, offset + 3); |
| 97 | case AIS8_366_22_SHAPE_POLYLINE: // FALLTHROUGH |
| 98 | case AIS8_366_22_SHAPE_POLYGON: |
| 99 | return new Ais8_367_22_Poly(bits, offset + 3, area_shape); |
| 100 | case AIS8_366_22_SHAPE_TEXT: |
| 101 | return new Ais8_367_22_Text(bits, offset + 3); |
| 102 | case AIS8_366_22_SHAPE_RESERVED_6: // FALLTHROUGH |
| 103 | case AIS8_366_22_SHAPE_RESERVED_7: // FALLTHROUGH |
| 104 | // Leave area as 0 to indicate error. |
| 105 | break; |
| 106 | case AIS8_366_22_SHAPE_ERROR: |
| 107 | break; |
| 108 | default: |
| 109 | assert(false); |
| 110 | } |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | Ais8_367_22::Ais8_367_22(const char *nmea_payload, const size_t pad) |
| 115 | : Ais8(nmea_payload, pad), version(0), link_id(0), notice_type(0), |
| 116 | month(0), day(0), hour(0), minute(0), duration_minutes(0), spare2(0) { |
| 117 | assert(dac == 367); |
| 118 | assert(fi == 22); |
| 119 | |
| 120 | if (!CheckStatus()) { |
| 121 | return; |
| 122 | } |
| 123 | if (num_bits < 216 || num_bits > 1016) { |
| 124 | status = AIS_ERR_BAD_BIT_COUNT; |
| 125 | return; |
| 126 | } |
| 127 | // Is there more than spare bits after the last submessage? |
| 128 | if ((num_bits - 120) % SUB_AREA_BITS >= 6) { |
| 129 | // More than just spare. |
| 130 | status = AIS_ERR_BAD_BIT_COUNT; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | bits.SeekTo(56); |
| 135 | version = bits.ToUnsignedInt(56, 6); |
| 136 | link_id = bits.ToUnsignedInt(62, 10); |
| 137 | notice_type = bits.ToUnsignedInt(72, 7); |
| 138 | month = bits.ToUnsignedInt(79, 4); |
| 139 | day = bits.ToUnsignedInt(83, 5); |
| 140 | hour = bits.ToUnsignedInt(88, 5); |
| 141 | minute = bits.ToUnsignedInt(93, 6); |
| 142 | duration_minutes = bits.ToUnsignedInt(99, 18); |
| 143 | spare2 = bits.ToUnsignedInt(117, 3); |
| 144 | |
| 145 | const int num_sub_areas = static_cast<int>( |
| 146 | floor((num_bits - 120) / static_cast<float>(SUB_AREA_BITS))); |
| 147 | |
| 148 | for (int area_idx = 0; area_idx < num_sub_areas; area_idx++) { |
| 149 | const size_t start = 120 + area_idx * SUB_AREA_BITS; |
| 150 | Ais8_367_22_SubArea *area = ais8_367_22_subarea_factory(bits, start); |
| 151 | if (area != nullptr) { |
| 152 | sub_areas.push_back(area); |
| 153 | } else { |
| 154 | status = AIS_ERR_BAD_SUB_SUB_MSG; |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // TODO(schwehr): Save the spare bits at the end of the message. |
| 160 | assert(bits.GetRemaining() < 6); |
| 161 | status = AIS_OK; |
| 162 | } |
| 163 | |
| 164 | Ais8_367_22::~Ais8_367_22() { |
| 165 | // Switch to unique_ptr. |
| 166 | for (size_t i = 0; i < sub_areas.size(); i++) { |
| 167 | delete sub_areas[i]; |
| 168 | sub_areas[i] = nullptr; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } // namespace libais |