如何解决如何使用python读取nmea文件
我尝试读取 GPS 记录器写入的 nmea 文件。 我只能找到很多关于串口的解决方案,但是我已经把文件写完了。
有没有办法读取 nmea 文件并获取 Pandas DataFrame?
解决方法
您可以使用以下代码将 nmea 文件转换为 csv:
https://gis.stackexchange.com/questions/180523/nmea-to-csv-with-timestamp-using-gpsbabel
import csv
from datetime import datetime
import math
# adapt this to your file
INPUT_FILENAME = 'in.nmea'
OUTPUT_FILENAME = 'out.csv'
# open the input file in read mode
with open(INPUT_FILENAME,'r') as input_file:
# open the output file in write mode
with open(OUTPUT_FILENAME,'wt') as output_file:
# create a csv reader object from the input file (nmea files are basically csv)
reader = csv.reader(input_file)
# create a csv writer object for the output file
writer = csv.writer(output_file,delimiter=',',lineterminator='\n')
# write the header line to the csv file
writer.writerow(['date_and_time','lat','lon','speed'])
# iterate over all the rows in the nmea file
for row in reader:
# skip all lines that do not start with $GPRMC
if not row[0].startswith('$GPRMC'):
continue
else:
# for each row,fetch the values from the row's columns
# columns that are not used contain technical GPS stuff that you are probably not interested in
time = row[1]
warning = row[2]
lat = row[3]
lat_direction = row[4]
lon = row[5]
lon_direction = row[6]
speed = row[7]
date = row[9]
# if the "warning" value is "V" (void),you may want to skip it since this is an indicator for an incomplete data row)
if warning == 'V':
continue
# merge the time and date columns into one Python datetime object (usually more convenient than having both separately)
date_and_time = datetime.strptime(date + ' ' + time,'%d%m%y %H%M%S.%f')
# convert the Python datetime into your preferred string format,see http://www.tutorialspoint.com/python/time_strftime.htm for futher possibilities
date_and_time = date_and_time.strftime('%y-%m-%d %H:%M:%S.%f')[:-3] # [:-3] cuts off the last three characters (trailing zeros from the fractional seconds)
# lat and lon values in the $GPRMC nmea sentences come in an rather uncommon format. for convenience,convert them into the commonly used decimal degree format which most applications can read.
# the "high level" formula for conversion is: DDMM.MMMMM => DD + (YY.ZZZZ / 60),multiplicated with (-1) if direction is either South or West
# the following reflects this formula in mathematical terms.
# lat and lon have to be converted from string to float in order to do calculations with them.
# you probably want the values rounded to 6 digits after the point for better readability.
lat = round(math.floor(float(lat) / 100) + (float(lat) % 100) / 60,6)
if lat_direction == 'S':
lat = lat * -1
lon = round(math.floor(float(lon) / 100) + (float(lon) % 100) / 60,6)
if lon_direction == 'W':
lon = lon * -1
# speed is given in knots,you'll probably rather want it in km/h and rounded to full integer values.
# speed has to be converted from string to float first in order to do calculations with it.
# conversion to int is to get rid of the tailing ".0".
speed = int(round(float(speed) * 1.852,0))
# write the calculated/formatted values of the row that we just read into the csv file
writer.writerow([date_and_time,lat,lon,speed])
然后用pandas打开csv
import pandas as pd
data = pd.read_csv("out.csv")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。