41 lines
909 B
Python
41 lines
909 B
Python
from typing import Union
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
import yaml
|
|
import geopy.distance
|
|
|
|
|
|
DATE_FORMAT = "%Y-%m-%d"
|
|
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
|
|
|
|
|
def read_yaml(path: Union[str, Path], encoding: str = "utf-8") -> dict:
|
|
"""Read content of yaml file.
|
|
|
|
Args:
|
|
path (Union[str, Path]): Path to file.
|
|
encoding (str, optional): Defaults to "utf-8".
|
|
|
|
Returns:
|
|
dict: File content.
|
|
"""
|
|
with open(path, "r", encoding=encoding) as f:
|
|
result = yaml.safe_load(f)
|
|
return result
|
|
|
|
|
|
def direct_distance(row: dict) -> float:
|
|
"""Direct distance between two points.
|
|
|
|
Args:
|
|
row (dict): Dictionary with from_lat/lon, to_lat/lon fields.
|
|
|
|
Returns:
|
|
float: Distance in km.
|
|
"""
|
|
return geopy.distance.geodesic(
|
|
(row["from_lat"], row["from_lon"]),
|
|
(row["to_lat"], row["to_lon"])
|
|
).km
|