Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question