使用pynmea2分析处理GNSS定位的NMEA语句

最近在做高精度卫星定位的项目,经常用到GNSS,需要分析处理NMEA语句,计算与标定点的位置误差,直接上代码:

import pynmea2
import csv
from geopy.distance import geodesic

# 基准坐标  E121°21′29.9967″  N31°07′40.4142″ 将度分秒的格式转换为度的格式
lat1 = 31.1278928
lng1 = 121.3583324

# 打开记录nmea语句的文本文件
file = open('nmea.txt', encoding='utf-8')
data = file.readlines()
file.close()
# 1. 创建保存分析数据的csv文件
f = open('data.csv', 'w', encoding='utf-8-sig', newline='')
# 2. 基于文件对象构建 csv写入对象
csv_writer = csv.writer(f)
# 3. 构建列表头
csv_writer.writerow(['time_beijing', 'lat', 'lng', 'altitude', 'distance', 'FixMode', 'satellites used', 'HDOP', 'ref_station_id'])

for line in data:
    try:
        if line.startswith('$GNGGA'):
            record = pynmea2.parse(line)
            gps_time = str(record.timestamp)
            gps_time_beijing = int(gps_time[0:2]) * 10000 + int(gps_time[3:5]) * 100 + int(gps_time[6:8]) + 80000
            if gps_time_beijing > 240000:
                gps_time_beijing = gps_time_beijing - 240000
            fix_mode = record.gps_qual  #定位模式,1->普通单点定位;2->差分定位;4->固定解;5->浮点解;
            num_satallites_used = record.num_sats #定位时卫星使用个数
            latitude = record.latitude   #纬度
            longitude = record.longitude  #经度
            altitude = record.altitude  #高度
            distance_1 = '%.3f' % geodesic((latitude, longitude), (lat1, lng1)).m  # 与基准点的距离
            hdop = record.horizontal_dil  #水平精度因子
            ref_station_id = record.ref_station_id #差分钟ID,使用RTK定位时才有
            print('timestamp_origin:', gps_time)
            print('timestamp_beijing:', gps_time_beijing)
            print('Fix Mode: ', fix_mode)
            print('Numbers of Satallites used:', num_satallites_used)
            print('Latitude: ', latitude)
            print('Longitude: ', longitude)
            print('hdop:', hdop)
            print('distance:', distance_1)
            csv_writer.writerow([gps_time_beijing, latitude, longitude, altitude, distance_1, fix_mode, num_satallites_used, hdop, ref_station_id])
    except pynmea2.nmea.ParseError:
        print('NMEA wrong')

附上2秒的NMEA原始语句
$GNRMC,084819.000,A,3107.5595974,N,12121.5648169,E,0.108,198.960,150622,F,S03
$GNGGA,084819.000,3107.5595974,N,12121.5648169,E,5,28,0.629,8.134,M,9.509,M,2,3347
44
$GNGSA,A,3,5,13,20,2,11,196,29,199,195,30,6,1.154,0.629,0.967,13E
$GNGSA,A,3,27,3,41,28,8,1,13,39,30,38,33,32,1.154,0.629,0.967,4
01
$GNGSA,A,3,13,8,15,1.154,0.629,0.967,309
$GPGSV,4,1,16,20,59,11,43,2,72,58,42,5,54,304,49,6,30,107,25,1
59
$GPGSV,4,2,16,29,27,313,38,30,18,97,26,11,64,67,41,195,65,150,33,15D
$GPGSV,4,3,16,199,53,169,33,194,5,165,34,13,49,183,45,196,77,79,42,1
53
$GPGSV,4,4,16,7,12,68,7,19,4,157,17,12,5,227,17,15,19,215,24,15C
$GPGSV,2,1,7,6,30,107,34,30,18,97,24,11,64,67,30,195,65,150,32,8
54
$GPGSV,2,2,7,199,53,169,28,194,5,165,31,196,77,79,32,851
$GBGSV,5,1,18,13,48,325,39,28,44,151,43,32,45,251,27,30,25,317,38,1
7A
$GBGSV,5,2,18,38,61,5,34,33,20,45,33,39,36,175,38,16,27,178,22,14E
$GBGSV,5,3,18,41,67,9,44,27,76,287,46,1,46,139,41,10,25,221,18,1
4C
$GBGSV,5,4,18,40,17,197,28,2,27,5,20,4,43,17A
$GBGSV,5,5,18,3,54,201,45,8,53,342,42,1
72
$GBGSV,3,1,9,28,44,151,30,32,45,251,21,30,25,317,25,38,61,5,35,549
$GBGSV,3,2,9,33,20,45,26,39,36,175,27,41,67,9,39,27,76,287,35,5
7C
$GBGSV,3,3,9,40,17,197,14,573
$GAGSV,2,1,6,3,40,45,40,7,28,212,37,34,11,122,17,8,79,168,43,7
42
$GAGSV,2,2,6,13,56,334,43,15,58,98,22,77F
$GAGSV,1,1,4,3,40,45,32,7,28,212,14,8,79,168,35,13,56,334,35,1
43
$GNGST,084819.000,0.700,1.152,1.70576
$GNSVD,-1,-5,-31,7,6,7
55
$GNRMC,084820.000,A,3107.5596148,N,12121.5648963,E,0.134,250.058,150622,F,S05
$GNGGA,084820.000,3107.5596148,N,12121.5648963,E,5,30,0.576,8.185,M,9.509,M,2,3347
42
$GNGSA,A,3,5,13,2,29,11,20,196,199,195,30,6,7,1.028,0.576,0.851,10E
$GNGSA,A,3,27,3,41,28,13,8,1,30,39,38,32,40,1.028,0.576,0.851,4
02
$GNGSA,A,3,13,8,15,1.028,0.576,0.851,30E
$GPGSV,4,1,16,20,59,11,40,2,72,58,42,5,54,304,49,6,30,107,25,1
5A
$GPGSV,4,2,16,29,27,313,42,30,18,97,25,11,64,67,41,195,65,150,33,153
$GPGSV,4,3,16,199,53,169,34,194,5,165,34,13,49,183,45,196,77,79,42,1
54
$GPGSV,4,4,16,7,12,68,21,19,4,157,16,12,5,227,16,15,19,215,22,16E
$GPGSV,2,1,7,6,30,107,34,30,18,97,25,11,64,67,31,195,65,150,32,8
54
$GPGSV,2,2,7,199,53,169,28,194,5,165,32,196,77,79,32,852
$GBGSV,5,1,18,13,48,325,42,28,44,151,43,32,45,251,28,30,25,317,39,1
78
$GBGSV,5,2,18,38,61,5,31,33,20,45,33,39,36,175,38,16,27,178,22,14B
$GBGSV,5,3,18,41,67,9,44,27,76,287,46,1,46,139,41,10,25,221,23,1
44
$GBGSV,5,4,18,40,17,197,28,2,25,5,13,4,43,178
$GBGSV,5,5,18,3,54,201,45,8,53,342,42,1
72
$GBGSV,3,1,9,28,44,151,30,32,45,251,21,30,25,317,24,38,61,5,35,548
$GBGSV,3,2,9,33,20,45,26,39,36,175,28,41,67,9,39,27,76,287,35,5
73
$GBGSV,3,3,9,40,17,197,14,573
$GAGSV,2,1,6,3,40,45,39,7,28,212,37,34,11,122,14,8,79,168,43,7
4F
$GAGSV,2,2,6,13,56,334,43,15,58,98,23,77E
$GAGSV,1,1,4,3,40,45,33,7,28,212,14,8,79,168,35,13,56,334,35,1
42
$GNGST,084820.000,0.695,1.142,1.71074
$GNSVD,-6,-2,-24,6,6,7
50

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值