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_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 no_index(expr: SaColumn) -> SaColumn:
28 """ Wrap the given expression, so that the query planner will
29 refrain from using the expression for index lookup.
31 return sa.func.coalesce(sa.null(), expr) # pylint: disable=not-callable
34 def _details_to_bind_params(details: SearchDetails) -> Dict[str, Any]:
35 """ Create a dictionary from search parameters that can be used
36 as bind parameter for SQL execute.
38 return {'limit': details.max_results,
39 'min_rank': details.min_rank,
40 'max_rank': details.max_rank,
41 'viewbox': details.viewbox,
42 'viewbox2': details.viewbox_x2,
44 'near_radius': details.near_radius,
45 'excluded': details.excluded,
46 'countries': details.countries}
49 LIMIT_PARAM: SaBind = sa.bindparam('limit')
50 MIN_RANK_PARAM: SaBind = sa.bindparam('min_rank')
51 MAX_RANK_PARAM: SaBind = sa.bindparam('max_rank')
52 VIEWBOX_PARAM: SaBind = sa.bindparam('viewbox', type_=Geometry)
53 VIEWBOX2_PARAM: SaBind = sa.bindparam('viewbox2', type_=Geometry)
54 NEAR_PARAM: SaBind = sa.bindparam('near', type_=Geometry)
55 NEAR_RADIUS_PARAM: SaBind = sa.bindparam('near_radius')
56 COUNTRIES_PARAM: SaBind = sa.bindparam('countries')
59 def filter_by_area(sql: SaSelect, t: SaFromClause,
60 details: SearchDetails, avoid_index: bool = False) -> SaSelect:
61 """ Apply SQL statements for filtering by viewbox and near point,
64 if details.near is not None and details.near_radius is not None:
65 if details.near_radius < 0.1 and not avoid_index:
66 sql = sql.where(t.c.geometry.within_distance(NEAR_PARAM, NEAR_RADIUS_PARAM))
68 sql = sql.where(t.c.geometry.ST_Distance(NEAR_PARAM) <= NEAR_RADIUS_PARAM)
69 if details.viewbox is not None and details.bounded_viewbox:
70 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM,
71 use_index=not avoid_index and
72 details.viewbox.area < 0.2))
77 def _exclude_places(t: SaFromClause) -> Callable[[], SaExpression]:
78 return lambda: t.c.place_id.not_in(sa.bindparam('excluded'))
81 def _select_placex(t: SaFromClause) -> SaSelect:
82 return sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name,
84 t.c.address, t.c.extratags,
85 t.c.housenumber, t.c.postcode, t.c.country_code,
87 t.c.parent_place_id, t.c.rank_address, t.c.rank_search,
88 t.c.linked_place_id, t.c.admin_level,
90 t.c.geometry.ST_Expand(0).label('bbox'))
93 def _add_geometry_columns(sql: SaLambdaSelect, col: SaColumn, details: SearchDetails) -> SaSelect:
96 if details.geometry_simplification > 0.0:
97 col = sa.func.ST_SimplifyPreserveTopology(col, details.geometry_simplification)
99 if details.geometry_output & GeometryFormat.GEOJSON:
100 out.append(sa.func.ST_AsGeoJSON(col, 7).label('geometry_geojson'))
101 if details.geometry_output & GeometryFormat.TEXT:
102 out.append(sa.func.ST_AsText(col).label('geometry_text'))
103 if details.geometry_output & GeometryFormat.KML:
104 out.append(sa.func.ST_AsKML(col, 7).label('geometry_kml'))
105 if details.geometry_output & GeometryFormat.SVG:
106 out.append(sa.func.ST_AsSVG(col, 0, 7).label('geometry_svg'))
108 return sql.add_columns(*out)
111 def _make_interpolation_subquery(table: SaFromClause, inner: SaFromClause,
112 numerals: List[int], details: SearchDetails) -> SaScalarSelect:
113 all_ids = array_agg(table.c.place_id) # type: ignore[no-untyped-call]
114 sql = sa.select(all_ids).where(table.c.parent_place_id == inner.c.place_id)
116 if len(numerals) == 1:
117 sql = sql.where(sa.between(numerals[0], table.c.startnumber, table.c.endnumber))\
118 .where((numerals[0] - table.c.startnumber) % table.c.step == 0)
120 sql = sql.where(sa.or_(
121 *(sa.and_(sa.between(n, table.c.startnumber, table.c.endnumber),
122 (n - table.c.startnumber) % table.c.step == 0)
126 sql = sql.where(_exclude_places(table))
128 return sql.scalar_subquery()
131 def _filter_by_layer(table: SaFromClause, layers: DataLayer) -> SaColumn:
132 orexpr: List[SaExpression] = []
133 if layers & DataLayer.ADDRESS and layers & DataLayer.POI:
134 orexpr.append(no_index(table.c.rank_address).between(1, 30))
135 elif layers & DataLayer.ADDRESS:
136 orexpr.append(no_index(table.c.rank_address).between(1, 29))
137 orexpr.append(sa.and_(no_index(table.c.rank_address) == 30,
138 sa.or_(table.c.housenumber != None,
139 table.c.address.has_key('addr:housename'))))
140 elif layers & DataLayer.POI:
141 orexpr.append(sa.and_(no_index(table.c.rank_address) == 30,
142 table.c.class_.not_in(('place', 'building'))))
144 if layers & DataLayer.MANMADE:
146 if not layers & DataLayer.RAILWAY:
147 exclude.append('railway')
148 if not layers & DataLayer.NATURAL:
149 exclude.extend(('natural', 'water', 'waterway'))
150 orexpr.append(sa.and_(table.c.class_.not_in(tuple(exclude)),
151 no_index(table.c.rank_address) == 0))
154 if layers & DataLayer.RAILWAY:
155 include.append('railway')
156 if layers & DataLayer.NATURAL:
157 include.extend(('natural', 'water', 'waterway'))
158 orexpr.append(sa.and_(table.c.class_.in_(tuple(include)),
159 no_index(table.c.rank_address) == 0))
164 return sa.or_(*orexpr)
167 def _interpolated_position(table: SaFromClause, nr: SaColumn) -> SaColumn:
168 pos = sa.cast(nr - table.c.startnumber, sa.Float) / (table.c.endnumber - table.c.startnumber)
170 (table.c.endnumber == table.c.startnumber, table.c.linegeo.ST_Centroid()),
171 else_=table.c.linegeo.ST_LineInterpolatePoint(pos)).label('centroid')
174 async def _get_placex_housenumbers(conn: SearchConnection,
175 place_ids: List[int],
176 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
178 sql = _select_placex(t).add_columns(t.c.importance)\
179 .where(t.c.place_id.in_(place_ids))
181 if details.geometry_output:
182 sql = _add_geometry_columns(sql, t.c.geometry, details)
184 for row in await conn.execute(sql):
185 result = nres.create_from_placex_row(row, nres.SearchResult)
187 result.bbox = Bbox.from_wkb(row.bbox)
191 async def _get_osmline(conn: SearchConnection, place_ids: List[int],
193 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
195 values = sa.values(sa.Column('nr', sa.Integer()), name='housenumber')\
196 .data([(n,) for n in numerals])
197 sql = sa.select(t.c.place_id, t.c.osm_id,
198 t.c.parent_place_id, t.c.address,
199 values.c.nr.label('housenumber'),
200 _interpolated_position(t, values.c.nr),
201 t.c.postcode, t.c.country_code)\
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_osmline_row(row, nres.SearchResult)
215 async def _get_tiger(conn: SearchConnection, place_ids: List[int],
216 numerals: List[int], osm_id: int,
217 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
219 values = sa.values(sa.Column('nr', sa.Integer()), name='housenumber')\
220 .data([(n,) for n in numerals])
221 sql = sa.select(t.c.place_id, t.c.parent_place_id,
222 sa.literal('W').label('osm_type'),
223 sa.literal(osm_id).label('osm_id'),
224 values.c.nr.label('housenumber'),
225 _interpolated_position(t, values.c.nr),
227 .where(t.c.place_id.in_(place_ids))\
228 .join(values, values.c.nr.between(t.c.startnumber, t.c.endnumber))
230 if details.geometry_output:
232 sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
234 for row in await conn.execute(sql):
235 result = nres.create_from_tiger_row(row, nres.SearchResult)
240 class AbstractSearch(abc.ABC):
241 """ Encapuslation of a single lookup in the database.
244 def __init__(self, penalty: float) -> None:
245 self.penalty = penalty
248 async def lookup(self, conn: SearchConnection,
249 details: SearchDetails) -> nres.SearchResults:
250 """ Find results for the search in the database.
254 class NearSearch(AbstractSearch):
255 """ Category search of a place type near the result of another search.
257 def __init__(self, penalty: float, categories: WeightedCategories,
258 search: AbstractSearch) -> None:
259 super().__init__(penalty)
261 self.categories = categories
264 async def lookup(self, conn: SearchConnection,
265 details: SearchDetails) -> nres.SearchResults:
266 """ Find results for the search in the database.
268 results = nres.SearchResults()
269 base = await self.search.lookup(conn, details)
274 base.sort(key=lambda r: (r.accuracy, r.rank_search))
275 max_accuracy = base[0].accuracy + 0.5
276 if base[0].rank_address == 0:
279 elif base[0].rank_address < 26:
281 max_rank = min(25, base[0].rank_address + 4)
285 base = nres.SearchResults(r for r in base if r.source_table == nres.SourceTable.PLACEX
286 and r.accuracy <= max_accuracy
287 and r.bbox and r.bbox.area < 20
288 and r.rank_address >= min_rank
289 and r.rank_address <= max_rank)
292 baseids = [b.place_id for b in base[:5] if b.place_id]
294 for category, penalty in self.categories:
295 await self.lookup_category(results, conn, baseids, category, penalty, details)
296 if len(results) >= details.max_results:
302 async def lookup_category(self, results: nres.SearchResults,
303 conn: SearchConnection, ids: List[int],
304 category: Tuple[str, str], penalty: float,
305 details: SearchDetails) -> None:
306 """ Find places of the given category near the list of
307 place ids and add the results to 'results'.
309 table = await conn.get_class_table(*category)
311 tgeom = conn.t.placex.alias('pgeom')
314 # No classtype table available, do a simplified lookup in placex.
315 table = conn.t.placex
316 sql = sa.select(table.c.place_id,
317 sa.func.min(tgeom.c.centroid.ST_Distance(table.c.centroid))
319 .join(tgeom, table.c.geometry.intersects(tgeom.c.centroid.ST_Expand(0.01)))\
320 .where(table.c.class_ == category[0])\
321 .where(table.c.type == category[1])
323 # Use classtype table. We can afford to use a larger
324 # radius for the lookup.
325 sql = sa.select(table.c.place_id,
326 sa.func.min(tgeom.c.centroid.ST_Distance(table.c.centroid))
329 table.c.centroid.ST_CoveredBy(
330 sa.case((sa.and_(tgeom.c.rank_address > 9,
331 tgeom.c.geometry.is_area()),
333 else_ = tgeom.c.centroid.ST_Expand(0.05))))
335 inner = sql.where(tgeom.c.place_id.in_(ids))\
336 .group_by(table.c.place_id).subquery()
339 sql = _select_placex(t).add_columns((-inner.c.dist).label('importance'))\
340 .join(inner, inner.c.place_id == t.c.place_id)\
341 .order_by(inner.c.dist)
343 sql = sql.where(no_index(t.c.rank_address).between(MIN_RANK_PARAM, MAX_RANK_PARAM))
344 if details.countries:
345 sql = sql.where(t.c.country_code.in_(COUNTRIES_PARAM))
347 sql = sql.where(_exclude_places(t))
348 if details.layers is not None:
349 sql = sql.where(_filter_by_layer(t, details.layers))
351 sql = sql.limit(LIMIT_PARAM)
352 for row in await conn.execute(sql, _details_to_bind_params(details)):
353 result = nres.create_from_placex_row(row, nres.SearchResult)
355 result.accuracy = self.penalty + penalty
356 result.bbox = Bbox.from_wkb(row.bbox)
357 results.append(result)
361 class PoiSearch(AbstractSearch):
362 """ Category search in a geographic area.
364 def __init__(self, sdata: SearchData) -> None:
365 super().__init__(sdata.penalty)
366 self.qualifiers = sdata.qualifiers
367 self.countries = sdata.countries
370 async def lookup(self, conn: SearchConnection,
371 details: SearchDetails) -> nres.SearchResults:
372 """ Find results for the search in the database.
374 bind_params = _details_to_bind_params(details)
377 rows: List[SaRow] = []
379 if details.near and details.near_radius is not None and details.near_radius < 0.2:
380 # simply search in placex table
381 def _base_query() -> SaSelect:
382 return _select_placex(t) \
383 .add_columns((-t.c.centroid.ST_Distance(NEAR_PARAM))
384 .label('importance'))\
385 .where(t.c.linked_place_id == None) \
386 .where(t.c.geometry.within_distance(NEAR_PARAM, NEAR_RADIUS_PARAM)) \
387 .order_by(t.c.centroid.ST_Distance(NEAR_PARAM)) \
390 classtype = self.qualifiers.values
391 if len(classtype) == 1:
392 cclass, ctype = classtype[0]
393 sql: SaLambdaSelect = sa.lambda_stmt(lambda: _base_query()
394 .where(t.c.class_ == cclass)
395 .where(t.c.type == ctype))
397 sql = _base_query().where(sa.or_(*(sa.and_(t.c.class_ == cls, t.c.type == typ)
398 for cls, typ in classtype)))
401 sql = sql.where(t.c.country_code.in_(self.countries.values))
403 if details.viewbox is not None and details.bounded_viewbox:
404 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
406 rows.extend(await conn.execute(sql, bind_params))
408 # use the class type tables
409 for category in self.qualifiers.values:
410 table = await conn.get_class_table(*category)
411 if table is not None:
412 sql = _select_placex(t)\
413 .add_columns(t.c.importance)\
414 .join(table, t.c.place_id == table.c.place_id)\
415 .where(t.c.class_ == category[0])\
416 .where(t.c.type == category[1])
418 if details.viewbox is not None and details.bounded_viewbox:
419 sql = sql.where(table.c.centroid.intersects(VIEWBOX_PARAM))
421 if details.near and details.near_radius is not None:
422 sql = sql.order_by(table.c.centroid.ST_Distance(NEAR_PARAM))\
423 .where(table.c.centroid.within_distance(NEAR_PARAM,
427 sql = sql.where(t.c.country_code.in_(self.countries.values))
429 sql = sql.limit(LIMIT_PARAM)
430 rows.extend(await conn.execute(sql, bind_params))
432 results = nres.SearchResults()
434 result = nres.create_from_placex_row(row, nres.SearchResult)
436 result.accuracy = self.penalty + self.qualifiers.get_penalty((row.class_, row.type))
437 result.bbox = Bbox.from_wkb(row.bbox)
438 results.append(result)
443 class CountrySearch(AbstractSearch):
444 """ Search for a country name or country code.
446 def __init__(self, sdata: SearchData) -> None:
447 super().__init__(sdata.penalty)
448 self.countries = sdata.countries
451 async def lookup(self, conn: SearchConnection,
452 details: SearchDetails) -> nres.SearchResults:
453 """ Find results for the search in the database.
457 ccodes = self.countries.values
458 sql = _select_placex(t)\
459 .add_columns(t.c.importance)\
460 .where(t.c.country_code.in_(ccodes))\
461 .where(t.c.rank_address == 4)
463 if details.geometry_output:
464 sql = _add_geometry_columns(sql, t.c.geometry, details)
467 sql = sql.where(_exclude_places(t))
469 sql = filter_by_area(sql, t, details)
471 results = nres.SearchResults()
472 for row in await conn.execute(sql, _details_to_bind_params(details)):
473 result = nres.create_from_placex_row(row, nres.SearchResult)
475 result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
476 result.bbox = Bbox.from_wkb(row.bbox)
477 results.append(result)
479 return results or await self.lookup_in_country_table(conn, details)
482 async def lookup_in_country_table(self, conn: SearchConnection,
483 details: SearchDetails) -> nres.SearchResults:
484 """ Look up the country in the fallback country tables.
486 # Avoid the fallback search when this is a more search. Country results
487 # usually are in the first batch of results and it is not possible
488 # to exclude these fallbacks.
490 return nres.SearchResults()
492 t = conn.t.country_name
493 tgrid = conn.t.country_grid
495 sql = sa.select(tgrid.c.country_code,
496 tgrid.c.geometry.ST_Centroid().ST_Collect().ST_Centroid()
498 tgrid.c.geometry.ST_Collect().ST_Expand(0).label('bbox'))\
499 .where(tgrid.c.country_code.in_(self.countries.values))\
500 .group_by(tgrid.c.country_code)
502 sql = filter_by_area(sql, tgrid, details, avoid_index=True)
504 sub = sql.subquery('grid')
506 sql = sa.select(t.c.country_code,
507 t.c.name.merge(t.c.derived_name).label('name'),
508 sub.c.centroid, sub.c.bbox)\
509 .join(sub, t.c.country_code == sub.c.country_code)
511 if details.geometry_output:
512 sql = _add_geometry_columns(sql, sub.c.centroid, details)
514 results = nres.SearchResults()
515 for row in await conn.execute(sql, _details_to_bind_params(details)):
516 result = nres.create_from_country_row(row, nres.SearchResult)
518 result.bbox = Bbox.from_wkb(row.bbox)
519 result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
520 results.append(result)
526 class PostcodeSearch(AbstractSearch):
527 """ Search for a postcode.
529 def __init__(self, extra_penalty: float, sdata: SearchData) -> None:
530 super().__init__(sdata.penalty + extra_penalty)
531 self.countries = sdata.countries
532 self.postcodes = sdata.postcodes
533 self.lookups = sdata.lookups
534 self.rankings = sdata.rankings
537 async def lookup(self, conn: SearchConnection,
538 details: SearchDetails) -> nres.SearchResults:
539 """ Find results for the search in the database.
542 pcs = self.postcodes.values
544 sql = sa.select(t.c.place_id, t.c.parent_place_id,
545 t.c.rank_search, t.c.rank_address,
546 t.c.postcode, t.c.country_code,
547 t.c.geometry.label('centroid'))\
548 .where(t.c.postcode.in_(pcs))
550 if details.geometry_output:
551 sql = _add_geometry_columns(sql, t.c.geometry, details)
553 penalty: SaExpression = sa.literal(self.penalty)
555 if details.viewbox is not None and not details.bounded_viewbox:
556 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
557 (t.c.geometry.intersects(VIEWBOX2_PARAM), 0.5),
560 if details.near is not None:
561 sql = sql.order_by(t.c.geometry.ST_Distance(NEAR_PARAM))
563 sql = filter_by_area(sql, t, details)
566 sql = sql.where(t.c.country_code.in_(self.countries.values))
569 sql = sql.where(_exclude_places(t))
572 assert len(self.lookups) == 1
573 tsearch = conn.t.search_name
574 sql = sql.where(tsearch.c.place_id == t.c.parent_place_id)\
575 .where((tsearch.c.name_vector + tsearch.c.nameaddress_vector)
576 .contains(self.lookups[0].tokens))
578 for ranking in self.rankings:
579 penalty += ranking.sql_penalty(conn.t.search_name)
580 penalty += sa.case(*((t.c.postcode == v, p) for v, p in self.postcodes),
584 sql = sql.add_columns(penalty.label('accuracy'))
585 sql = sql.order_by('accuracy').limit(LIMIT_PARAM)
587 results = nres.SearchResults()
588 for row in await conn.execute(sql, _details_to_bind_params(details)):
589 result = nres.create_from_postcode_row(row, nres.SearchResult)
591 result.accuracy = row.accuracy
592 results.append(result)
598 class PlaceSearch(AbstractSearch):
599 """ Generic search for an address or named place.
601 def __init__(self, extra_penalty: float, sdata: SearchData, expected_count: int) -> None:
602 super().__init__(sdata.penalty + extra_penalty)
603 self.countries = sdata.countries
604 self.postcodes = sdata.postcodes
605 self.housenumbers = sdata.housenumbers
606 self.qualifiers = sdata.qualifiers
607 self.lookups = sdata.lookups
608 self.rankings = sdata.rankings
609 self.expected_count = expected_count
612 async def lookup(self, conn: SearchConnection,
613 details: SearchDetails) -> nres.SearchResults:
614 """ Find results for the search in the database.
617 tsearch = conn.t.search_name
619 sql: SaLambdaSelect = sa.lambda_stmt(lambda:
620 _select_placex(t).where(t.c.place_id == tsearch.c.place_id))
623 if details.geometry_output:
624 sql = _add_geometry_columns(sql, t.c.geometry, details)
626 penalty: SaExpression = sa.literal(self.penalty)
627 for ranking in self.rankings:
628 penalty += ranking.sql_penalty(tsearch)
630 for lookup in self.lookups:
631 sql = sql.where(lookup.sql_condition(tsearch))
634 sql = sql.where(tsearch.c.country_code.in_(self.countries.values))
637 # if a postcode is given, don't search for state or country level objects
638 sql = sql.where(tsearch.c.address_rank > 9)
639 tpc = conn.t.postcode
640 pcs = self.postcodes.values
641 if self.expected_count > 5000:
642 # Many results expected. Restrict by postcode.
643 sql = sql.where(sa.select(tpc.c.postcode)
644 .where(tpc.c.postcode.in_(pcs))
645 .where(tsearch.c.centroid.within_distance(tpc.c.geometry, 0.12))
648 # Less results, only have a preference for close postcodes
649 pc_near = sa.select(sa.func.min(tpc.c.geometry.ST_Distance(tsearch.c.centroid)))\
650 .where(tpc.c.postcode.in_(pcs))\
652 penalty += sa.case((t.c.postcode.in_(pcs), 0.0),
653 else_=sa.func.coalesce(pc_near, 2.0))
655 if details.viewbox is not None:
656 if details.bounded_viewbox:
657 sql = sql.where(tsearch.c.centroid
658 .intersects(VIEWBOX_PARAM,
659 use_index=details.viewbox.area < 0.2))
660 elif self.expected_count >= 10000:
661 sql = sql.where(tsearch.c.centroid
662 .intersects(VIEWBOX2_PARAM,
663 use_index=details.viewbox.area < 0.5))
665 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM, use_index=False), 0.0),
666 (t.c.geometry.intersects(VIEWBOX2_PARAM, use_index=False), 0.5),
669 if details.near is not None:
670 if details.near_radius is not None:
671 if details.near_radius < 0.1:
672 sql = sql.where(tsearch.c.centroid.within_distance(NEAR_PARAM,
675 sql = sql.where(tsearch.c.centroid
676 .ST_Distance(NEAR_PARAM) < NEAR_RADIUS_PARAM)
677 sql = sql.add_columns((-tsearch.c.centroid.ST_Distance(NEAR_PARAM))
678 .label('importance'))
679 sql = sql.order_by(sa.desc(sa.text('importance')))
681 if self.expected_count < 10000\
682 or (details.viewbox is not None and details.viewbox.area < 0.5):
684 penalty - sa.case((tsearch.c.importance > 0, tsearch.c.importance),
685 else_=0.75001-(sa.cast(tsearch.c.search_rank, sa.Float())/40)))
686 sql = sql.add_columns(t.c.importance)
689 sql = sql.add_columns(penalty.label('accuracy'))
691 if self.expected_count < 10000:
692 sql = sql.order_by(sa.text('accuracy'))
694 if self.housenumbers:
695 hnr_regexp = f"\\m({'|'.join(self.housenumbers.values)})\\M"
696 sql = sql.where(tsearch.c.address_rank.between(16, 30))\
697 .where(sa.or_(tsearch.c.address_rank < 30,
698 t.c.housenumber.op('~*')(hnr_regexp)))
700 # Cross check for housenumbers, need to do that on a rather large
701 # set. Worst case there are 40.000 main streets in OSM.
702 inner = sql.limit(10000).subquery()
704 # Housenumbers from placex
705 thnr = conn.t.placex.alias('hnr')
706 pid_list = array_agg(thnr.c.place_id) # type: ignore[no-untyped-call]
707 place_sql = sa.select(pid_list)\
708 .where(thnr.c.parent_place_id == inner.c.place_id)\
709 .where(thnr.c.housenumber.op('~*')(hnr_regexp))\
710 .where(thnr.c.linked_place_id == None)\
711 .where(thnr.c.indexed_status == 0)
714 place_sql = place_sql.where(thnr.c.place_id.not_in(sa.bindparam('excluded')))
716 place_sql = place_sql.where(self.qualifiers.sql_restrict(thnr))
718 numerals = [int(n) for n in self.housenumbers.values
719 if n.isdigit() and len(n) < 8]
720 interpol_sql: SaColumn
723 (not self.qualifiers or ('place', 'house') in self.qualifiers.values):
724 # Housenumbers from interpolations
725 interpol_sql = _make_interpolation_subquery(conn.t.osmline, inner,
727 # Housenumbers from Tiger
728 tiger_sql = sa.case((inner.c.country_code == 'us',
729 _make_interpolation_subquery(conn.t.tiger, inner,
733 interpol_sql = sa.null()
734 tiger_sql = sa.null()
736 unsort = sa.select(inner, place_sql.scalar_subquery().label('placex_hnr'),
737 interpol_sql.label('interpol_hnr'),
738 tiger_sql.label('tiger_hnr')).subquery('unsort')
739 sql = sa.select(unsort)\
740 .order_by(sa.case((unsort.c.placex_hnr != None, 1),
741 (unsort.c.interpol_hnr != None, 2),
742 (unsort.c.tiger_hnr != None, 3),
746 sql = sql.where(t.c.linked_place_id == None)\
747 .where(t.c.indexed_status == 0)
749 sql = sql.where(self.qualifiers.sql_restrict(t))
751 sql = sql.where(_exclude_places(tsearch))
752 if details.min_rank > 0:
753 sql = sql.where(sa.or_(tsearch.c.address_rank >= MIN_RANK_PARAM,
754 tsearch.c.search_rank >= MIN_RANK_PARAM))
755 if details.max_rank < 30:
756 sql = sql.where(sa.or_(tsearch.c.address_rank <= MAX_RANK_PARAM,
757 tsearch.c.search_rank <= MAX_RANK_PARAM))
758 if details.layers is not None:
759 sql = sql.where(_filter_by_layer(t, details.layers))
761 sql = sql.limit(LIMIT_PARAM)
763 results = nres.SearchResults()
764 for row in await conn.execute(sql, _details_to_bind_params(details)):
765 result = nres.create_from_placex_row(row, nres.SearchResult)
767 result.bbox = Bbox.from_wkb(row.bbox)
768 result.accuracy = row.accuracy
769 if self.housenumbers and row.rank_address < 30:
771 subs = _get_placex_housenumbers(conn, row.placex_hnr, details)
772 elif row.interpol_hnr:
773 subs = _get_osmline(conn, row.interpol_hnr, numerals, details)
775 subs = _get_tiger(conn, row.tiger_hnr, numerals, row.osm_id, details)
780 async for sub in subs:
781 assert sub.housenumber
782 sub.accuracy = result.accuracy
783 if not any(nr in self.housenumbers.values
784 for nr in sub.housenumber.split(';')):
788 # Only add the street as a result, if it meets all other
790 if (not details.excluded or result.place_id not in details.excluded)\
791 and (not self.qualifiers or result.category in self.qualifiers.values)\
792 and result.rank_address >= details.min_rank:
793 result.accuracy += 1.0 # penalty for missing housenumber
794 results.append(result)
796 results.append(result)