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 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')
58 def _within_near(t: SaFromClause) -> Callable[[], SaExpression]:
59 return lambda: t.c.geometry.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM)
61 def _exclude_places(t: SaFromClause) -> Callable[[], SaExpression]:
62 return lambda: t.c.place_id.not_in(sa.bindparam('excluded'))
64 def _select_placex(t: SaFromClause) -> SaSelect:
65 return sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name,
67 t.c.address, t.c.extratags,
68 t.c.housenumber, t.c.postcode, t.c.country_code,
70 t.c.parent_place_id, t.c.rank_address, t.c.rank_search,
71 t.c.linked_place_id, t.c.admin_level,
73 t.c.geometry.ST_Expand(0).label('bbox'))
76 def _add_geometry_columns(sql: SaLambdaSelect, col: SaColumn, details: SearchDetails) -> SaSelect:
79 if details.geometry_simplification > 0.0:
80 col = sa.func.ST_SimplifyPreserveTopology(col, details.geometry_simplification)
82 if details.geometry_output & GeometryFormat.GEOJSON:
83 out.append(sa.func.ST_AsGeoJSON(col, 7).label('geometry_geojson'))
84 if details.geometry_output & GeometryFormat.TEXT:
85 out.append(sa.func.ST_AsText(col).label('geometry_text'))
86 if details.geometry_output & GeometryFormat.KML:
87 out.append(sa.func.ST_AsKML(col, 7).label('geometry_kml'))
88 if details.geometry_output & GeometryFormat.SVG:
89 out.append(sa.func.ST_AsSVG(col, 0, 7).label('geometry_svg'))
91 return sql.add_columns(*out)
94 def _make_interpolation_subquery(table: SaFromClause, inner: SaFromClause,
95 numerals: List[int], details: SearchDetails) -> SaScalarSelect:
96 all_ids = array_agg(table.c.place_id) # type: ignore[no-untyped-call]
97 sql = sa.select(all_ids).where(table.c.parent_place_id == inner.c.place_id)
99 if len(numerals) == 1:
100 sql = sql.where(sa.between(numerals[0], table.c.startnumber, table.c.endnumber))\
101 .where((numerals[0] - table.c.startnumber) % table.c.step == 0)
103 sql = sql.where(sa.or_(
104 *(sa.and_(sa.between(n, table.c.startnumber, table.c.endnumber),
105 (n - table.c.startnumber) % table.c.step == 0)
109 sql = sql.where(_exclude_places(table))
111 return sql.scalar_subquery()
114 def _filter_by_layer(table: SaFromClause, layers: DataLayer) -> SaColumn:
115 orexpr: List[SaExpression] = []
116 if layers & DataLayer.ADDRESS and layers & DataLayer.POI:
117 orexpr.append(no_index(table.c.rank_address).between(1, 30))
118 elif layers & DataLayer.ADDRESS:
119 orexpr.append(no_index(table.c.rank_address).between(1, 29))
120 orexpr.append(sa.and_(no_index(table.c.rank_address) == 30,
121 sa.or_(table.c.housenumber != None,
122 table.c.address.has_key('addr:housename'))))
123 elif layers & DataLayer.POI:
124 orexpr.append(sa.and_(no_index(table.c.rank_address) == 30,
125 table.c.class_.not_in(('place', 'building'))))
127 if layers & DataLayer.MANMADE:
129 if not layers & DataLayer.RAILWAY:
130 exclude.append('railway')
131 if not layers & DataLayer.NATURAL:
132 exclude.extend(('natural', 'water', 'waterway'))
133 orexpr.append(sa.and_(table.c.class_.not_in(tuple(exclude)),
134 no_index(table.c.rank_address) == 0))
137 if layers & DataLayer.RAILWAY:
138 include.append('railway')
139 if layers & DataLayer.NATURAL:
140 include.extend(('natural', 'water', 'waterway'))
141 orexpr.append(sa.and_(table.c.class_.in_(tuple(include)),
142 no_index(table.c.rank_address) == 0))
147 return sa.or_(*orexpr)
150 def _interpolated_position(table: SaFromClause, nr: SaColumn) -> SaColumn:
151 pos = sa.cast(nr - table.c.startnumber, sa.Float) / (table.c.endnumber - table.c.startnumber)
153 (table.c.endnumber == table.c.startnumber, table.c.linegeo.ST_Centroid()),
154 else_=table.c.linegeo.ST_LineInterpolatePoint(pos)).label('centroid')
157 async def _get_placex_housenumbers(conn: SearchConnection,
158 place_ids: List[int],
159 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
161 sql = _select_placex(t).add_columns(t.c.importance)\
162 .where(t.c.place_id.in_(place_ids))
164 if details.geometry_output:
165 sql = _add_geometry_columns(sql, t.c.geometry, details)
167 for row in await conn.execute(sql):
168 result = nres.create_from_placex_row(row, nres.SearchResult)
170 result.bbox = Bbox.from_wkb(row.bbox)
174 async def _get_osmline(conn: SearchConnection, place_ids: List[int],
176 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
178 values = sa.values(sa.Column('nr', sa.Integer()), name='housenumber')\
179 .data([(n,) for n in numerals])
180 sql = sa.select(t.c.place_id, t.c.osm_id,
181 t.c.parent_place_id, t.c.address,
182 values.c.nr.label('housenumber'),
183 _interpolated_position(t, values.c.nr),
184 t.c.postcode, t.c.country_code)\
185 .where(t.c.place_id.in_(place_ids))\
186 .join(values, values.c.nr.between(t.c.startnumber, t.c.endnumber))
188 if details.geometry_output:
190 sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
192 for row in await conn.execute(sql):
193 result = nres.create_from_osmline_row(row, nres.SearchResult)
198 async def _get_tiger(conn: SearchConnection, place_ids: List[int],
199 numerals: List[int], osm_id: int,
200 details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
202 values = sa.values(sa.Column('nr', sa.Integer()), name='housenumber')\
203 .data([(n,) for n in numerals])
204 sql = sa.select(t.c.place_id, t.c.parent_place_id,
205 sa.literal('W').label('osm_type'),
206 sa.literal(osm_id).label('osm_id'),
207 values.c.nr.label('housenumber'),
208 _interpolated_position(t, values.c.nr),
210 .where(t.c.place_id.in_(place_ids))\
211 .join(values, values.c.nr.between(t.c.startnumber, t.c.endnumber))
213 if details.geometry_output:
215 sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
217 for row in await conn.execute(sql):
218 result = nres.create_from_tiger_row(row, nres.SearchResult)
223 class AbstractSearch(abc.ABC):
224 """ Encapuslation of a single lookup in the database.
227 def __init__(self, penalty: float) -> None:
228 self.penalty = penalty
231 async def lookup(self, conn: SearchConnection,
232 details: SearchDetails) -> nres.SearchResults:
233 """ Find results for the search in the database.
237 class NearSearch(AbstractSearch):
238 """ Category search of a place type near the result of another search.
240 def __init__(self, penalty: float, categories: WeightedCategories,
241 search: AbstractSearch) -> None:
242 super().__init__(penalty)
244 self.categories = categories
247 async def lookup(self, conn: SearchConnection,
248 details: SearchDetails) -> nres.SearchResults:
249 """ Find results for the search in the database.
251 results = nres.SearchResults()
252 base = await self.search.lookup(conn, details)
257 base.sort(key=lambda r: (r.accuracy, r.rank_search))
258 max_accuracy = base[0].accuracy + 0.5
259 base = nres.SearchResults(r for r in base if r.source_table == nres.SourceTable.PLACEX
260 and r.accuracy <= max_accuracy
261 and r.bbox and r.bbox.area < 20)
264 baseids = [b.place_id for b in base[:5] if b.place_id]
266 for category, penalty in self.categories:
267 await self.lookup_category(results, conn, baseids, category, penalty, details)
268 if len(results) >= details.max_results:
274 async def lookup_category(self, results: nres.SearchResults,
275 conn: SearchConnection, ids: List[int],
276 category: Tuple[str, str], penalty: float,
277 details: SearchDetails) -> None:
278 """ Find places of the given category near the list of
279 place ids and add the results to 'results'.
281 table = await conn.get_class_table(*category)
283 tgeom = conn.t.placex.alias('pgeom')
286 # No classtype table available, do a simplified lookup in placex.
287 table = conn.t.placex.alias('inner')
288 sql = sa.select(table.c.place_id,
289 sa.func.min(tgeom.c.centroid.ST_Distance(table.c.centroid))
291 .join(tgeom, table.c.geometry.intersects(tgeom.c.centroid.ST_Expand(0.01)))\
292 .where(table.c.class_ == category[0])\
293 .where(table.c.type == category[1])
295 # Use classtype table. We can afford to use a larger
296 # radius for the lookup.
297 sql = sa.select(table.c.place_id,
298 sa.func.min(tgeom.c.centroid.ST_Distance(table.c.centroid))
301 table.c.centroid.ST_CoveredBy(
302 sa.case((sa.and_(tgeom.c.rank_address > 9,
303 tgeom.c.geometry.is_area()),
305 else_ = tgeom.c.centroid.ST_Expand(0.05))))
307 inner = sql.where(tgeom.c.place_id.in_(ids))\
308 .group_by(table.c.place_id).subquery()
311 sql = _select_placex(t).add_columns((-inner.c.dist).label('importance'))\
312 .join(inner, inner.c.place_id == t.c.place_id)\
313 .order_by(inner.c.dist)
315 sql = sql.where(no_index(t.c.rank_address).between(MIN_RANK_PARAM, MAX_RANK_PARAM))
316 if details.countries:
317 sql = sql.where(t.c.country_code.in_(COUNTRIES_PARAM))
319 sql = sql.where(_exclude_places(t))
320 if details.layers is not None:
321 sql = sql.where(_filter_by_layer(t, details.layers))
323 sql = sql.limit(LIMIT_PARAM)
324 for row in await conn.execute(sql, _details_to_bind_params(details)):
325 result = nres.create_from_placex_row(row, nres.SearchResult)
327 result.accuracy = self.penalty + penalty
328 result.bbox = Bbox.from_wkb(row.bbox)
329 results.append(result)
333 class PoiSearch(AbstractSearch):
334 """ Category search in a geographic area.
336 def __init__(self, sdata: SearchData) -> None:
337 super().__init__(sdata.penalty)
338 self.qualifiers = sdata.qualifiers
339 self.countries = sdata.countries
342 async def lookup(self, conn: SearchConnection,
343 details: SearchDetails) -> nres.SearchResults:
344 """ Find results for the search in the database.
346 bind_params = _details_to_bind_params(details)
349 rows: List[SaRow] = []
351 if details.near and details.near_radius is not None and details.near_radius < 0.2:
352 # simply search in placex table
353 def _base_query() -> SaSelect:
354 return _select_placex(t) \
355 .add_columns((-t.c.centroid.ST_Distance(NEAR_PARAM))
356 .label('importance'))\
357 .where(t.c.linked_place_id == None) \
358 .where(t.c.geometry.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM)) \
359 .order_by(t.c.centroid.ST_Distance(NEAR_PARAM)) \
362 classtype = self.qualifiers.values
363 if len(classtype) == 1:
364 cclass, ctype = classtype[0]
365 sql: SaLambdaSelect = sa.lambda_stmt(lambda: _base_query()
366 .where(t.c.class_ == cclass)
367 .where(t.c.type == ctype))
369 sql = _base_query().where(sa.or_(*(sa.and_(t.c.class_ == cls, t.c.type == typ)
370 for cls, typ in classtype)))
373 sql = sql.where(t.c.country_code.in_(self.countries.values))
375 if details.viewbox is not None and details.bounded_viewbox:
376 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
378 rows.extend(await conn.execute(sql, bind_params))
380 # use the class type tables
381 for category in self.qualifiers.values:
382 table = await conn.get_class_table(*category)
383 if table is not None:
384 sql = _select_placex(t)\
385 .add_columns(t.c.importance)\
386 .join(table, t.c.place_id == table.c.place_id)\
387 .where(t.c.class_ == category[0])\
388 .where(t.c.type == category[1])
390 if details.viewbox is not None and details.bounded_viewbox:
391 sql = sql.where(table.c.centroid.intersects(VIEWBOX_PARAM))
393 if details.near and details.near_radius is not None:
394 sql = sql.order_by(table.c.centroid.ST_Distance(NEAR_PARAM))\
395 .where(table.c.centroid.ST_DWithin(NEAR_PARAM,
399 sql = sql.where(t.c.country_code.in_(self.countries.values))
401 sql = sql.limit(LIMIT_PARAM)
402 rows.extend(await conn.execute(sql, bind_params))
404 results = nres.SearchResults()
406 result = nres.create_from_placex_row(row, nres.SearchResult)
408 result.accuracy = self.penalty + self.qualifiers.get_penalty((row.class_, row.type))
409 result.bbox = Bbox.from_wkb(row.bbox)
410 results.append(result)
415 class CountrySearch(AbstractSearch):
416 """ Search for a country name or country code.
418 def __init__(self, sdata: SearchData) -> None:
419 super().__init__(sdata.penalty)
420 self.countries = sdata.countries
423 async def lookup(self, conn: SearchConnection,
424 details: SearchDetails) -> nres.SearchResults:
425 """ Find results for the search in the database.
429 ccodes = self.countries.values
430 sql = _select_placex(t)\
431 .add_columns(t.c.importance)\
432 .where(t.c.country_code.in_(ccodes))\
433 .where(t.c.rank_address == 4)
435 if details.geometry_output:
436 sql = _add_geometry_columns(sql, t.c.geometry, details)
439 sql = sql.where(_exclude_places(t))
441 if details.viewbox is not None and details.bounded_viewbox:
442 sql = sql.where(lambda: t.c.geometry.intersects(VIEWBOX_PARAM))
444 if details.near is not None and details.near_radius is not None:
445 sql = sql.where(_within_near(t))
447 results = nres.SearchResults()
448 for row in await conn.execute(sql, _details_to_bind_params(details)):
449 result = nres.create_from_placex_row(row, nres.SearchResult)
451 result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
452 result.bbox = Bbox.from_wkb(row.bbox)
453 results.append(result)
455 return results or await self.lookup_in_country_table(conn, details)
458 async def lookup_in_country_table(self, conn: SearchConnection,
459 details: SearchDetails) -> nres.SearchResults:
460 """ Look up the country in the fallback country tables.
462 # Avoid the fallback search when this is a more search. Country results
463 # usually are in the first batch of results and it is not possible
464 # to exclude these fallbacks.
466 return nres.SearchResults()
468 t = conn.t.country_name
469 tgrid = conn.t.country_grid
471 sql = sa.select(tgrid.c.country_code,
472 tgrid.c.geometry.ST_Centroid().ST_Collect().ST_Centroid()
474 tgrid.c.geometry.ST_Collect().ST_Expand(0).label('bbox'))\
475 .where(tgrid.c.country_code.in_(self.countries.values))\
476 .group_by(tgrid.c.country_code)
478 if details.viewbox is not None and details.bounded_viewbox:
479 sql = sql.where(tgrid.c.geometry.intersects(VIEWBOX_PARAM))
480 if details.near is not None and details.near_radius is not None:
481 sql = sql.where(_within_near(tgrid))
483 sub = sql.subquery('grid')
485 sql = sa.select(t.c.country_code,
487 + sa.func.coalesce(t.c.derived_name,
488 sa.cast('', type_=conn.t.types.Composite))
490 sub.c.centroid, sub.c.bbox)\
491 .join(sub, t.c.country_code == sub.c.country_code)
493 if details.geometry_output:
494 sql = _add_geometry_columns(sql, sub.c.centroid, details)
496 results = nres.SearchResults()
497 for row in await conn.execute(sql, _details_to_bind_params(details)):
498 result = nres.create_from_country_row(row, nres.SearchResult)
500 result.bbox = Bbox.from_wkb(row.bbox)
501 result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
502 results.append(result)
508 class PostcodeSearch(AbstractSearch):
509 """ Search for a postcode.
511 def __init__(self, extra_penalty: float, sdata: SearchData) -> None:
512 super().__init__(sdata.penalty + extra_penalty)
513 self.countries = sdata.countries
514 self.postcodes = sdata.postcodes
515 self.lookups = sdata.lookups
516 self.rankings = sdata.rankings
519 async def lookup(self, conn: SearchConnection,
520 details: SearchDetails) -> nres.SearchResults:
521 """ Find results for the search in the database.
524 pcs = self.postcodes.values
526 sql = sa.select(t.c.place_id, t.c.parent_place_id,
527 t.c.rank_search, t.c.rank_address,
528 t.c.postcode, t.c.country_code,
529 t.c.geometry.label('centroid'))\
530 .where(t.c.postcode.in_(pcs))
532 if details.geometry_output:
533 sql = _add_geometry_columns(sql, t.c.geometry, details)
535 penalty: SaExpression = sa.literal(self.penalty)
537 if details.viewbox is not None:
538 if details.bounded_viewbox:
539 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
541 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
542 (t.c.geometry.intersects(VIEWBOX2_PARAM), 0.5),
545 if details.near is not None:
546 if details.near_radius is not None:
547 sql = sql.where(_within_near(t))
548 sql = sql.order_by(t.c.geometry.ST_Distance(NEAR_PARAM))
551 sql = sql.where(t.c.country_code.in_(self.countries.values))
554 sql = sql.where(_exclude_places(t))
557 assert len(self.lookups) == 1
558 assert self.lookups[0].lookup_type == 'restrict'
559 tsearch = conn.t.search_name
560 sql = sql.where(tsearch.c.place_id == t.c.parent_place_id)\
561 .where(sa.func.array_cat(tsearch.c.name_vector,
562 tsearch.c.nameaddress_vector,
563 type_=ARRAY(sa.Integer))
564 .contains(self.lookups[0].tokens))
566 for ranking in self.rankings:
567 penalty += ranking.sql_penalty(conn.t.search_name)
568 penalty += sa.case(*((t.c.postcode == v, p) for v, p in self.postcodes),
572 sql = sql.add_columns(penalty.label('accuracy'))
573 sql = sql.order_by('accuracy').limit(LIMIT_PARAM)
575 results = nres.SearchResults()
576 for row in await conn.execute(sql, _details_to_bind_params(details)):
577 result = nres.create_from_postcode_row(row, nres.SearchResult)
579 result.accuracy = row.accuracy
580 results.append(result)
586 class PlaceSearch(AbstractSearch):
587 """ Generic search for an address or named place.
589 def __init__(self, extra_penalty: float, sdata: SearchData, expected_count: int) -> None:
590 super().__init__(sdata.penalty + extra_penalty)
591 self.countries = sdata.countries
592 self.postcodes = sdata.postcodes
593 self.housenumbers = sdata.housenumbers
594 self.qualifiers = sdata.qualifiers
595 self.lookups = sdata.lookups
596 self.rankings = sdata.rankings
597 self.expected_count = expected_count
600 async def lookup(self, conn: SearchConnection,
601 details: SearchDetails) -> nres.SearchResults:
602 """ Find results for the search in the database.
605 tsearch = conn.t.search_name
607 sql: SaLambdaSelect = sa.lambda_stmt(lambda:
608 _select_placex(t).where(t.c.place_id == tsearch.c.place_id))
611 if details.geometry_output:
612 sql = _add_geometry_columns(sql, t.c.geometry, details)
614 penalty: SaExpression = sa.literal(self.penalty)
615 for ranking in self.rankings:
616 penalty += ranking.sql_penalty(tsearch)
618 for lookup in self.lookups:
619 sql = sql.where(lookup.sql_condition(tsearch))
622 sql = sql.where(tsearch.c.country_code.in_(self.countries.values))
625 # if a postcode is given, don't search for state or country level objects
626 sql = sql.where(tsearch.c.address_rank > 9)
627 tpc = conn.t.postcode
628 pcs = self.postcodes.values
629 if self.expected_count > 1000:
630 # Many results expected. Restrict by postcode.
631 sql = sql.where(sa.select(tpc.c.postcode)
632 .where(tpc.c.postcode.in_(pcs))
633 .where(tsearch.c.centroid.ST_DWithin(tpc.c.geometry, 0.12))
636 # Less results, only have a preference for close postcodes
637 pc_near = sa.select(sa.func.min(tpc.c.geometry.ST_Distance(tsearch.c.centroid)))\
638 .where(tpc.c.postcode.in_(pcs))\
640 penalty += sa.case((t.c.postcode.in_(pcs), 0.0),
641 else_=sa.func.coalesce(pc_near, 2.0))
643 if details.viewbox is not None:
644 if details.bounded_viewbox:
645 if details.viewbox.area < 0.2:
646 sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX_PARAM))
648 sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX_PARAM))
649 elif self.expected_count >= 10000:
650 if details.viewbox.area < 0.5:
651 sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX2_PARAM))
653 sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX2_PARAM))
655 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
656 (t.c.geometry.intersects(VIEWBOX2_PARAM), 0.5),
659 if details.near is not None:
660 if details.near_radius is not None:
661 if details.near_radius < 0.1:
662 sql = sql.where(tsearch.c.centroid.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM))
664 sql = sql.where(tsearch.c.centroid.ST_DWithin_no_index(NEAR_PARAM,
666 sql = sql.add_columns((-tsearch.c.centroid.ST_Distance(NEAR_PARAM))
667 .label('importance'))
668 sql = sql.order_by(sa.desc(sa.text('importance')))
670 if self.expected_count < 10000\
671 or (details.viewbox is not None and details.viewbox.area < 0.5):
673 penalty - sa.case((tsearch.c.importance > 0, tsearch.c.importance),
674 else_=0.75001-(sa.cast(tsearch.c.search_rank, sa.Float())/40)))
675 sql = sql.add_columns(t.c.importance)
678 sql = sql.add_columns(penalty.label('accuracy'))
680 if self.expected_count < 10000:
681 sql = sql.order_by(sa.text('accuracy'))
683 if self.housenumbers:
684 hnr_regexp = f"\\m({'|'.join(self.housenumbers.values)})\\M"
685 sql = sql.where(tsearch.c.address_rank.between(16, 30))\
686 .where(sa.or_(tsearch.c.address_rank < 30,
687 t.c.housenumber.op('~*')(hnr_regexp)))
689 # Cross check for housenumbers, need to do that on a rather large
690 # set. Worst case there are 40.000 main streets in OSM.
691 inner = sql.limit(10000).subquery()
693 # Housenumbers from placex
694 thnr = conn.t.placex.alias('hnr')
695 pid_list = array_agg(thnr.c.place_id) # type: ignore[no-untyped-call]
696 place_sql = sa.select(pid_list)\
697 .where(thnr.c.parent_place_id == inner.c.place_id)\
698 .where(thnr.c.housenumber.op('~*')(hnr_regexp))\
699 .where(thnr.c.linked_place_id == None)\
700 .where(thnr.c.indexed_status == 0)
703 place_sql = place_sql.where(thnr.c.place_id.not_in(sa.bindparam('excluded')))
705 place_sql = place_sql.where(self.qualifiers.sql_restrict(thnr))
707 numerals = [int(n) for n in self.housenumbers.values
708 if n.isdigit() and len(n) < 8]
709 interpol_sql: SaColumn
712 (not self.qualifiers or ('place', 'house') in self.qualifiers.values):
713 # Housenumbers from interpolations
714 interpol_sql = _make_interpolation_subquery(conn.t.osmline, inner,
716 # Housenumbers from Tiger
717 tiger_sql = sa.case((inner.c.country_code == 'us',
718 _make_interpolation_subquery(conn.t.tiger, inner,
722 interpol_sql = sa.null()
723 tiger_sql = sa.null()
725 unsort = sa.select(inner, place_sql.scalar_subquery().label('placex_hnr'),
726 interpol_sql.label('interpol_hnr'),
727 tiger_sql.label('tiger_hnr')).subquery('unsort')
728 sql = sa.select(unsort)\
729 .order_by(sa.case((unsort.c.placex_hnr != None, 1),
730 (unsort.c.interpol_hnr != None, 2),
731 (unsort.c.tiger_hnr != None, 3),
735 sql = sql.where(t.c.linked_place_id == None)\
736 .where(t.c.indexed_status == 0)
738 sql = sql.where(self.qualifiers.sql_restrict(t))
740 sql = sql.where(_exclude_places(tsearch))
741 if details.min_rank > 0:
742 sql = sql.where(sa.or_(tsearch.c.address_rank >= MIN_RANK_PARAM,
743 tsearch.c.search_rank >= MIN_RANK_PARAM))
744 if details.max_rank < 30:
745 sql = sql.where(sa.or_(tsearch.c.address_rank <= MAX_RANK_PARAM,
746 tsearch.c.search_rank <= MAX_RANK_PARAM))
747 if details.layers is not None:
748 sql = sql.where(_filter_by_layer(t, details.layers))
750 sql = sql.limit(LIMIT_PARAM)
752 results = nres.SearchResults()
753 for row in await conn.execute(sql, _details_to_bind_params(details)):
754 result = nres.create_from_placex_row(row, nres.SearchResult)
756 result.bbox = Bbox.from_wkb(row.bbox)
757 result.accuracy = row.accuracy
758 if not details.excluded or not result.place_id in details.excluded:
759 results.append(result)
761 if self.housenumbers and row.rank_address < 30:
763 subs = _get_placex_housenumbers(conn, row.placex_hnr, details)
764 elif row.interpol_hnr:
765 subs = _get_osmline(conn, row.interpol_hnr, numerals, details)
767 subs = _get_tiger(conn, row.tiger_hnr, numerals, row.osm_id, details)
772 async for sub in subs:
773 assert sub.housenumber
774 sub.accuracy = result.accuracy
775 if not any(nr in self.housenumbers.values
776 for nr in sub.housenumber.split(';')):
780 result.accuracy += 1.0 # penalty for missing housenumber