def haversine(coord1, coord2):
# Calculate distance between coordinates on Earth's surface
R = 6372800 # Earth radius in meters
lat1, lon1 = coord1
lat2, lon2 = coord2
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dphi = math.radians(lat2 - lat1)
dlambda = math.radians(lon2 - lon1)
a = (
math.sin(dphi / 2) ** 2
+ math.cos(phi1) * math.cos(phi2) * math.sin(dlambda / 2) ** 2
)
return 2 * R * math.atan2(math.sqrt(a), math.sqrt(1 - a))
def define_data(coordinates: list, names: list = None) -> tuple[list, pd.DataFrame]:
# Get two dataframes (one with the properly coordinates, one with the distances) given a list of
# coordinates
df = pd.DataFrame.from_records(
coordinates, columns=["latitude", "longitude"], index=names
)
df.index.name = "NODE"
nodes = list(df.index)
distances = {
(orig, dest): [
round(
haversine(
(df.loc[orig]["latitude"], df.loc[orig]["longitude"]),
(df.loc[dest]["latitude"], df.loc[dest]["longitude"]),
)
/ 1000
)
]
for orig in nodes
for dest in nodes
if nodes.index(dest) > nodes.index(orig)
}
return (df, distances)
def sample_data():
# Get some sample data
otherLocs = [
{
"name": "Bolzano Airport",
"city": "Bolzano",
"country": "Italy",
"iata": "BZO",
"latitude": 46.4626,
"longitude": 11.3264,
},
{
"name": "Ottawa Macdonald-Cartier International Airport",
"city": "Ottawa",
"country": "Canada",
"iata": "YOW",
"latitude": 45.3208,
"longitude": -75.6724,
},
{
"name": "Porto Airport",
"city": "Porto",
"country": "Portugal",
"iata": "OPO",
"latitude": 41.2481,
"longitude": -8.681389,
},
{
"name": "Newark Liberty International Airport",
"city": "Newark",
"country": "United States",
"iata": "EWR",
"latitude": 40.6925,
"longitude": -74.168611,
},
{
"name": "Madrid Barajas Airport",
"city": "Madrid",
"country": "Spain",
"iata": "MAD",
"latitude": 40.493556,
"longitude": -3.566764,
},
{
"name": "Chicago O'Hare International Airport",
"city": "Chicago",
"country": "United States",
"iata": "ORD",
"latitude": 41.978611,
"longitude": -87.904722,
},
{
"name": "Albuquerque International Sunport",
"city": "Albuquerque",
"country": "United States",
"iata": "ABQ",
"latitude": 35.0401,
"longitude": -106.6094,
},
{
"name": "Melbourne Airport",
"city": "Melbourne",
"country": "Australia",
"iata": "MEL",
"latitude": -37.673333,
"longitude": 144.843333,
},
{
"name": "San Francisco International Airport",
"city": "San Francisco",
"country": "United States",
"iata": "SFO",
"latitude": 37.618889,
"longitude": -122.375,
},
]
(coords, distances) = define_data(
[(c["latitude"], c["longitude"]) for c in otherLocs],
[c["city"] for c in otherLocs],
)
return (coords, distances)