Fork me on GitHub

一 概念
Python有现成的库可以调用来解析GPS数据——pymea2
安装方式:
pip install pynmea2
二 实例
解析单个数据:
import pynmea2

text = "$GNRMC,074733.00,A,2241.28818,N,11358.44210,E,3.866,,070720,,,A*60"
msg = pynmea2.parse(text)

print(msg.latitude)

print(msg.longitude)


解析多个数据:

import pynmea2

#该函数主要针对整个文件的gps进行解析
def parse_file(file_path):
    txt_tables = []
    f = open(file_path, "r",encoding='utf-8')
    line = f.readline() # 读取第一行
    locations = []
    while line:
        text = line[1:]
        msg = pynmea2.parse(text)
        # print(msg.latitude)  #24.551053333333332
        # print(msg.longitude)  #118.1067375
        tmp = []
        if(msg.latitude == 0.0 or msg.longitude == 0.0):
            line = f.readline() # 读取下一行
            continue
        tmp.append(msg.latitude)
        tmp.append(msg.longitude)
        locations.append(tmp)
        line = f.readline() # 读取下一行
    return locations


if __name__ == '__main__':

    file_path ='gps_data_60.txt'
    locate = parse_file(file_path)
    print(locate)

附件,gps文件数据:gps_data_60.txt

$GNRMC,023445.200,A,3448.72275,N,11331.95121,E,0.00,222.94,110424,,,A*7A
$GNRMC,023445.400,A,3448.72275,N,11331.95121,E,0.00,222.94,110424,,,A*7C
$GNRMC,023445.600,A,3448.72275,N,11331.95121,E,0.00,222.94,110424,,,A*7E
$GNRMC,023455.600,A,3448.72279,N,11331.95124,E,0.00,222.94,110424,,,A*76
$GNRMC,023455.800,A,3448.72279,N,11331.95124,E,0.00,222.94,110424,,,A*78
$GNRMC,023456.000,A,3448.72279,N,11331.95124,E,0.00,222.94,110424,,,A*73

 

posted on 2024-04-18 10:43  虚生  阅读(233)  评论(0编辑  收藏  举报