D
D
Danil Ekimov2019-07-11 11:44:20
Python
Danil Ekimov, 2019-07-11 11:44:20

How to calculate the distance between gps coordinates and their azimuth (or rhumb)?

Good afternoon, now I am writing a program to fill in the documentation for the kml file, but I have problems...

import urllib.request
import math
from pykml import parser

cos = math.cos
sqrt = math.sqrt
sin = math.sin
arctan = math.atan
asin = math.asin
acos = math.acos
radians = math.radians
def toFixed(numObj, digits=0):
    return f"{numObj:.{digits}f}"

def haversine(lat1, lon1, lat2, lon2):
    # Конвертируем в список
    lon1, lat1, lon2, lat2 = map(radians, (lon1, lat1, lon2, lat2))

    # Формула
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    km = 6367 * c
    km = km*1000
    km = toFixed(km, '2')
    print ('Расстояние в метрах:', km)

url = 'https://berezca.com/test.kml'
fileobject = urllib.request.urlopen(url)
root = parser.parse(fileobject).getroot()
i = 0
print('=======НАЧАЛА ОБРАБОТКИ==========')
while i<20:
    try:
        namet = (str(f'Имя точки: {root.Document.Folder.Placemark[i].name}'))
        coordt = str(root.Document.Folder.Placemark[i].Point.coordinates).split(',')
        coordt2 =  str(root.Document.Folder.Placemark[i+1].Point.coordinates).split(',')
        sh1= (float(coordt[0]))
        dg1= (float(coordt[1]))
        sh2= (float(coordt2[0]))
        dg2= (float(coordt2[1]))
        print (namet)
        print (coordt[0], coordt[1])
        haversine(sh1,dg1,sh2,dg2)
        i = i+1
    except (IndexError):
        if i == 0 or i >= 5:
            tchk = 'точек'
        elif i == 1:
            tchk = 'точка'
        elif i == 2 or i == 3:
            tchk = 'точки'
        print ('Найдено:',(i+1),tchk)
        print('========КОНЕЦ ОБРАБОТКИ========')
        break

The fact that the cat is not so hot - I know, but now is not about that.
The program normally accepts the file and displays information about the points, that is, it parses their names / coordinates and displays the length between the points, so there were problems with it, I already went through a dozen formulas, now I found a function in the public put it in my code, but the output is me not happy, let's say between the 1st and 2nd point it should turn out 147.20, and the program displays 250.50 ...
5d26f6a9f3488213785931.png
(I didn’t enter the length, between the first and last)
I would also like to figure out the azimuths, but I think if this question decide, then I will quickly deal with them, thanks
Help, please (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-07-11
@NtOwl

I don’t know where you got 147.20 from - there is less than 250, well, it just doesn’t work out. The formula is correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question