日常工作中有时会用到需要计算gnss定位模组的定位精确度,需要将被测设备和真值设备进行经纬度坐标之间的对比,由于经纬度坐标想要计算差值,需要涉及到坐标系的转换,计算方法比较复杂,geopy库很好的解决了这个问题,集成了大量的方法,可以做很多地理坐标相关的事情,其中就有计算两个坐标点之间距离的方法。
下面是我写的计算一系列坐标点之间的距离的python脚本,可以给大家提供参考。
整理出两个设备输出的gps的utc时间以及经纬度,按照下面格式写到txt文本中,脚本读取txt文本,进行整秒的经纬度进行比较,输出某一时间点的定位误差,单位为m。
locationA.txt
1641541189000 38.018449 119.205511
1641541190000 38.018492 119.205554
1641541191000 38.018551 119.205591
1641541192000 38.018589 119.205625
1641541193000 38.018629 119.205657
1641541194000 38.018660 119.205693
locationB.txt
1641541189000 33.018449 119.205511
1641541190000 33.018492 114.205554
1641541191000 38.018551 116.205591
1641541192000 38.018589 116.205625
1641541193000 38.018629 119.205657
1641541194000 38.018660 119.205693
diff.txt
1641541189000 1.0
1641541190000 2.0
1641541191000 3.0
1641541192000 3.0
1641541193000 4.0
1641541194000 1.0
postion.py
# -*- coding:utf-8 -*-
#!/usr/bin/python3
from geopy.distance import distance
import time
dictpostionA = {}
dictpostionB = {}
dictdiff = {}
#转换时间戳 dt为字符串
def datetime_timestamp(dt):
#中间过程,一般都需要将字符串转化为时间数组
time.strptime(dt, '%Y/%m/%d %H:%M:%S')
s = time.mktime(time.strptime(dt, '%Y/%m/%d %H:%M:%S'))
return int(s)
def saveATempData(filename):
with open(filename, "w") as f:
for time, pos in dictpostionA.items():
data = str(time) + "\t" + pos[0] + "\t" + pos[1] + "\n"
#print(data)
f.write(data)
#读取excel数据转成txt
def readAData(filename):
with open(filename, "r") as f:
for line in f.readlines():
line = line.strip('\n')
#print(line)
poslist = line.split()
#print(poslist[0])
#print(poslist[1])
#print(poslist[2])
#print(poslist[3])
#print(poslist[4])
#print(poslist[5])
dt = poslist[0]+' '+poslist[1]+':'+poslist[2]+':'+poslist[3]
#print(dt)
index = datetime_timestamp(dt) * 1000
#print(index)
sublist = poslist[4:6]
#print(sublist)
dictpostionA[index] = tuple(sublist)
def readBData(filename):
with open(filename, "r") as f:
for line in f.readlines():
line = line.strip('\n')
#print(line)
poslist = line.split()
#print(poslist[0])
#print(poslist[1])
#print(poslist[2])
sublist = poslist[1:3]
#print(sublist)
index = int(poslist[0])
dictpostionB[index] = tuple(sublist)
def processData():
for timeA, posA in dictpostionA.items():
for timeB, posB in dictpostionB.items():
#print(timeA, timeB)
if timeA == timeB:
d = distance(posA, posB).m # m是单位
print(timeA, d)
dictdiff[timeA] = d
continue
def saveDiffData(filename):
with open(filename, "w") as f:
for time, diff in dictdiff.items():
data = str(time) + "\t" + str(diff) + "\n"
#print(data)
f.write(data)
#读取数据
readAData("locationA.txt")
saveATempData("locationA_temp.txt")
readBData("locationB.txt")
#处理数据
processData()
#保存数据
saveDiffData("diff.txt")