Skip to content

Commit 3506016

Browse files
committed
reverse: only return housenumbers near street
1 parent 4cfc179 commit 3506016

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/nominatim_api/reverse.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ async def _find_closest_street_or_pois(self, distance: float,
210210
sa.func.first_value(inner.c.distance)
211211
.over(order_by=inner.c.distance)
212212
.label('_min_distance'),
213-
sa.func.first_value(inner.c._geometry.ST_ClosestPoint(WKT_PARAM))
213+
sa.func.first_value(
214+
sa.case((inner.c.rank_search <= 27,
215+
inner.c._geometry.ST_ClosestPoint(WKT_PARAM)),
216+
else_=None))
214217
.over(order_by=inner.c.distance)
215218
.label('_closest_point'),
216219
sa.func.first_value(sa.case((sa.or_(inner.c.rank_search <= 27,
@@ -221,8 +224,10 @@ async def _find_closest_street_or_pois(self, distance: float,
221224
.subquery()
222225

223226
outer = sa.select(*(c for c in windowed.c if not c.key.startswith('_')),
224-
windowed.c.centroid.ST_Distance(windowed.c._closest_point)
225-
.label('best_distance'),
227+
sa.case((sa.or_(windowed.c._closest_point == None,
228+
windowed.c.housenumber == None), None),
229+
else_=windowed.c.centroid.ST_Distance(windowed.c._closest_point))
230+
.label('distance_from_best'),
226231
sa.case((sa.or_(windowed.c._best_geometry == None,
227232
windowed.c.rank_search <= 27,
228233
windowed.c.osm_type != 'N'), False),
@@ -337,13 +342,13 @@ async def lookup_street_poi(self) -> Tuple[Optional[SaRow], RowFunc]:
337342
# If the closest result was a street but an address was requested,
338343
# see if we can refine the result with a housenumber closeby.
339344
elif parent_street is not None \
340-
and row.rank_address > 27 \
341-
and row.best_distance < 0.001 \
342-
and (hnr_distance is None or hnr_distance > row.best_distance) \
345+
and row.distance_from_best is not None \
346+
and row.distance_from_best < 0.001 \
347+
and (hnr_distance is None or hnr_distance > row.distance_from_best) \
343348
and row.parent_place_id == parent_street:
344349
log().var_dump('Housenumber to closest result', row)
345350
result = row
346-
hnr_distance = row.best_distance
351+
hnr_distance = row.distance_from_best
347352
distance = row.distance
348353
# If the closest object is inside an area, then check if there is
349354
# a POI nearby and return that with preference.

test/bdd/features/db/query/reverse.feature

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,32 @@ Feature: Reverse searches
99
And the places
1010
| osm | class | type | geometry |
1111
| W1 | aeroway | terminal | (1,2,3,4,1) |
12-
| N1 | amenity | restaurant | 9 |
12+
| N9 | amenity | restaurant | 9 |
1313
When importing
1414
And reverse geocoding 1.0001,1.0001
1515
Then the result contains
1616
| object |
17-
| N1 |
17+
| N9 |
1818
When reverse geocoding 1.0003,1.0001
1919
Then the result contains
2020
| object |
2121
| W1 |
22+
23+
24+
Scenario: Find closest housenumber for street matches
25+
Given the 0.0001 grid with origin 1,1
26+
| | 1 | | |
27+
| | | 2 | |
28+
| 10 | | | 11 |
29+
And the places
30+
| osm | class | type | name | geometry |
31+
| W1 | highway | service | Goose Drive | 10,11 |
32+
| N2 | tourism | art_work | Beauty | 2 |
33+
And the places
34+
| osm | class | type | housenr | geometry |
35+
| N1 | place | house | 23 | 1 |
36+
When importing
37+
When reverse geocoding 1.0002,1.0002
38+
Then the result contains
39+
| object |
40+
| N1 |

0 commit comments

Comments
 (0)