Skip to content

Commit 31426a7

Browse files
committed
Refactoring and input validation of nearest_* fns
1 parent e0e9672 commit 31426a7

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pip install .
278278

279279
## Version History
280280

281-
* 0.6.0: `nearest_*` functions now use R-Trees for much faster lookups. Added `nearest_*_with_dist`functions. Updated address and placename data. (22/11/2025)
281+
* 0.6.0: `nearest_*` functions now use R-Trees for much faster lookups. Added `nearest_*_with_dist` functions. Updated address and placename data. (22/11/2025)
282282
* 0.5.10: Updated address and placename data. Added `region_for_postcode` function. Minor optimizations (07/11/2025)
283283
* 0.5.9: Updated address and placename data. Fixed bug in `placename_lookup` function (31/07/2025)
284284
* 0.5.8: Updated address and placename data. Added municipality name data to address records. Now requires Python 3.9+ (26/02/2025)

src/iceaddr/addresses.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,7 @@ def nearest_addr(
199199
) -> list[dict[str, Any]]:
200200
"""Find the address closest to the given coordinates."""
201201

202-
results_with_dist = find_nearest(
203-
lat=lat,
204-
lon=lon,
205-
rtree_table="stadfong_rtree",
206-
main_table="stadfong",
207-
id_column="hnitnum",
208-
limit=limit,
209-
max_dist=max_dist,
210-
post_process=_postprocess_addr,
211-
)
202+
results_with_dist = nearest_addr_with_dist(lat=lat, lon=lon, limit=limit, max_dist=max_dist)
212203

213204
# Strip out distances for backward compatibility
214205
return [addr for addr, _dist in results_with_dist]
@@ -224,6 +215,12 @@ def nearest_addr_with_dist(
224215
- float: Distance from the search point in kilometers
225216
"""
226217

218+
if lat > 90.0 or lat < -90.0 or lon > 180.0 or lon < -180.0:
219+
raise ValueError("Invalid latitude or longitude value: {}, {}".format(lat, lon))
220+
221+
if limit < 0 or max_dist < 0.0:
222+
raise ValueError("limit and max_dist must be non-negative")
223+
227224
return find_nearest(
228225
lat=lat,
229226
lon=lon,

src/iceaddr/placenames.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,9 @@ def nearest_placenames(
9393
lat: float, lon: float, limit: int = 1, max_dist: float = 0.0
9494
) -> list[dict[str, Any]]:
9595
"""Find the placename closest to the given coordinates."""
96-
results_with_dist = find_nearest(
97-
lat=lat,
98-
lon=lon,
99-
rtree_table="ornefni_rtree",
100-
main_table="ornefni",
101-
id_column="id",
102-
limit=limit,
103-
max_dist=max_dist,
104-
post_process=None, # No extra processing needed for placenames
96+
97+
results_with_dist = nearest_placenames_with_dist(
98+
lat=lat, lon=lon, limit=limit, max_dist=max_dist
10599
)
106100

107101
# Strip out distances for backward compatibility
@@ -117,6 +111,13 @@ def nearest_placenames_with_dist(
117111
- dict: Placename information
118112
- float: Distance from the search point in kilometers
119113
"""
114+
115+
if lat > 90.0 or lat < -90.0 or lon > 180.0 or lon < -180.0:
116+
raise ValueError("Invalid latitude or longitude value: {}, {}".format(lat, lon))
117+
118+
if limit < 0 or max_dist < 0.0:
119+
raise ValueError("limit and max_dist must be non-negative")
120+
120121
return find_nearest(
121122
lat=lat,
122123
lon=lon,

0 commit comments

Comments
 (0)