From 030984d49edb84e249ed4c8977b11a294d392894 Mon Sep 17 00:00:00 2001 From: Valentin Date: Sun, 13 Apr 2025 01:57:41 +0200 Subject: [PATCH 1/2] LOW: Inverse postal code search --- pgeocode.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pgeocode.py b/pgeocode.py index 8695a46..4859e7c 100644 --- a/pgeocode.py +++ b/pgeocode.py @@ -387,6 +387,32 @@ def query_location( return pd.DataFrame(columns=self._data.columns) + def query_geocode(self, lat: float, lon: float): + """Get locations information from geo points + + Args: + lat (float): latitude + lon (float): longitude + + Returns: + pandas.DataFrame: a DataFrame with the relevant information + """ + distances = self._data_frame.apply( + lambda row: haversine_distance( + np.array([[lat, lon]]), np.array([[row["latitude"], row["longitude"]]]) + ), + axis=1, + ) + + # Get the index of the row with the smallest distance + nearest_index = distances.idxmin() + + # Return the nearest row + nearest_row = self._data_frame.loc[nearest_index].copy() + nearest_row['calculated_distance'] = distances[nearest_index][0] + + return nearest_row + def _str_contains_search(self, text: str, col: str) -> pd.DataFrame: match_mask = self._data[col].str.lower().str.contains(text.lower()) match_mask.fillna(False, inplace=True) From d3a1239002e9c6d2725c86750fbad3a60b0ef9a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Apr 2025 23:58:47 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pgeocode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pgeocode.py b/pgeocode.py index 4859e7c..5f8a510 100644 --- a/pgeocode.py +++ b/pgeocode.py @@ -395,7 +395,7 @@ def query_geocode(self, lat: float, lon: float): lon (float): longitude Returns: - pandas.DataFrame: a DataFrame with the relevant information + pandas.DataFrame: a DataFrame with the relevant information """ distances = self._data_frame.apply( lambda row: haversine_distance( @@ -409,7 +409,7 @@ def query_geocode(self, lat: float, lon: float): # Return the nearest row nearest_row = self._data_frame.loc[nearest_index].copy() - nearest_row['calculated_distance'] = distances[nearest_index][0] + nearest_row["calculated_distance"] = distances[nearest_index][0] return nearest_row