diff options
| author | Alex Schofield <git@ajschof.me> | 2026-05-04 21:36:01 +0100 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2026-05-04 21:36:01 +0100 |
| commit | f7b0f039ad955b661b3c25400e1447569efd882b (patch) | |
| tree | d0825d36c77c459b50c8cc9957304c5cdbb74764 /fnme/station.py | |
| parent | 25d13201617acbbbb5adba1df6743b4b2c8562ee (diff) | |
| download | fuelnearme-f7b0f039ad955b661b3c25400e1447569efd882b.tar.gz fuelnearme-f7b0f039ad955b661b3c25400e1447569efd882b.zip | |
promote nested functions in process_stations to private helpers
Diffstat (limited to 'fnme/station.py')
| -rw-r--r-- | fnme/station.py | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/fnme/station.py b/fnme/station.py index 109b927..94d965a 100644 --- a/fnme/station.py +++ b/fnme/station.py @@ -9,36 +9,42 @@ from fnme.constants import SORT_KV _PRICE_KEYS = ("e5_price", "e10_price", "diesel_price") +def _bounding_box( + dframe: pd.DataFrame, loc: Tuple[float, float], rad: int +) -> pd.DataFrame: + lat, lon = loc + deg_lat = rad / 69.0 + deg_lon = rad / (69.0 * math.cos(math.radians(lat))) + return dframe[ + dframe["forecourts.location.latitude"].between(lat - deg_lat, lat + deg_lat) + & dframe["forecourts.location.longitude"].between(lon - deg_lon, lon + deg_lon) + ] + + +def _haversine_miles( + loc: Tuple[float, float], lat2: np.ndarray, lon2: np.ndarray +) -> np.ndarray: + R = 3958.8 + lat1, lon1 = np.radians(loc[0]), np.radians(loc[1]) + lat2, lon2 = np.radians(lat2), np.radians(lon2) + dlat = lat2 - lat1 + dlon = lon2 - lon1 + a = np.sin(dlat / 2) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2) ** 2 + return R * 2 * np.arcsin(np.sqrt(a)) + + +def _pence_to_pounds(col: pd.Series) -> pd.Series: + return (col / 100).round(2) + + def process_stations( dframe: pd.DataFrame, rad: int, loc: Tuple[float, float] ) -> List[Dict[str, Any]]: - def bounding_box() -> pd.DataFrame: - lat, lon = loc - deg_lat = rad / 69.0 - deg_lon = rad / (69.0 * math.cos(math.radians(lat))) - return dframe[ - dframe["forecourts.location.latitude"].between(lat - deg_lat, lat + deg_lat) - & dframe["forecourts.location.longitude"].between( - lon - deg_lon, lon + deg_lon - ) - ] - - def haversine_miles(lat2: np.ndarray, lon2: np.ndarray) -> np.ndarray: - R = 3958.8 - lat1, lon1 = np.radians(loc[0]), np.radians(loc[1]) - lat2, lon2 = np.radians(lat2), np.radians(lon2) - dlat = lat2 - lat1 - dlon = lon2 - lon1 - a = np.sin(dlat / 2) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2) ** 2 - return R * 2 * np.arcsin(np.sqrt(a)) - - def pence_to_pounds(col: pd.Series) -> pd.Series: - return (col / 100).round(2) - - df = bounding_box().copy() - - df["distance"] = haversine_miles( + df = _bounding_box(dframe, loc, rad).copy() + + df["distance"] = _haversine_miles( + loc, df["forecourts.location.latitude"].to_numpy(), df["forecourts.location.longitude"].to_numpy(), ).round(1) @@ -46,9 +52,9 @@ def process_stations( df = df[df["distance"] < rad] df = df.assign( - e5_price=pence_to_pounds(df["forecourts.fuel_price.E5"]), - e10_price=pence_to_pounds(df["forecourts.fuel_price.E10"]), - diesel_price=pence_to_pounds(df["forecourts.fuel_price.B7S"]), + e5_price=_pence_to_pounds(df["forecourts.fuel_price.E5"]), + e10_price=_pence_to_pounds(df["forecourts.fuel_price.E10"]), + diesel_price=_pence_to_pounds(df["forecourts.fuel_price.B7S"]), ) records = df.rename(columns={"forecourts.trading_name": "station_name"})[ |
