@@ -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.
0 commit comments