最近在做高精度卫星定位的项目,经常用到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,334744
$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,401
$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,159
$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,153
$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,854
$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,17A
$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,14C
$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,172
$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,57C
$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,742
$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,143
$GNGST,084819.000,0.700,1.152,1.70576
$GNSVD,-1,-5,-31,7,6,755
$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,334742
$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,402
$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,15A
$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,154
$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,854
$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,178
$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,144
$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,172
$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,573
$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,74F
$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,142
$GNGST,084820.000,0.695,1.142,1.71074
$GNSVD,-6,-2,-24,6,6,750