mapillary_download/lib/gps_parser.py

89 lines
2.5 KiB
Python
Raw Normal View History

2023-09-10 21:02:33 +02:00
#!/usr/bin/python
import sys
import os
import datetime
import time
from .geo import gpgga_to_dms, utc_to_localtime
import gpxpy
import pynmea2
'''
Methods for parsing gps data from various file format e.g. GPX, NMEA, SRT.
'''
def get_lat_lon_time_from_gpx(gpx_file, local_time=True):
'''
Read location and time stamps from a track in a GPX file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly.
'''
with open(gpx_file, 'r') as f:
gpx = gpxpy.parse(f)
points = []
if len(gpx.tracks)>0:
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
t = utc_to_localtime(point.time) if local_time else point.time
points.append( (t, point.latitude, point.longitude, point.elevation) )
'''if len(gpx.waypoints) > 0:
for point in gpx.waypoints:
t = utc_to_localtime(point.time) if local_time else point.time
points.append( (t, point.latitude, point.longitude, point.elevation) )'''
# sort by time just in case
points.sort()
return points
def get_lat_lon_time_from_nmea(nmea_file, local_time=True):
'''
Read location and time stamps from a track in a NMEA file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly.
'''
gga_Talker_id = ("$GNGGA", "$GPGGA", "$GLGGA", "$GBGGA", "$GAGGA")
rmc_Talker_id = ("$GNRMC", "$GPRMC", "$GLRMC", "$GBRMC", "$GARMC")
with open(nmea_file, "r") as f:
lines = f.readlines()
lines = [l.rstrip("\n\r") for l in lines]
# Get initial date
for l in lines:
if any(rmc in l for rmc in rmc_Talker_id):
data = pynmea2.parse(l, check=False)
date = data.datetime.date()
break
# Parse GPS trace
points = []
for l in lines:
if any(rmc in l for rmc in rmc_Talker_id):
data = pynmea2.parse(l, check=False)
date = data.datetime.date()
if any(gga in l for gga in gga_Talker_id):
data = pynmea2.parse(l, check=False)
timestamp = datetime.datetime.combine(date, data.timestamp)
lat, lon, alt = data.latitude, data.longitude, data.altitude
points.append((timestamp, lat, lon, alt))
points.sort()
return points