add lookup of tiger data

This commit is contained in:
Sarah Hoffmann
2023-02-02 15:31:42 +01:00
parent f1ceefe9a6
commit 70f6f9a711
4 changed files with 188 additions and 42 deletions

View 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

View 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 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
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,