1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of the acutal database accesses for forward search.
10 from typing import List, Tuple, AsyncIterator, Dict, Any, Callable
13 import sqlalchemy as sa
14 from sqlalchemy.dialects.postgresql import ARRAY, array_agg
16 from nominatim.typing import SaFromClause, SaScalarSelect, SaColumn, \
17 SaExpression, SaSelect, SaLambdaSelect, SaRow, SaBind
18 from nominatim.api.connection import SearchConnection
19 from nominatim.api.types import SearchDetails, DataLayer, GeometryFormat, Bbox
20 import nominatim.api.results as nres
21 from nominatim.api.search.db_search_fields import SearchData, WeightedCategories
22 from nominatim.db.sqlalchemy_types import Geometry
24 #pylint: disable=singleton-comparison,not-callable
25 #pylint: disable=too-many-branches,too-many-arguments,too-many-locals,too-many-statements
27 def _details_to_bind_params(details: SearchDetails) -> Dict[str, Any]:
28 """ Create a dictionary from search parameters that can be used
29 as bind parameter for SQL execute.
31 return {'limit': details.max_results,
32 'min_rank': details.min_rank,
33 'max_rank': details.max_rank,
34 'viewbox': details.viewbox,
35 'viewbox2': details.viewbox_x2,
37 'near_radius': details.near_radius,
38 'excluded': details.excluded,
39 'countries': details.countries}
42 LIMIT_PARAM: SaBind = sa.bindparam('limit')
43 MIN_RANK_PARAM: SaBind = sa.bindparam('min_rank')
44 MAX_RANK_PARAM: SaBind = sa.bindparam('max_rank')
45 VIEWBOX_PARAM: SaBind = sa.bindparam('viewbox', type_=Geometry)
46 VIEWBOX2_PARAM: SaBind = sa.bindparam('viewbox2', type_=Geometry)
47 NEAR_PARAM: SaBind = sa.bindparam('near', type_=Geometry)
48 NEAR_RADIUS_PARAM: SaBind = sa.bindparam('near_radius')
49 COUNTRIES_PARAM: SaBind = sa.bindparam('countries')
51 def _within_near(t: SaFromClause) -> Callable[[], SaExpression]:
52 return lambda: t.c.geometry.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM)
54 def _exclude_places(t: SaFromClause) -> Callable[[], SaExpression]:
55 return lambda: t.c.place_id.not_in(sa.bindparam('excluded'))
57 def _select_placex(t: SaFromClause) -> SaSelect:
58 return sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name,
60 t.c.address, t.c.extratags,
61 t.c.housenumber, t.c.postcode, t.c.country_code,
62 t.c.importance, t.c.wikipedia,
63 t.c.parent_place_id, t.c.rank_address, t.c.rank_search,
64 t.c.linked_place_id, t.c.admin_level,
66 t.c.geometry.ST_Expand(0).label('bbox'))
69 def _add_geometry_columns(sql: SaLambdaSelect, col: SaColumn, details: SearchDetails) -> SaSelect:
72 if details.geometry_simplification > 0.0:
73 col = sa.func.ST_SimplifyPreserveTopology(col, details.geometry_simplification)
75 if details.geometry_output & GeometryFormat.GEOJSON:
76 out.append(sa.func.ST_AsGeoJSON(col).label('geometry_geojson'))
77 if details.geometry_output & GeometryFormat.TEXT:
78 out.append(sa.func.ST_AsText(col).label('geometry_text'))
79 if details.geometry_output & GeometryFormat.KML:
80 out.append(sa.func.ST_AsKML(col).label('geometry_kml'))
81 if details.geometry_output & GeometryFormat.SVG:
82 out.append(sa.func.ST_AsSVG(col).label('geometry_svg'))
84 return sql.add_columns(*out)
87 def _make_interpolation_subquery(table: SaFromClause, inner: SaFromClause,
88 numerals: List[int], details: SearchDetails) -> SaScalarSelect:
89 all_ids = array_agg(table.c.place_id) # type: ignore[no-untyped-call]
90 sql = sa.select(all_ids).where(table.c.parent_place_id == inner.c.place_id)
92 if len(numerals) == 1:
93 sql = sql.where(sa.between(numerals[0], table.c.startnumber, table.c.endnumber))\
94 .where((numerals[0] - table.c.startnumber) % table.c.step == 0)
96 sql = sql.where(sa.or_(
97 *(sa.and_(sa.between(n, table.c.startnumber, table.c.endnumber),
98 (n - table.c.startnumber) % table.c.step == 0)
102 sql = sql.where(_exclude_places(table))
104 return sql.scalar_subquery()
107 def _filter_by_layer(table: SaFromClause, layers: DataLayer) -> SaColumn:
108 orexpr: List[SaExpression] = []
109 if layers & DataLayer.ADDRESS and layers & DataLayer.POI:
110 orexpr.append(table.c.rank_address.between(1, 30))
111 elif layers & DataLayer.ADDRESS:
112 orexpr.append(table.c.rank_address.between(1, 29))
113 orexpr.append(sa.and_(table.c.rank_address == 30,
114 sa.or_(table.c.housenumber != None,
115 table.c.address.has_key('addr:housename'))))
116 elif layers & DataLayer.POI:
117 orexpr.append(sa.and_(table.c.rank_address == 30,
118 table.c.class_.not_in(('place', 'building'))))
120 if layers & DataLayer.MANMADE:
122 if not layers & DataLayer.RAILWAY:
123 exclude.append('railway')
124 if not layers & DataLayer.NATURAL:
125 exclude.extend(('natural', 'water', 'waterway'))
126 orexpr.append(sa.and_(table.c.class_.not_in(tuple(exclude)),
127 table.c.rank_address == 0))
130 if layers & DataLayer.RAILWAY:
131 include.append('railway')
132 if layers & DataLayer.NATURAL:
133 include.extend(('natural', 'water', 'waterway'))
134 orexpr.append(sa.and_(table.c.class_.in_(tuple(include)),
135 table.c.rank_address == 0))
140 return sa.or_(*orexpr)
143 def _interpolated_position(table: SaFromClause, nr: SaColumn) -> SaColumn:
144 pos = sa.cast(nr - table.c.startnumber, sa.Float) / (table.c.endnumber - table.c.startnumber)
146 (table.c.endnumber == table.c.startnumber, table.c.linegeo.ST_Centroid()),
147 else_=table.c.linegeo.ST_LineInterpolatePoint(pos)).label('centroid')
150 async def _get_placex_housenumbers(conn: SearchConnection,
151 place_ids: List[int],
152 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
154 sql = _select_placex(t).where(t.c.place_id.in_(place_ids))
156 if details.geometry_output:
157 sql = _add_geometry_columns(sql, t.c.geometry, details)
159 for row in await conn.execute(sql):
160 result = nres.create_from_placex_row(row, nres.SearchResult)
162 result.bbox = Bbox.from_wkb(row.bbox)
166 async def _get_osmline(conn: SearchConnection, place_ids: List[int],
168 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
170 values = sa.values(sa.Column('nr', sa.Integer()), name='housenumber')\
171 .data([(n,) for n in numerals])
172 sql = sa.select(t.c.place_id, t.c.osm_id,
173 t.c.parent_place_id, t.c.address,
174 values.c.nr.label('housenumber'),
175 _interpolated_position(t, values.c.nr),
176 t.c.postcode, t.c.country_code)\
177 .where(t.c.place_id.in_(place_ids))\
178 .join(values, values.c.nr.between(t.c.startnumber, t.c.endnumber))
180 if details.geometry_output:
182 sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
184 for row in await conn.execute(sql):
185 result = nres.create_from_osmline_row(row, nres.SearchResult)
190 async def _get_tiger(conn: SearchConnection, place_ids: List[int],
191 numerals: List[int], osm_id: int,
192 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
194 values = sa.values(sa.Column('nr', sa.Integer()), name='housenumber')\
195 .data([(n,) for n in numerals])
196 sql = sa.select(t.c.place_id, t.c.parent_place_id,
197 sa.literal('W').label('osm_type'),
198 sa.literal(osm_id).label('osm_id'),
199 values.c.nr.label('housenumber'),
200 _interpolated_position(t, values.c.nr),
202 .where(t.c.place_id.in_(place_ids))\
203 .join(values, values.c.nr.between(t.c.startnumber, t.c.endnumber))
205 if details.geometry_output:
207 sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
209 for row in await conn.execute(sql):
210 result = nres.create_from_tiger_row(row, nres.SearchResult)
215 class AbstractSearch(abc.ABC):
216 """ Encapuslation of a single lookup in the database.
219 def __init__(self, penalty: float) -> None:
220 self.penalty = penalty
223 async def lookup(self, conn: SearchConnection,
224 details: SearchDetails) -> nres.SearchResults:
225 """ Find results for the search in the database.
229 class NearSearch(AbstractSearch):
230 """ Category search of a place type near the result of another search.
232 def __init__(self, penalty: float, categories: WeightedCategories,
233 search: AbstractSearch) -> None:
234 super().__init__(penalty)
236 self.categories = categories
239 async def lookup(self, conn: SearchConnection,
240 details: SearchDetails) -> nres.SearchResults:
241 """ Find results for the search in the database.
243 results = nres.SearchResults()
244 base = await self.search.lookup(conn, details)
249 base.sort(key=lambda r: (r.accuracy, r.rank_search))
250 max_accuracy = base[0].accuracy + 0.5
251 base = nres.SearchResults(r for r in base if r.source_table == nres.SourceTable.PLACEX
252 and r.accuracy <= max_accuracy
253 and r.bbox and r.bbox.area < 20)
256 baseids = [b.place_id for b in base[:5] if b.place_id]
258 for category, penalty in self.categories:
259 await self.lookup_category(results, conn, baseids, category, penalty, details)
260 if len(results) >= details.max_results:
266 async def lookup_category(self, results: nres.SearchResults,
267 conn: SearchConnection, ids: List[int],
268 category: Tuple[str, str], penalty: float,
269 details: SearchDetails) -> None:
270 """ Find places of the given category near the list of
271 place ids and add the results to 'results'.
273 table = await conn.get_class_table(*category)
276 tgeom = conn.t.placex.alias('pgeom')
278 sql = _select_placex(t).where(tgeom.c.place_id.in_(ids))\
279 .where(t.c.class_ == category[0])\
280 .where(t.c.type == category[1])
283 # No classtype table available, do a simplified lookup in placex.
284 sql = sql.join(tgeom, t.c.geometry.ST_DWithin(tgeom.c.centroid, 0.01))\
285 .order_by(tgeom.c.centroid.ST_Distance(t.c.centroid))
287 # Use classtype table. We can afford to use a larger
288 # radius for the lookup.
289 sql = sql.join(table, t.c.place_id == table.c.place_id)\
291 table.c.centroid.ST_CoveredBy(
292 sa.case((sa.and_(tgeom.c.rank_address < 9,
293 tgeom.c.geometry.is_area()),
295 else_ = tgeom.c.centroid.ST_Expand(0.05))))\
296 .order_by(tgeom.c.centroid.ST_Distance(table.c.centroid))
298 sql = sql.where(t.c.rank_address.between(MIN_RANK_PARAM, MAX_RANK_PARAM))
299 if details.countries:
300 sql = sql.where(t.c.country_code.in_(COUNTRIES_PARAM))
302 sql = sql.where(_exclude_places(t))
303 if details.layers is not None:
304 sql = sql.where(_filter_by_layer(t, details.layers))
306 sql = sql.limit(LIMIT_PARAM)
307 for row in await conn.execute(sql, _details_to_bind_params(details)):
308 result = nres.create_from_placex_row(row, nres.SearchResult)
310 result.accuracy = self.penalty + penalty
311 result.bbox = Bbox.from_wkb(row.bbox)
312 results.append(result)
316 class PoiSearch(AbstractSearch):
317 """ Category search in a geographic area.
319 def __init__(self, sdata: SearchData) -> None:
320 super().__init__(sdata.penalty)
321 self.qualifiers = sdata.qualifiers
322 self.countries = sdata.countries
325 async def lookup(self, conn: SearchConnection,
326 details: SearchDetails) -> nres.SearchResults:
327 """ Find results for the search in the database.
329 bind_params = _details_to_bind_params(details)
332 rows: List[SaRow] = []
334 if details.near and details.near_radius is not None and details.near_radius < 0.2:
335 # simply search in placex table
336 def _base_query() -> SaSelect:
337 return _select_placex(t) \
338 .where(t.c.linked_place_id == None) \
339 .where(t.c.geometry.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM)) \
340 .order_by(t.c.centroid.ST_Distance(NEAR_PARAM)) \
343 classtype = self.qualifiers.values
344 if len(classtype) == 1:
345 cclass, ctype = classtype[0]
346 sql: SaLambdaSelect = sa.lambda_stmt(lambda: _base_query()
347 .where(t.c.class_ == cclass)
348 .where(t.c.type == ctype))
350 sql = _base_query().where(sa.or_(*(sa.and_(t.c.class_ == cls, t.c.type == typ)
351 for cls, typ in classtype)))
354 sql = sql.where(t.c.country_code.in_(self.countries.values))
356 if details.viewbox is not None and details.bounded_viewbox:
357 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
359 rows.extend(await conn.execute(sql, bind_params))
361 # use the class type tables
362 for category in self.qualifiers.values:
363 table = await conn.get_class_table(*category)
364 if table is not None:
365 sql = _select_placex(t)\
366 .join(table, t.c.place_id == table.c.place_id)\
367 .where(t.c.class_ == category[0])\
368 .where(t.c.type == category[1])
370 if details.viewbox is not None and details.bounded_viewbox:
371 sql = sql.where(table.c.centroid.intersects(VIEWBOX_PARAM))
373 if details.near and details.near_radius is not None:
374 sql = sql.order_by(table.c.centroid.ST_Distance(NEAR_PARAM))\
375 .where(table.c.centroid.ST_DWithin(NEAR_PARAM,
379 sql = sql.where(t.c.country_code.in_(self.countries.values))
381 sql = sql.limit(LIMIT_PARAM)
382 rows.extend(await conn.execute(sql, bind_params))
384 results = nres.SearchResults()
386 result = nres.create_from_placex_row(row, nres.SearchResult)
388 result.accuracy = self.penalty + self.qualifiers.get_penalty((row.class_, row.type))
389 result.bbox = Bbox.from_wkb(row.bbox)
390 results.append(result)
395 class CountrySearch(AbstractSearch):
396 """ Search for a country name or country code.
398 def __init__(self, sdata: SearchData) -> None:
399 super().__init__(sdata.penalty)
400 self.countries = sdata.countries
403 async def lookup(self, conn: SearchConnection,
404 details: SearchDetails) -> nres.SearchResults:
405 """ Find results for the search in the database.
409 ccodes = self.countries.values
410 sql = _select_placex(t)\
411 .where(t.c.country_code.in_(ccodes))\
412 .where(t.c.rank_address == 4)
414 if details.geometry_output:
415 sql = _add_geometry_columns(sql, t.c.geometry, details)
418 sql = sql.where(_exclude_places(t))
420 if details.viewbox is not None and details.bounded_viewbox:
421 sql = sql.where(lambda: t.c.geometry.intersects(VIEWBOX_PARAM))
423 if details.near is not None and details.near_radius is not None:
424 sql = sql.where(_within_near(t))
426 results = nres.SearchResults()
427 for row in await conn.execute(sql, _details_to_bind_params(details)):
428 result = nres.create_from_placex_row(row, nres.SearchResult)
430 result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
431 result.bbox = Bbox.from_wkb(row.bbox)
432 results.append(result)
434 return results or await self.lookup_in_country_table(conn, details)
437 async def lookup_in_country_table(self, conn: SearchConnection,
438 details: SearchDetails) -> nres.SearchResults:
439 """ Look up the country in the fallback country tables.
441 # Avoid the fallback search when this is a more search. Country results
442 # usually are in the first batch of results and it is not possible
443 # to exclude these fallbacks.
445 return nres.SearchResults()
447 t = conn.t.country_name
448 tgrid = conn.t.country_grid
450 sql = sa.select(tgrid.c.country_code,
451 tgrid.c.geometry.ST_Centroid().ST_Collect().ST_Centroid()
453 tgrid.c.geometry.ST_Collect().ST_Expand(0).label('bbox'))\
454 .where(tgrid.c.country_code.in_(self.countries.values))\
455 .group_by(tgrid.c.country_code)
457 if details.viewbox is not None and details.bounded_viewbox:
458 sql = sql.where(tgrid.c.geometry.intersects(VIEWBOX_PARAM))
459 if details.near is not None and details.near_radius is not None:
460 sql = sql.where(_within_near(tgrid))
462 sub = sql.subquery('grid')
464 sql = sa.select(t.c.country_code,
466 + sa.func.coalesce(t.c.derived_name,
467 sa.cast('', type_=conn.t.types.Composite))
469 sub.c.centroid, sub.c.bbox)\
470 .join(sub, t.c.country_code == sub.c.country_code)
472 if details.geometry_output:
473 sql = _add_geometry_columns(sql, sub.c.centroid, details)
475 results = nres.SearchResults()
476 for row in await conn.execute(sql, _details_to_bind_params(details)):
477 result = nres.create_from_country_row(row, nres.SearchResult)
479 result.bbox = Bbox.from_wkb(row.bbox)
480 result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
481 results.append(result)
487 class PostcodeSearch(AbstractSearch):
488 """ Search for a postcode.
490 def __init__(self, extra_penalty: float, sdata: SearchData) -> None:
491 super().__init__(sdata.penalty + extra_penalty)
492 self.countries = sdata.countries
493 self.postcodes = sdata.postcodes
494 self.lookups = sdata.lookups
495 self.rankings = sdata.rankings
498 async def lookup(self, conn: SearchConnection,
499 details: SearchDetails) -> nres.SearchResults:
500 """ Find results for the search in the database.
503 pcs = self.postcodes.values
505 sql = sa.select(t.c.place_id, t.c.parent_place_id,
506 t.c.rank_search, t.c.rank_address,
507 t.c.postcode, t.c.country_code,
508 t.c.geometry.label('centroid'))\
509 .where(t.c.postcode.in_(pcs))
511 if details.geometry_output:
512 sql = _add_geometry_columns(sql, t.c.geometry, details)
514 penalty: SaExpression = sa.literal(self.penalty)
516 if details.viewbox is not None:
517 if details.bounded_viewbox:
518 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
520 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
521 (t.c.geometry.intersects(VIEWBOX2_PARAM), 1.0),
524 if details.near is not None:
525 if details.near_radius is not None:
526 sql = sql.where(_within_near(t))
527 sql = sql.order_by(t.c.geometry.ST_Distance(NEAR_PARAM))
530 sql = sql.where(t.c.country_code.in_(self.countries.values))
533 sql = sql.where(_exclude_places(t))
536 assert len(self.lookups) == 1
537 assert self.lookups[0].lookup_type == 'restrict'
538 tsearch = conn.t.search_name
539 sql = sql.where(tsearch.c.place_id == t.c.parent_place_id)\
540 .where(sa.func.array_cat(tsearch.c.name_vector,
541 tsearch.c.nameaddress_vector,
542 type_=ARRAY(sa.Integer))
543 .contains(self.lookups[0].tokens))
545 for ranking in self.rankings:
546 penalty += ranking.sql_penalty(conn.t.search_name)
547 penalty += sa.case(*((t.c.postcode == v, p) for v, p in self.postcodes),
551 sql = sql.add_columns(penalty.label('accuracy'))
552 sql = sql.order_by('accuracy').limit(LIMIT_PARAM)
554 results = nres.SearchResults()
555 for row in await conn.execute(sql, _details_to_bind_params(details)):
556 result = nres.create_from_postcode_row(row, nres.SearchResult)
558 result.accuracy = row.accuracy
559 results.append(result)
565 class PlaceSearch(AbstractSearch):
566 """ Generic search for an address or named place.
568 def __init__(self, extra_penalty: float, sdata: SearchData, expected_count: int) -> None:
569 super().__init__(sdata.penalty + extra_penalty)
570 self.countries = sdata.countries
571 self.postcodes = sdata.postcodes
572 self.housenumbers = sdata.housenumbers
573 self.qualifiers = sdata.qualifiers
574 self.lookups = sdata.lookups
575 self.rankings = sdata.rankings
576 self.expected_count = expected_count
579 async def lookup(self, conn: SearchConnection,
580 details: SearchDetails) -> nres.SearchResults:
581 """ Find results for the search in the database.
584 tsearch = conn.t.search_name
586 sql: SaLambdaSelect = sa.lambda_stmt(lambda:
587 sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name,
588 t.c.class_, t.c.type,
589 t.c.address, t.c.extratags, t.c.admin_level,
590 t.c.housenumber, t.c.postcode, t.c.country_code,
592 t.c.parent_place_id, t.c.rank_address, t.c.rank_search,
594 t.c.geometry.ST_Expand(0).label('bbox'))
595 .where(t.c.place_id == tsearch.c.place_id))
598 if details.geometry_output:
599 sql = _add_geometry_columns(sql, t.c.geometry, details)
601 penalty: SaExpression = sa.literal(self.penalty)
602 for ranking in self.rankings:
603 penalty += ranking.sql_penalty(tsearch)
605 for lookup in self.lookups:
606 sql = sql.where(lookup.sql_condition(tsearch))
609 sql = sql.where(tsearch.c.country_code.in_(self.countries.values))
612 # if a postcode is given, don't search for state or country level objects
613 sql = sql.where(tsearch.c.address_rank > 9)
614 tpc = conn.t.postcode
615 pcs = self.postcodes.values
616 if self.expected_count > 1000:
617 # Many results expected. Restrict by postcode.
618 sql = sql.where(sa.select(tpc.c.postcode)
619 .where(tpc.c.postcode.in_(pcs))
620 .where(tsearch.c.centroid.ST_DWithin(tpc.c.geometry, 0.12))
623 # Less results, only have a preference for close postcodes
624 pc_near = sa.select(sa.func.min(tpc.c.geometry.ST_Distance(tsearch.c.centroid)))\
625 .where(tpc.c.postcode.in_(pcs))\
627 penalty += sa.case((t.c.postcode.in_(pcs), 0.0),
628 else_=sa.func.coalesce(pc_near, 2.0))
630 if details.viewbox is not None:
631 if details.bounded_viewbox:
632 if details.viewbox.area < 0.2:
633 sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX_PARAM))
635 sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX_PARAM))
636 elif self.expected_count >= 10000:
637 if details.viewbox.area < 0.5:
638 sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX2_PARAM))
640 sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX2_PARAM))
642 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
643 (t.c.geometry.intersects(VIEWBOX2_PARAM), 1.0),
646 if details.near is not None:
647 if details.near_radius is not None:
648 if details.near_radius < 0.1:
649 sql = sql.where(tsearch.c.centroid.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM))
651 sql = sql.where(tsearch.c.centroid.ST_DWithin_no_index(NEAR_PARAM,
653 sql = sql.add_columns((-tsearch.c.centroid.ST_Distance(NEAR_PARAM))
654 .label('importance'))
655 sql = sql.order_by(sa.desc(sa.text('importance')))
657 if self.expected_count < 10000\
658 or (details.viewbox is not None and details.viewbox.area < 0.5):
660 penalty - sa.case((tsearch.c.importance > 0, tsearch.c.importance),
661 else_=0.75001-(sa.cast(tsearch.c.search_rank, sa.Float())/40)))
662 sql = sql.add_columns(t.c.importance)
665 sql = sql.add_columns(penalty.label('accuracy'))
667 if self.expected_count < 10000:
668 sql = sql.order_by(sa.text('accuracy'))
670 if self.housenumbers:
671 hnr_regexp = f"\\m({'|'.join(self.housenumbers.values)})\\M"
672 sql = sql.where(tsearch.c.address_rank.between(16, 30))\
673 .where(sa.or_(tsearch.c.address_rank < 30,
674 t.c.housenumber.op('~*')(hnr_regexp)))
676 # Cross check for housenumbers, need to do that on a rather large
677 # set. Worst case there are 40.000 main streets in OSM.
678 inner = sql.limit(10000).subquery()
680 # Housenumbers from placex
681 thnr = conn.t.placex.alias('hnr')
682 pid_list = array_agg(thnr.c.place_id) # type: ignore[no-untyped-call]
683 place_sql = sa.select(pid_list)\
684 .where(thnr.c.parent_place_id == inner.c.place_id)\
685 .where(thnr.c.housenumber.op('~*')(hnr_regexp))\
686 .where(thnr.c.linked_place_id == None)\
687 .where(thnr.c.indexed_status == 0)
690 place_sql = place_sql.where(thnr.c.place_id.not_in(sa.bindparam('excluded')))
692 place_sql = place_sql.where(self.qualifiers.sql_restrict(thnr))
694 numerals = [int(n) for n in self.housenumbers.values
695 if n.isdigit() and len(n) < 8]
696 interpol_sql: SaColumn
699 (not self.qualifiers or ('place', 'house') in self.qualifiers.values):
700 # Housenumbers from interpolations
701 interpol_sql = _make_interpolation_subquery(conn.t.osmline, inner,
703 # Housenumbers from Tiger
704 tiger_sql = sa.case((inner.c.country_code == 'us',
705 _make_interpolation_subquery(conn.t.tiger, inner,
709 interpol_sql = sa.null()
710 tiger_sql = sa.null()
712 unsort = sa.select(inner, place_sql.scalar_subquery().label('placex_hnr'),
713 interpol_sql.label('interpol_hnr'),
714 tiger_sql.label('tiger_hnr')).subquery('unsort')
715 sql = sa.select(unsort)\
716 .order_by(sa.case((unsort.c.placex_hnr != None, 1),
717 (unsort.c.interpol_hnr != None, 2),
718 (unsort.c.tiger_hnr != None, 3),
722 sql = sql.where(t.c.linked_place_id == None)\
723 .where(t.c.indexed_status == 0)
725 sql = sql.where(self.qualifiers.sql_restrict(t))
727 sql = sql.where(_exclude_places(tsearch))
728 if details.min_rank > 0:
729 sql = sql.where(sa.or_(tsearch.c.address_rank >= MIN_RANK_PARAM,
730 tsearch.c.search_rank >= MIN_RANK_PARAM))
731 if details.max_rank < 30:
732 sql = sql.where(sa.or_(tsearch.c.address_rank <= MAX_RANK_PARAM,
733 tsearch.c.search_rank <= MAX_RANK_PARAM))
734 if details.layers is not None:
735 sql = sql.where(_filter_by_layer(t, details.layers))
737 sql = sql.limit(LIMIT_PARAM)
739 results = nres.SearchResults()
740 for row in await conn.execute(sql, _details_to_bind_params(details)):
741 result = nres.create_from_placex_row(row, nres.SearchResult)
743 result.bbox = Bbox.from_wkb(row.bbox)
744 result.accuracy = row.accuracy
745 if not details.excluded or not result.place_id in details.excluded:
746 results.append(result)
748 if self.housenumbers and row.rank_address < 30:
750 subs = _get_placex_housenumbers(conn, row.placex_hnr, details)
751 elif row.interpol_hnr:
752 subs = _get_osmline(conn, row.interpol_hnr, numerals, details)
754 subs = _get_tiger(conn, row.tiger_hnr, numerals, row.osm_id, details)
759 async for sub in subs:
760 assert sub.housenumber
761 sub.accuracy = result.accuracy
762 if not any(nr in self.housenumbers.values
763 for nr in sub.housenumber.split(';')):
767 result.accuracy += 1.0 # penalty for missing housenumber