]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/api/lookup.py
switch details cli command to new Python implementation
[nominatim.git] / nominatim / api / lookup.py
index cb9f9839ea2722964a15ea9fce2ba0804161949a..56a41310d78e48a84de4261a7cb106410e8fb7f4 100644 (file)
@@ -95,8 +95,8 @@ async def find_in_osmline(conn: SearchConnection, place: ntyp.PlaceRef,
 
 async def find_in_tiger(conn: SearchConnection, place: ntyp.PlaceRef,
                         details: ntyp.LookupDetails) -> Optional[SaRow]:
-    """ Search for the given place in table of Tiger addresses and return the
-        base information.
+    """ Search for the given place in the table of Tiger addresses and return
+        the base information. Only lookup by place ID is supported.
     """
     t = conn.t.tiger
     sql = sa.select(t.c.place_id, t.c.parent_place_id,
@@ -114,6 +114,27 @@ async def find_in_tiger(conn: SearchConnection, place: ntyp.PlaceRef,
     return (await conn.execute(sql)).one_or_none()
 
 
+async def find_in_postcode(conn: SearchConnection, place: ntyp.PlaceRef,
+                           details: ntyp.LookupDetails) -> Optional[SaRow]:
+    """ Search for the given place in the postcode table and return the
+        base information. Only lookup by place ID is supported.
+    """
+    t = conn.t.postcode
+    sql = sa.select(t.c.place_id, t.c.parent_place_id,
+                    t.c.rank_search, t.c.rank_address,
+                    t.c.indexed_date, t.c.postcode, t.c.country_code,
+                    sa.func.ST_X(t.c.geometry).label('x'),
+                    sa.func.ST_Y(t.c.geometry).label('y'),
+                    _select_column_geometry(t.c.geometry, details.geometry_output))
+
+    if isinstance(place, ntyp.PlaceID):
+        sql = sql.where(t.c.place_id == place.place_id)
+    else:
+        return None
+
+    return (await conn.execute(sql)).one_or_none()
+
+
 async def get_place_by_id(conn: SearchConnection, place: ntyp.PlaceRef,
                           details: ntyp.LookupDetails) -> Optional[nres.SearchResult]:
     """ Retrieve a place with additional details from the database.
@@ -133,6 +154,12 @@ async def get_place_by_id(conn: SearchConnection, place: ntyp.PlaceRef,
         await nres.add_result_details(conn, result, details)
         return result
 
+    row = await find_in_postcode(conn, place, details)
+    if row is not None:
+        result = nres.create_from_postcode_row(row)
+        await nres.add_result_details(conn, result, details)
+        return result
+
     row = await find_in_tiger(conn, place, details)
     if row is not None:
         result = nres.create_from_tiger_row(row)