nmea_parser/gnss/
dtm.rs

1/*
2Copyright 2020 Timo Saarinen
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use super::*;
18
19/// DTM - Datum being used
20#[derive(Clone, Debug, PartialEq, Serialize)]
21pub struct DtmData {
22    /// Navigation system
23    pub source: NavigationSystem,
24
25    /// Local datum code
26    pub datum_id: Option<String>,
27
28    /// Local datum subdivision code
29    pub datum_sub_id: Option<String>,
30
31    /// Latitude offset in degrees
32    pub lat_offset: Option<f64>,
33
34    /// Longitude offset in degrees
35    pub lon_offset: Option<f64>,
36
37    /// Altitude offset in metres
38    pub alt_offset: Option<f64>,
39
40    /// Reference datum code
41    pub ref_datum_id: Option<String>,
42}
43
44// -------------------------------------------------------------------------------------------------
45
46/// xxDTM: Datum being used
47pub(crate) fn handle(
48    sentence: &str,
49    nav_system: NavigationSystem,
50) -> Result<ParsedMessage, ParseError> {
51    let split: Vec<&str> = sentence.split(',').collect();
52
53    Ok(ParsedMessage::Dtm(DtmData {
54        source: nav_system,
55        datum_id: pick_string_field(&split, 1),
56        datum_sub_id: pick_string_field(&split, 2),
57        lat_offset: parse_latitude_m_m(split.get(3).unwrap_or(&""), split.get(4).unwrap_or(&""))?,
58        lon_offset: parse_longitude_m_m(split.get(5).unwrap_or(&""), split.get(6).unwrap_or(&""))?,
59        alt_offset: pick_number_field(&split, 7)?,
60        ref_datum_id: pick_string_field(&split, 8),
61    }))
62}
63
64// -------------------------------------------------------------------------------------------------
65
66#[cfg(test)]
67mod test {
68    use super::*;
69
70    #[test]
71    fn test_parse_cpdtm() {
72        match NmeaParser::new().parse_sentence("$GPDTM,999,,0.002,S,0.005,E,005.8,W84*1A") {
73            Ok(ps) => match ps {
74                ParsedMessage::Dtm(dtm) => {
75                    assert_eq!(dtm.source, NavigationSystem::Gps);
76                    assert_eq!(dtm.datum_id, Some("999".into()));
77                    assert_eq!(dtm.datum_sub_id, None);
78                    assert::close(dtm.lat_offset.unwrap_or(0.0), -0.000033, 0.000001);
79                    assert::close(dtm.lon_offset.unwrap_or(0.0), 0.000083, 0.000001);
80                    assert_eq!(dtm.alt_offset, Some(5.8));
81                    assert_eq!(dtm.ref_datum_id, Some("W84".into()));
82                }
83                ParsedMessage::Incomplete => {
84                    assert!(false);
85                }
86                _ => {
87                    assert!(false);
88                }
89            },
90            Err(e) => {
91                assert_eq!(e.to_string(), "OK");
92            }
93        }
94    }
95}