From 3febf1cab47212a363852f7f49866849d95300c3 Mon Sep 17 00:00:00 2001 From: craisin Date: Sun, 15 Feb 2026 13:59:31 -0800 Subject: [PATCH] add forward azimuth, change to pyproj --- citydist.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/citydist.py b/citydist.py index 786ccb8..1d486ac 100644 --- a/citydist.py +++ b/citydist.py @@ -1,6 +1,6 @@ -import pandas as pd import numpy as np -from geopy.distance import geodesic +import pandas as pd +from pyproj import Geod def closest_city(point, df=None, population_threshold=0): @@ -9,12 +9,15 @@ def closest_city(point, df=None, population_threshold=0): if df is None: df = pd.read_csv("africapolis.csv", index_col=0) df = df[df["Population"] > population_threshold] - distances = df.apply(lambda row: geodesic(point, (row["Latitude"], row["Longitude"])), axis=1) + geod = Geod(ellps="WGS84") + pos = df.apply(lambda row: geod.inv(point[1], point[0], row["Longitude"], row["Latitude"]), axis=1) + fwd_azimuth, distances = pos.apply(lambda row: row[0]), pos.apply(lambda row: row[2]) min_index = distances.idxmin() city = df.loc[min_index] return { "name": city["Name"], "population": np.int64(city["Population"]), - "distance": np.float64(distances[min_index].km), - "location": (city["Latitude"], city["Longitude"]) + "distance": np.float64(distances[min_index]), + "location": (city["Latitude"], city["Longitude"]), + "fwd_azimuth": fwd_azimuth[min_index], } \ No newline at end of file