add forward azimuth, change to pyproj
This commit is contained in:
13
citydist.py
13
citydist.py
@@ -1,6 +1,6 @@
|
|||||||
import pandas as pd
|
|
||||||
import numpy as np
|
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):
|
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:
|
if df is None:
|
||||||
df = pd.read_csv("africapolis.csv", index_col=0)
|
df = pd.read_csv("africapolis.csv", index_col=0)
|
||||||
df = df[df["Population"] > population_threshold]
|
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()
|
min_index = distances.idxmin()
|
||||||
city = df.loc[min_index]
|
city = df.loc[min_index]
|
||||||
return {
|
return {
|
||||||
"name": city["Name"],
|
"name": city["Name"],
|
||||||
"population": np.int64(city["Population"]),
|
"population": np.int64(city["Population"]),
|
||||||
"distance": np.float64(distances[min_index].km),
|
"distance": np.float64(distances[min_index]),
|
||||||
"location": (city["Latitude"], city["Longitude"])
|
"location": (city["Latitude"], city["Longitude"]),
|
||||||
|
"fwd_azimuth": fwd_azimuth[min_index],
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user