Skip to content

Commit cb8b49a

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

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

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)