current position:Home>Using the python library geopy method calculating the distance between the multiple sets of longitude and latitude

Using the python library geopy method calculating the distance between the multiple sets of longitude and latitude

2022-08-06 07:06:10Mortal Leaf Maple

Calculations are sometimes used in daily workgnssThe positioning accuracy of the positioning module,It is necessary to compare the latitude and longitude coordinates of the device under test and the true value device,Since the latitude and longitude coordinates want to calculate the difference,It needs to involve the transformation of the coordinate system,计算方法比较复杂,geopyThe library solves this problem nicely,A large number of methods are integrated,There are a lot of things that can be done with geographic coordinates,One of them is to calculate the distance between two coordinate points.

Below is what I wrote to calculate the distance between a series of coordinate pointspython脚本,Can provide you with reference.
Sort out the output of the two devicesgps的utctime and latitude,Write in the following formattxt文本中,脚本读取txt文本,Do a full second latitude and longitude comparison,Output the positioning error at a certain point in time,单位为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")

copyright notice
author[Mortal Leaf Maple],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/218/202208060638528846.html

Random recommended