python reverse: add support for point geometries in interpolations

This commit is contained in:
Sarah Hoffmann
2023-03-29 17:21:33 +02:00
parent c150ca4889
commit 26ee6b6dde
2 changed files with 45 additions and 2 deletions

View File

@@ -66,6 +66,14 @@ def _interpolated_position(table: SaFromClause) -> SaLabel:
else_=table.c.linegeo.ST_LineInterpolatePoint(rounded_pos)).label('centroid') else_=table.c.linegeo.ST_LineInterpolatePoint(rounded_pos)).label('centroid')
def _locate_interpolation(table: SaFromClause, wkt: WKTElement) -> SaLabel:
""" Given a position, locate the closest point on the line.
"""
return sa.case((table.c.linegeo.ST_GeometryType() == 'ST_LineString',
sa.func.ST_LineLocatePoint(table.c.linegeo, wkt)),
else_=0).label('position')
def _is_address_point(table: SaFromClause) -> SaColumn: def _is_address_point(table: SaFromClause) -> SaColumn:
return sa.and_(table.c.rank_address == 30, return sa.and_(table.c.rank_address == 30,
sa.or_(table.c.housenumber != None, sa.or_(table.c.housenumber != None,
@@ -207,7 +215,7 @@ class ReverseGeocoder:
sql = sa.select(t, sql = sa.select(t,
t.c.linegeo.ST_Distance(wkt).label('distance'), t.c.linegeo.ST_Distance(wkt).label('distance'),
t.c.linegeo.ST_LineLocatePoint(wkt).label('position'))\ _locate_interpolation(t, wkt))\
.where(t.c.linegeo.ST_DWithin(wkt, distance))\ .where(t.c.linegeo.ST_DWithin(wkt, distance))\
.where(t.c.startnumber != None)\ .where(t.c.startnumber != None)\
.order_by('distance')\ .order_by('distance')\
@@ -239,7 +247,7 @@ class ReverseGeocoder:
inner = sa.select(t, inner = sa.select(t,
t.c.linegeo.ST_Distance(wkt).label('distance'), t.c.linegeo.ST_Distance(wkt).label('distance'),
sa.func.ST_LineLocatePoint(t.c.linegeo, wkt).label('position'))\ _locate_interpolation(t, wkt))\
.where(t.c.linegeo.ST_DWithin(wkt, 0.001))\ .where(t.c.linegeo.ST_DWithin(wkt, 0.001))\
.where(t.c.parent_place_id == parent_place_id)\ .where(t.c.parent_place_id == parent_place_id)\
.order_by('distance')\ .order_by('distance')\

View File

@@ -135,6 +135,23 @@ def test_reverse_housenumber_interpolation(apiobj):
assert apiobj.api.reverse((10.0, 10.0)).place_id == 992 assert apiobj.api.reverse((10.0, 10.0)).place_id == 992
def test_reverse_housenumber_point_interpolation(apiobj):
apiobj.add_placex(place_id=990, class_='highway', type='service',
rank_search=27, rank_address=27,
name = {'name': 'My Street'},
centroid=(10.0, 10.0),
geometry='LINESTRING(9.995 10, 10.005 10)')
apiobj.add_osmline(place_id=992,
parent_place_id=990,
startnumber=42, endnumber=42, step=1,
centroid=(10.0, 10.00001),
geometry='POINT(10.0 10.00001)')
res = apiobj.api.reverse((10.0, 10.0))
assert res.place_id == 992
assert res.housenumber == '42'
def test_reverse_tiger_number(apiobj): def test_reverse_tiger_number(apiobj):
apiobj.add_placex(place_id=990, class_='highway', type='service', apiobj.add_placex(place_id=990, class_='highway', type='service',
rank_search=27, rank_address=27, rank_search=27, rank_address=27,
@@ -152,6 +169,24 @@ def test_reverse_tiger_number(apiobj):
assert apiobj.api.reverse((10.0, 10.00001)).place_id == 992 assert apiobj.api.reverse((10.0, 10.00001)).place_id == 992
def test_reverse_point_tiger(apiobj):
apiobj.add_placex(place_id=990, class_='highway', type='service',
rank_search=27, rank_address=27,
name = {'name': 'My Street'},
centroid=(10.0, 10.0),
country_code='us',
geometry='LINESTRING(9.995 10, 10.005 10)')
apiobj.add_tiger(place_id=992,
parent_place_id=990,
startnumber=1, endnumber=1, step=1,
centroid=(10.0, 10.00001),
geometry='POINT(10.0 10.00001)')
res = apiobj.api.reverse((10.0, 10.0))
assert res.place_id == 992
assert res.housenumber == '1'
def test_reverse_low_zoom_address(apiobj): def test_reverse_low_zoom_address(apiobj):
apiobj.add_placex(place_id=1001, class_='place', type='house', apiobj.add_placex(place_id=1001, class_='place', type='house',
housenumber='1', housenumber='1',