PyNMEA2 is a python library for the NMEA 0183 protocol.
In [1]:
import pynmea2
GGA - GPS Fix Data
In [2]:
msg = pynmea2.parse("$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D", check=True)
msg
Out[2]:
The lat
and lon
attributes are in DDDMM.SSSSS format while latitude
and longitude
are their float values.
In [10]:
msg.lat, msg.latitude, msg.latitude_minutes, msg.latitude_seconds
Out[10]:
In [11]:
msg.lon, msg.longitude, msg.longitude_minutes, msg.longitude_seconds
Out[11]:
VTG - Track Made Good and Ground Speed
In [4]:
pynmea2.parse("$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48")
Out[4]:
ZDA - Time and Date
In [13]:
pynmea2.parse("$GPZDA,201530.00,04,07,2002,00,00*60") # Time is in UTC, or GPS time if offset is not yet known
Out[13]:
In [6]:
msg = pynmea2.GGA('GP', 'GGA', ('184353.07', '1929.045', 'S', '02410.506', 'E', '1', '04', '2.6',
'100.00', 'M', '-33.9', 'M', '', '0000'))
msg
Out[6]:
In [7]:
str(msg)
Out[7]:
Notes
pynmea2.GGA
class and others require lat
and lon
values in DDDMM.SSSSS format as well as lat_dir
and lon_dir
. Use the following function to convert floats into that format (the inverse to nmea_utils.py:dm_to_sd()).
In [8]:
def sd_to_dm(latitude, longitude):
if latitude < 0:
lat_dir = 'S'
else:
lat_dir = 'N'
lat = ('%010.5f' % (abs(int(latitude)) * 100 + (abs(latitude) % 1.0) * 60)).rstrip('0')
if longitude < 0:
lon_dir = 'W'
else:
lon_dir = 'E'
lon = ('%011.5f' % (abs(int(longitude)) * 100 + (abs(longitude) % 1.0) * 60)).rstrip('0')
return lat, lat_dir, lon, lon_dir
# 1929.045,S,02410.506,E
sd_to_dm(-19.484083333333334, 24.1751)
Out[8]:
In [ ]: