You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
959 B

import csv
from latlon import LatLon, Latitude, Longitude
import math
# https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude#43211266
# https://pypi.org/project/latlon3/
airports = []
with open("airports.csv") as airport_file:
for port in csv.reader(airport_file):
airports.append(port)
with open("routes.csv") as routes_file:
with open("new.csv", "w") as routes2_file:
routes = csv.reader(routes_file)
new = csv.writer(routes2_file)
next(routes, None)
for route in routes:
p1 = None
p2 = None
for port in airports:
if port[4] == route[3]:
p1 = port
if port[4] == route[5]:
p2 = port
d1 = None
d2 = None
try:
d1 = LatLon(Latitude(float(p1[6])), Longitude(float(p1[7])))
d2 = LatLon(Latitude(float(p2[6])), Longitude(float(p2[7])))
except:
continue
route[9] = d1.distance(d2)
if str(route[9]) != "nan":
new.writerow(route)