I try to calculate distance between points from gpx file using harvesine formula. I stuck in moment when I should use 'lats' and 'lons' for formula which I found in internet. I want to use that two list as pairs, so zip function would be ok. But how i should do that?
import xml.etree.ElementTree as ET
import math
f = ('St_Louis_Zoo_sample.gpx')
p = ET.parse(f)
root = p.getroot()
lats=[]
lons=[]
for mainElement in root.findall('{http://www.topografix.com/GPX/1/1}wpt'):
y = float(mainElement.attrib['lat'])
x = float(mainElement.attrib['lon'])
lats.append(y)
lons.append(x)
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d