source: src/webapp/webservice/inc/GnuBoat/Nmea/Nmea0183/Nmea0183Rmc.php@ bd0cb38

debian/bullseye
Last change on this file since bd0cb38 was 26f77a6, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago
  • Fixed wrong nmea 0183 position formats. They are now (d: degrees, m: minutes):
    • Latitude: ddmm.mmmm
    • Longitude: dddmm.mmmm
  • Added checkums-fix to gnuboat-log-converter to fix checksums of nmea sentences during conversion, if needed
  • Property mode set to 100644
File size: 5.7 KB
Line 
1<?php
2/*
3 Copyright (C) 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19namespace GnuBoat\Nmea\Nmea0183;
20
21/**
22 * @brief NMEA0183 RMC Naviation sentence
23 *
24 * Keep in mind, that the latitude() and longitude() is converted to +/- values
25 */
26class Nmea0183Rmc extends Nmea0183Sentence
27{
28 function __construct($talker, $checksum, $sentence)
29 {
30 parent::__construct("RMC", $talker, $checksum, $sentence);
31
32 // init default values
33 $this->_dateTimeUtc = null;
34 $this->_status = Nmea0183RmcStatus::V;
35 $this->_latitude = 0;
36 $this->_latitudeDirection = Nmea0183Direction::Invalid;
37 $this->_longitude = 0;
38 $this->_longitudeDirection = Nmea0183Direction::Invalid;
39 $this->_speedOverGround = 0;
40 $this->_courseOverGround = 0;
41 $this->_magneticDeclination = 0;
42 $this->_magneticDeclinationDirection = Nmea0183Direction::Invalid;
43 $this->_signalIntegrity = Nmea0183RmcSignal::DataNotValid;
44
45 $content = explode(",", $sentence);
46 if(count($content) != 13)
47 return;
48
49 if(strlen($content[1]) > 6)
50 $this->_dateTimeUtc = \DateTime::createFromFormat('dmy His.u', $content[9] ." ". $content[1], new \DateTimeZone("UTC"));
51 else
52 $this->_dateTimeUtc = \DateTime::createFromFormat('dmy His', $content[9] ." ". $content[1], new \DateTimeZone("UTC"));
53
54 if($content[2] == 'A')
55 $this->_status = Nmea0183RmcStatus::A;
56
57 $this->_latitude = Nmea0183Rmc::nmeaLatitudeToDecimalDegrees($content[3]);
58 if(Nmea0183Rmc::directionToEnum($content[4]) == Nmea0183Direction::South)
59 $this->_latitude = $this->_latitude * -1;
60
61 $this->_longitude = Nmea0183Rmc::nmeaLongitudeToDecimalDegrees($content[5]);
62 if(Nmea0183Rmc::directionToEnum($content[6]) == Nmea0183Direction::West)
63 $this->_longitude = $this->_longitude * -1;
64
65 $this->_speedOverGround = (float)$content[7];
66
67 $this->_courseOverGround = (float)$content[8];
68
69 $this->_magneticDeclination = (float)$content[10];
70 $this->_magneticDeclinationDirection = Nmea0183Rmc::directionToEnum($content[11]);
71
72 $tmp = explode("*", $content[12]);
73 $this->_signalIntegrity = Nmea0183Rmc::signalIntegrityToEnum($tmp[0]);
74 }
75
76 public function dateTime($timezone = "UTC")
77 {
78 $timezone = new \DateTimeZone($timezone);
79 $date = $this->_dateTimeUtc;
80 if($date == null)
81 return null;
82 $date->setTimezone($timezone);
83 return $date;
84 }
85
86 public function status()
87 {
88 return $this->_status;
89 }
90
91 /**
92 * @brief Latitude with North (+) and South (-)
93 */
94 public function latitude()
95 {
96 return $this->_latitude;
97 }
98
99 /**
100 * @brief Longitude with East (+) and West (-)
101 */
102 public function longitude()
103 {
104 return $this->_longitude;
105 }
106
107 public function speedOverGround()
108 {
109 return $this->_speedOverGround;
110 }
111
112 public function courseOverGround()
113 {
114 return $this->_courseOverGround;
115 }
116
117 public function magneticDeclination()
118 {
119 return $this->_magneticDeclination;
120 }
121
122 public function magneticDeclinationDirection()
123 {
124 return $this->_magneticDeclinationDirection;
125 }
126
127 public function signalIntegrity()
128 {
129 return $this->_signalIntegrity;
130 }
131
132 private static function directionToEnum($direction)
133 {
134 switch($direction){
135 case 'N':
136 return Nmea0183Direction::North;
137 case 'E':
138 return Nmea0183Direction::East;
139 case 'S':
140 return Nmea0183Direction::South;
141 case 'W':
142 return Nmea0183Direction::West;
143 }
144 return Nmea0183Direction::Invalid;
145 }
146
147 private static function signalIntegrityToEnum($integrity)
148 {
149 switch($integrity){
150 case 'A':
151 return Nmea0183RmcSignal::AutonomousMode;
152 case 'D':
153 return Nmea0183RmcSignal::DifferentialMode;
154 case 'E':
155 return Nmea0183RmcSignal::EstimatedMode;
156 case 'M':
157 return Nmea0183RmcSignal::ManualInputMode;
158 case 'S':
159 return Nmea0183RmcSignal::SimulatedMode;
160 }
161 return Nmea0183RmcSignal::DataNotValid;
162 }
163
164 private static function nmeaLatitudeToDecimalDegrees($nmeaValue)
165 {
166 if((float)$nmeaValue == 0)
167 return 0;
168
169 $matches = array();
170 $result = preg_match('/^([0-9]{2})([0-9]{2}\.[0-9]+)$/', $nmeaValue, $matches);
171 if($result === false || $result != 1 || count($matches) != 3){
172 $message = "Could not convert nmea latitude position into decimal degress ". $nmeaValue;
173 error_log($message);
174 return 0;
175 }
176 $decimalDegrees = (float)$matches[1];
177 $decimalMinutes = (float)$matches[2];
178 return $decimalDegrees + ($decimalMinutes / 60.0);
179 }
180
181 private static function nmeaLongitudeToDecimalDegrees($nmeaValue)
182 {
183 if((float)$nmeaValue == 0)
184 return 0;
185
186 $matches = array();
187 $result = preg_match('/^([0-9]{3})([0-9]{2}\.[0-9]+)$/', $nmeaValue, $matches);
188 if($result === false || $result != 1 || count($matches) != 3){
189 $message = "Could not convert nmea longitude position into decimal degress ". $nmeaValue;
190 error_log($message);
191 return 0;
192 }
193 $decimalDegrees = (float)$matches[1];
194 $decimalMinutes = (float)$matches[2];
195 return $decimalDegrees + ($decimalMinutes / 60.0);
196 }
197
198 private $_dateTimeUtc;
199 private $_status;
200 private $_latitude;
201 private $_longitude;
202 private $_speedOverGround;
203 private $_courseOverGround;
204 private $_magneticDeclination;
205 private $_magneticDeclinationDirection;
206 private $_signalIntegrity;
207}
208
209?>
Note: See TracBrowser for help on using the repository browser.