]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/api/reverse.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / api / reverse.py
index a2daee157eb032c943869f4c0cc0da7bf2883dae..5471455962e991aabf728aa8448568eff2bd876f 100644 (file)
@@ -175,7 +175,7 @@ class ReverseGeocoder:
         t = self.conn.t.placex
 
         # PostgreSQL must not get the distance as a parameter because
-        # there is a danger it won't be able to proberly estimate index use
+        # there is a danger it won't be able to properly estimate index use
         # when used with prepared statements
         diststr = sa.text(f"{distance}")
 
@@ -186,7 +186,7 @@ class ReverseGeocoder:
                 .where(sa.or_(sa.not_(t.c.geometry.is_area()),
                               t.c.centroid.ST_Distance(WKT_PARAM) < diststr))
                 .order_by('distance')
-                .limit(1))
+                .limit(2))
 
         if self.has_geometries():
             sql = self._add_geometry_columns(sql, t.c.geometry)
@@ -212,7 +212,20 @@ class ReverseGeocoder:
 
         sql = sql.where(sa.or_(*restrict))
 
-        return (await self.conn.execute(sql, self.bind_params)).one_or_none()
+        # If the closest object is inside an area, then check if there is a
+        # POI node nearby and return that.
+        prev_row = None
+        for row in await self.conn.execute(sql, self.bind_params):
+            if prev_row is None:
+                if row.rank_search <= 27 or row.osm_type == 'N' or row.distance > 0:
+                    return row
+                prev_row = row
+            else:
+                if row.rank_search > 27 and row.osm_type == 'N'\
+                   and row.distance < 0.0001:
+                    return row
+
+        return prev_row
 
 
     async def _find_housenumber_for_street(self, parent_place_id: int) -> Optional[SaRow]: