Add license
[uberlogger] / nmea.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2010 Dmitry Marakasov
4 #
5 # This file is part of UberLogger.
6 #
7 # UberLogger is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # UberLogger is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with UberLogger.  If not, see <http://www.gnu.org/licenses/>.
19
20 import re
21
22 class GPSData:
23         def __init__(self):
24                 self.reset()
25
26         def reset(self):
27                 self.time = None
28                 self.lat = None
29                 self.lon = None
30                 self.quality = None
31                 self.nsat = None
32                 self.hdop = None
33                 self.ele = None
34
35         def parse_nmea_string(self, string):
36                 try:
37                         string = string.lstrip('$')
38                         (data, dummy, csum) = string.rpartition('*')
39
40                         mycsum = 0
41                         for byte in data:
42                                 mycsum ^= ord(byte)
43
44                         if mycsum != int(csum, 16): # can throw with invalid csum
45                                 return
46
47                         data = data.split(',')
48
49                         if data[0] == 'GPGGA':
50                                 self.reset()
51
52                                 # time
53                                 (temp, n) = re.subn('^(\d{2})(\d{2})(\d{2}\.\d+)$', '\\1:\\2:\\3', data[1])
54                                 if n == 1:
55                                         self.time = temp
56
57                                 # latitude
58                                 (temp, n) = re.subn('^(\d{2})(\d{2})\.(.*)$', '\\1.\\2\\3', data[2])
59                                 if n == 1:
60                                         self.lat = temp + data[3]
61
62                                 # longitude
63                                 (temp, n) = re.subn('^0?(\d{2,3}?)(\d{2})\.(.*)$', '\\1.\\2\\3', data[4])
64                                 if n == 1:
65                                         self.lon = temp + data[5]
66
67                                 # quality
68                                 if int(data[6]) == 0:
69                                         self.quality = "none"
70                                 elif int(data[6]) == 1:
71                                         self.quality = "normal"
72                                 elif int(data[6]) == 2:
73                                         self.quality = "diff"
74                                 elif int(data[6]) == 3:
75                                         self.quality = "precision"
76                                 else:
77                                         self.quality = None
78
79                                 # other params
80                                 self.nsat = int(data[7])
81                                 self.hdop = data[8]
82                                 self.ele = data[9]
83                 except:
84                         return
85
86         def dump(self):
87                 return "Time: %s\tQuality: %s\nLat: %s\tSat: %s\nLon: %s\tHDOP: %s\nEle: %s" % (self.time, self.quality, self.lat, self.nsat, self.lon, self.hdop, self.ele)