adapt search frontend to new postcode table

This commit is contained in:
Sarah Hoffmann
2025-12-23 19:10:01 +01:00
parent f9cf320794
commit 6a67cfcddf
8 changed files with 59 additions and 78 deletions

View File

@@ -175,7 +175,8 @@ class AddressSearch(base.AbstractSearch):
sql = sql.where(sa.select(tpc.c.postcode)
.where(tpc.c.postcode.in_(self.postcodes.values))
.where(tpc.c.country_code == t.c.country_code)
.where(t.c.centroid.within_distance(tpc.c.geometry, 0.4))
.where(t.c.centroid.intersects(tpc.c.geometry,
use_index=False))
.exists())
if details.viewbox is not None:
@@ -225,7 +226,7 @@ class AddressSearch(base.AbstractSearch):
tpc = conn.t.postcode
pcs = self.postcodes.values
pc_near = sa.select(sa.func.min(tpc.c.geometry.ST_Distance(t.c.centroid)
pc_near = sa.select(sa.func.min(tpc.c.centroid.ST_Distance(t.c.centroid)
* (tpc.c.rank_search - 19)))\
.where(tpc.c.postcode.in_(pcs))\
.where(tpc.c.country_code == t.c.country_code)\

View File

@@ -79,7 +79,8 @@ class PlaceSearch(base.AbstractSearch):
tpc = conn.t.postcode
sql = sql.where(sa.select(tpc.c.postcode)
.where(tpc.c.postcode.in_(self.postcodes.values))
.where(t.c.centroid.within_distance(tpc.c.geometry, 0.4))
.where(t.c.centroid.intersects(tpc.c.geometry,
use_index=False))
.exists())
if details.viewbox is not None:
@@ -157,7 +158,7 @@ class PlaceSearch(base.AbstractSearch):
tpc = conn.t.postcode
pcs = self.postcodes.values
pc_near = sa.select(sa.func.min(tpc.c.geometry.ST_Distance(t.c.centroid)))\
pc_near = sa.select(sa.func.min(tpc.c.centroid.ST_Distance(t.c.centroid)))\
.where(tpc.c.postcode.in_(pcs))\
.scalar_subquery()
penalty += sa.case((t.c.postcode.in_(pcs), 0.0),

View File

@@ -14,7 +14,7 @@ from . import base
from ...typing import SaBind, SaExpression
from ...sql.sqlalchemy_types import Geometry, IntArray
from ...connection import SearchConnection
from ...types import SearchDetails, Bbox
from ...types import SearchDetails
from ... import results as nres
from ..db_search_fields import SearchData
@@ -42,10 +42,9 @@ class PostcodeSearch(base.AbstractSearch):
t = conn.t.postcode
pcs = self.postcodes.values
sql = sa.select(t.c.place_id, t.c.parent_place_id,
t.c.rank_search, t.c.rank_address,
t.c.postcode, t.c.country_code,
t.c.geometry.label('centroid'))\
sql = sa.select(t.c.place_id, t.c.parent_place_id, t.c.osm_id,
t.c.rank_search, t.c.postcode, t.c.country_code,
t.c.centroid)\
.where(t.c.postcode.in_(pcs))
if details.geometry_output:
@@ -59,7 +58,7 @@ class PostcodeSearch(base.AbstractSearch):
else_=1.0)
if details.near is not None:
sql = sql.order_by(t.c.geometry.ST_Distance(NEAR_PARAM))
sql = sql.order_by(t.c.centroid.ST_Distance(NEAR_PARAM))
sql = base.filter_by_area(sql, t, details)
@@ -100,29 +99,9 @@ class PostcodeSearch(base.AbstractSearch):
results = nres.SearchResults()
for row in await conn.execute(sql, bind_params):
p = conn.t.placex
placex_sql = base.select_placex(p)\
.add_columns(p.c.importance)\
.where(sa.text("""class = 'boundary'
AND type = 'postal_code'
AND osm_type = 'R'"""))\
.where(p.c.country_code == row.country_code)\
.where(p.c.postcode == row.postcode)\
.limit(1)
result = nres.create_from_postcode_row(row, nres.SearchResult)
if details.geometry_output:
placex_sql = base.add_geometry_columns(placex_sql, p.c.geometry, details)
for prow in await conn.execute(placex_sql, bind_params):
result = nres.create_from_placex_row(prow, nres.SearchResult)
if result is not None:
result.bbox = Bbox.from_wkb(prow.bbox)
break
else:
result = nres.create_from_postcode_row(row, nres.SearchResult)
if result.place_id not in details.excluded:
result.accuracy = row.accuracy
results.append(result)
result.accuracy = row.accuracy
results.append(result)
return results