pflaenz.li/pflaenzli/pflaenzli/utils/distance.py

20 lines
483 B
Python
Raw Normal View History

2023-04-05 15:34:00 +02:00
from geopy.distance import distance
import os
from pandas import read_pickle
path = os.path.dirname(os.path.abspath(__file__))
df = read_pickle(os.path.join(path, 'plz.pkl'))
def calculate_distance(zip_1, zip_2):
2023-04-09 13:07:50 +02:00
if zip_1 == zip_2:
return None
2023-04-05 15:34:00 +02:00
zip_1_coords = tuple(df[df.index == zip_1].values)
zip_2_coords = tuple(df[df.index == zip_2].values)
2023-04-05 18:19:06 +02:00
dist = round(distance((zip_1_coords), (zip_2_coords)).kilometers)
2023-04-05 15:34:00 +02:00
return None if dist > 400 else dist