]> git.openstreetmap.org Git - nominatim.git/commitdiff
add lookup of tiger data
authorSarah Hoffmann <lonvia@denofr.de>
Thu, 2 Feb 2023 14:31:42 +0000 (15:31 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Sat, 4 Feb 2023 20:22:22 +0000 (21:22 +0100)
nominatim/api/lookup.py
nominatim/api/results.py
test/python/api/conftest.py
test/python/api/test_api_lookup.py

index 44380803d75565455d028c1ffe72059bd48859db..cb9f9839ea2722964a15ea9fce2ba0804161949a 100644 (file)
@@ -93,6 +93,27 @@ async def find_in_osmline(conn: SearchConnection, place: ntyp.PlaceRef,
     return (await conn.execute(sql)).one_or_none()
 
 
+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.
+    """
+    t = conn.t.tiger
+    sql = sa.select(t.c.place_id, t.c.parent_place_id,
+                    t.c.startnumber, t.c.endnumber, t.c.step,
+                    t.c.postcode,
+                    sa.func.ST_X(sa.func.ST_Centroid(t.c.linegeo)).label('x'),
+                    sa.func.ST_Y(sa.func.ST_Centroid(t.c.linegeo)).label('y'),
+                    _select_column_geometry(t.c.linegeo, 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.
@@ -102,13 +123,19 @@ async def get_place_by_id(conn: SearchConnection, place: ntyp.PlaceRef,
 
     row = await find_in_placex(conn, place, details)
     if row is not None:
-        result = nres.create_from_placex_row(row=row)
+        result = nres.create_from_placex_row(row)
         await nres.add_result_details(conn, result, details)
         return result
 
     row = await find_in_osmline(conn, place, details)
     if row is not None:
-        result = nres.create_from_osmline_row(row=row)
+        result = nres.create_from_osmline_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)
         await nres.add_result_details(conn, result, details)
         return result
 
index 107dcc26a6572803280af534f857f7e50949bcb9..7839859fbe37d6dc9b6ce2364b91a25925113022 100644 (file)
@@ -144,50 +144,63 @@ def create_from_placex_row(row: SaRow) -> SearchResult:
     """ Construct a new SearchResult and add the data from the result row
         from the placex table.
     """
-    result = SearchResult(source_table=SourceTable.PLACEX,
-                          place_id=row.place_id,
-                          parent_place_id=row.parent_place_id,
-                          linked_place_id=row.linked_place_id,
-                          osm_object=(row.osm_type, row.osm_id),
-                          category=(row.class_, row.type),
-                          admin_level=row.admin_level,
-                          names=row.name,
-                          address=row.address,
-                          extratags=row.extratags,
-                          housenumber=row.housenumber,
-                          postcode=row.postcode,
-                          wikipedia=row.wikipedia,
-                          rank_address=row.rank_address,
-                          rank_search=row.rank_search,
-                          importance=row.importance,
-                          country_code=row.country_code,
-                          indexed_date=getattr(row, 'indexed_date'),
-                          centroid=Point(row.x, row.y),
-                          geometry = _filter_geometries(row))
-
-    return result
+    return SearchResult(source_table=SourceTable.PLACEX,
+                        place_id=row.place_id,
+                        parent_place_id=row.parent_place_id,
+                        linked_place_id=row.linked_place_id,
+                        osm_object=(row.osm_type, row.osm_id),
+                        category=(row.class_, row.type),
+                        admin_level=row.admin_level,
+                        names=row.name,
+                        address=row.address,
+                        extratags=row.extratags,
+                        housenumber=row.housenumber,
+                        postcode=row.postcode,
+                        wikipedia=row.wikipedia,
+                        rank_address=row.rank_address,
+                        rank_search=row.rank_search,
+                        importance=row.importance,
+                        country_code=row.country_code,
+                        indexed_date=getattr(row, 'indexed_date'),
+                        centroid=Point(row.x, row.y),
+                        geometry=_filter_geometries(row))
 
 
 def create_from_osmline_row(row: SaRow) -> SearchResult:
     """ Construct a new SearchResult and add the data from the result row
         from the osmline table.
     """
-    result = SearchResult(source_table=SourceTable.OSMLINE,
-                          place_id=row.place_id,
-                          parent_place_id=row.parent_place_id,
-                          osm_object=('W', row.osm_id),
-                          category=('place', 'houses'),
-                          address=row.address,
-                          postcode=row.postcode,
-                          extratags={'startnumber': str(row.startnumber),
-                                     'endnumber': str(row.endnumber),
-                                     'step': str(row.step)},
-                          country_code=row.country_code,
-                          indexed_date=getattr(row, 'indexed_date'),
-                          centroid=Point(row.x, row.y),
-                          geometry = _filter_geometries(row))
-
-    return result
+    return SearchResult(source_table=SourceTable.OSMLINE,
+                        place_id=row.place_id,
+                        parent_place_id=row.parent_place_id,
+                        osm_object=('W', row.osm_id),
+                        category=('place', 'houses'),
+                        address=row.address,
+                        postcode=row.postcode,
+                        extratags={'startnumber': str(row.startnumber),
+                                   'endnumber': str(row.endnumber),
+                                   'step': str(row.step)},
+                        country_code=row.country_code,
+                        indexed_date=getattr(row, 'indexed_date'),
+                        centroid=Point(row.x, row.y),
+                        geometry=_filter_geometries(row))
+
+
+def create_from_tiger_row(row: SaRow) -> SearchResult:
+    """ Construct a new SearchResult and add the data from the result row
+        from the Tiger table.
+    """
+    return SearchResult(source_table=SourceTable.TIGER,
+                        place_id=row.place_id,
+                        parent_place_id=row.parent_place_id,
+                        category=('place', 'houses'),
+                        postcode=row.postcode,
+                        extratags={'startnumber': str(row.startnumber),
+                                   'endnumber': str(row.endnumber),
+                                   'step': str(row.step)},
+                        country_code='us',
+                        centroid=Point(row.x, row.y),
+                        geometry=_filter_geometries(row))
 
 
 async def add_result_details(conn: SearchConnection, result: SearchResult,
index 0164ee22cd5ea29c6d396097a513fd1f2e02c482..abd36bfb8d91846f2c559e5de2ad8cdbda853b93 100644 (file)
@@ -18,7 +18,7 @@ from nominatim.db.sql_preprocessor import SQLPreprocessor
 class APITester:
 
     def __init__(self):
-        self.api = napi.NominatimAPI(Path('/invalid'), {})
+        self.api = napi.NominatimAPI(Path('/invalid'))
         self.async_to_sync(self.api._async_api.setup_database())
 
 
@@ -93,6 +93,16 @@ class APITester:
                       'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
 
 
+    def add_tiger(self, **kw):
+        self.add_data('tiger',
+                     {'place_id': kw.get('place_id', 30000),
+                      'parent_place_id': kw.get('parent_place_id'),
+                      'startnumber': kw.get('startnumber', 2),
+                      'endnumber': kw.get('endnumber', 6),
+                      'step': kw.get('step', 2),
+                      'postcode': kw.get('postcode'),
+                      'linegeo': 'SRID=4326;' + kw.get('geometry', 'LINESTRING(1.1 -0.2, 1.09 -0.22)')})
+
     async def exec_async(self, sql, *args, **kwargs):
         async with self.api._async_api.begin() as conn:
             return await conn.execute(sql, *args, **kwargs)
@@ -104,9 +114,10 @@ class APITester:
 
 
 @pytest.fixture
-def apiobj(temp_db_with_extensions, temp_db_conn):
+def apiobj(temp_db_with_extensions, temp_db_conn, monkeypatch):
     """ Create an asynchronous SQLAlchemy engine for the test DB.
     """
+    monkeypatch.setenv('NOMINATIM_USE_US_TIGER_DATA', 'yes')
     testapi = APITester()
     testapi.async_to_sync(testapi.create_tables())
 
index c445085730f34f1b7a84e05b4b0785c93a1bbdb4..aa53dd6258e1ddec279227f57c51cb7c337945e4 100644 (file)
@@ -168,7 +168,6 @@ def test_lookup_placex_with_address_details(apiobj):
                                 names={'ref': 'pl'}, extratags={},
                                 admin_level=None, fromarea=True, isaddress=False,
                                 rank_address=4, distance=0.0)
-
            ]
 
 
@@ -370,7 +369,103 @@ def test_lookup_osmline_with_address_details(apiobj):
                                 names={'ref': 'pl'}, extratags={},
                                 admin_level=None, fromarea=True, isaddress=False,
                                 rank_address=4, distance=0.0)
+           ]
+
+
+def test_lookup_in_tiger(apiobj):
+    apiobj.add_tiger(place_id=4924,
+                     parent_place_id=12,
+                     startnumber=1, endnumber=4, step=1,
+                     postcode='34425',
+                     geometry='LINESTRING(23 34, 23 35)')
+
+    result = apiobj.api.lookup(napi.PlaceID(4924), napi.LookupDetails())
+
+    assert result is not None
+
+    assert result.source_table.name == 'TIGER'
+    assert result.category == ('place', 'houses')
+    assert result.centroid == (pytest.approx(23.0), pytest.approx(34.5))
+
+    assert result.place_id == 4924
+    assert result.parent_place_id == 12
+    assert result.linked_place_id is None
+    assert result.osm_object is None
+    assert result.admin_level == 15
+
+    assert result.names is None
+    assert result.address is None
+    assert result.extratags == {'startnumber': '1', 'endnumber': '4', 'step': '1'}
+
+    assert result.housenumber is None
+    assert result.postcode == '34425'
+    assert result.wikipedia is None
+
+    assert result.rank_search == 30
+    assert result.rank_address == 30
+    assert result.importance is None
+
+    assert result.country_code == 'us'
+    assert result.indexed_date is None
+
+    assert result.address_rows is None
+    assert result.linked_rows is None
+    assert result.parented_rows is None
+    assert result.name_keywords is None
+    assert result.address_keywords is None
+
+    assert result.geometry == {'type': 'ST_LineString'}
+
+
+def test_lookup_tiger_with_address_details(apiobj):
+    apiobj.add_tiger(place_id=9000,
+                     startnumber=2, endnumber=4, step=1,
+                     parent_place_id=332)
+    apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
+                     class_='highway', type='residential',  name='Street',
+                     country_code='us',
+                     rank_search=27, rank_address=26)
+    apiobj.add_address_placex(332, fromarea=False, isaddress=False,
+                              distance=0.0034,
+                              place_id=1000, osm_type='N', osm_id=3333,
+                              class_='place', type='suburb', name='Smallplace',
+                              country_code='us', admin_level=13,
+                              rank_search=24, rank_address=23)
+    apiobj.add_address_placex(332, fromarea=True, isaddress=True,
+                              place_id=1001, osm_type='N', osm_id=3334,
+                              class_='place', type='city', name='Bigplace',
+                              country_code='us',
+                              rank_search=17, rank_address=16)
 
+    result = apiobj.api.lookup(napi.PlaceID(9000),
+                               napi.LookupDetails(address_details=True))
+
+    assert result.address_rows == [
+               napi.AddressLine(place_id=None, osm_object=None,
+                                category=('place', 'house_number'),
+                                names={'ref': '2'}, extratags={},
+                                admin_level=None, fromarea=True, isaddress=True,
+                                rank_address=28, distance=0.0),
+               napi.AddressLine(place_id=332, osm_object=('W', 4),
+                                category=('highway', 'residential'),
+                                names={'name': 'Street'}, extratags={},
+                                admin_level=15, fromarea=True, isaddress=True,
+                                rank_address=26, distance=0.0),
+               napi.AddressLine(place_id=1000, osm_object=('N', 3333),
+                                category=('place', 'suburb'),
+                                names={'name': 'Smallplace'}, extratags={},
+                                admin_level=13, fromarea=False, isaddress=True,
+                                rank_address=23, distance=0.0034),
+               napi.AddressLine(place_id=1001, osm_object=('N', 3334),
+                                category=('place', 'city'),
+                                names={'name': 'Bigplace'}, extratags={},
+                                admin_level=15, fromarea=True, isaddress=True,
+                                rank_address=16, distance=0.0),
+               napi.AddressLine(place_id=None, osm_object=None,
+                                category=('place', 'country_code'),
+                                names={'ref': 'us'}, extratags={},
+                                admin_level=None, fromarea=True, isaddress=False,
+                                rank_address=4, distance=0.0)
            ]