]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/api/search/db_searches.py
move get_addressdata() implementation to Python
[nominatim.git] / nominatim / api / search / db_searches.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Implementation of the acutal database accesses for forward search.
9 """
10 from typing import List, Tuple, AsyncIterator, Dict, Any, Callable
11 import abc
12
13 import sqlalchemy as sa
14 from sqlalchemy.dialects.postgresql import ARRAY, array_agg
15
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
23
24 #pylint: disable=singleton-comparison,not-callable
25 #pylint: disable=too-many-branches,too-many-arguments,too-many-locals,too-many-statements
26
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.
30     """
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,
36             'near': details.near,
37             'near_radius': details.near_radius,
38             'excluded': details.excluded,
39             'countries': details.countries}
40
41
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')
50
51 def _within_near(t: SaFromClause) -> Callable[[], SaExpression]:
52     return lambda: t.c.geometry.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM)
53
54 def _exclude_places(t: SaFromClause) -> Callable[[], SaExpression]:
55     return lambda: t.c.place_id.not_in(sa.bindparam('excluded'))
56
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,
59                      t.c.class_, t.c.type,
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,
65                      t.c.centroid,
66                      t.c.geometry.ST_Expand(0).label('bbox'))
67
68
69 def _add_geometry_columns(sql: SaLambdaSelect, col: SaColumn, details: SearchDetails) -> SaSelect:
70     out = []
71
72     if details.geometry_simplification > 0.0:
73         col = sa.func.ST_SimplifyPreserveTopology(col, details.geometry_simplification)
74
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'))
83
84     return sql.add_columns(*out)
85
86
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)
91
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)
95     else:
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)
99                   for n in numerals)))
100
101     if details.excluded:
102         sql = sql.where(_exclude_places(table))
103
104     return sql.scalar_subquery()
105
106
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'))))
119
120     if layers & DataLayer.MANMADE:
121         exclude = []
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))
128     else:
129         include = []
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))
136
137     if len(orexpr) == 1:
138         return orexpr[0]
139
140     return sa.or_(*orexpr)
141
142
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)
145     return sa.case(
146             (table.c.endnumber == table.c.startnumber, table.c.linegeo.ST_Centroid()),
147             else_=table.c.linegeo.ST_LineInterpolatePoint(pos)).label('centroid')
148
149
150 async def _get_placex_housenumbers(conn: SearchConnection,
151                                    place_ids: List[int],
152                                    details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
153     t = conn.t.placex
154     sql = _select_placex(t).where(t.c.place_id.in_(place_ids))
155
156     if details.geometry_output:
157         sql = _add_geometry_columns(sql, t.c.geometry, details)
158
159     for row in await conn.execute(sql):
160         result = nres.create_from_placex_row(row, nres.SearchResult)
161         assert result
162         result.bbox = Bbox.from_wkb(row.bbox)
163         yield result
164
165
166 async def _get_osmline(conn: SearchConnection, place_ids: List[int],
167                        numerals: List[int],
168                        details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
169     t = conn.t.osmline
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))
179
180     if details.geometry_output:
181         sub = sql.subquery()
182         sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
183
184     for row in await conn.execute(sql):
185         result = nres.create_from_osmline_row(row, nres.SearchResult)
186         assert result
187         yield result
188
189
190 async def _get_tiger(conn: SearchConnection, place_ids: List[int],
191                      numerals: List[int], osm_id: int,
192                      details: SearchDetails) -> AsyncIterator[nres.SearchResult]:
193     t = conn.t.tiger
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),
201                     t.c.postcode)\
202             .where(t.c.place_id.in_(place_ids))\
203             .join(values, values.c.nr.between(t.c.startnumber, t.c.endnumber))
204
205     if details.geometry_output:
206         sub = sql.subquery()
207         sql = _add_geometry_columns(sa.select(sub), sub.c.centroid, details)
208
209     for row in await conn.execute(sql):
210         result = nres.create_from_tiger_row(row, nres.SearchResult)
211         assert result
212         yield result
213
214
215 class AbstractSearch(abc.ABC):
216     """ Encapuslation of a single lookup in the database.
217     """
218
219     def __init__(self, penalty: float) -> None:
220         self.penalty = penalty
221
222     @abc.abstractmethod
223     async def lookup(self, conn: SearchConnection,
224                      details: SearchDetails) -> nres.SearchResults:
225         """ Find results for the search in the database.
226         """
227
228
229 class NearSearch(AbstractSearch):
230     """ Category search of a place type near the result of another search.
231     """
232     def __init__(self, penalty: float, categories: WeightedCategories,
233                  search: AbstractSearch) -> None:
234         super().__init__(penalty)
235         self.search = search
236         self.categories = categories
237
238
239     async def lookup(self, conn: SearchConnection,
240                      details: SearchDetails) -> nres.SearchResults:
241         """ Find results for the search in the database.
242         """
243         results = nres.SearchResults()
244         base = await self.search.lookup(conn, details)
245
246         if not base:
247             return results
248
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)
254
255         if base:
256             baseids = [b.place_id for b in base[:5] if b.place_id]
257
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:
261                     break
262
263         return results
264
265
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'.
272         """
273         table = await conn.get_class_table(*category)
274
275         t = conn.t.placex
276         tgeom = conn.t.placex.alias('pgeom')
277
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])
281
282         if table is None:
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))
286         else:
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)\
290                      .join(tgeom,
291                            table.c.centroid.ST_CoveredBy(
292                                sa.case((sa.and_(tgeom.c.rank_address < 9,
293                                                 tgeom.c.geometry.is_area()),
294                                         tgeom.c.geometry),
295                                        else_ = tgeom.c.centroid.ST_Expand(0.05))))\
296                      .order_by(tgeom.c.centroid.ST_Distance(table.c.centroid))
297
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))
301         if details.excluded:
302             sql = sql.where(_exclude_places(t))
303         if details.layers is not None:
304             sql = sql.where(_filter_by_layer(t, details.layers))
305
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)
309             assert result
310             result.accuracy = self.penalty + penalty
311             result.bbox = Bbox.from_wkb(row.bbox)
312             results.append(result)
313
314
315
316 class PoiSearch(AbstractSearch):
317     """ Category search in a geographic area.
318     """
319     def __init__(self, sdata: SearchData) -> None:
320         super().__init__(sdata.penalty)
321         self.qualifiers = sdata.qualifiers
322         self.countries = sdata.countries
323
324
325     async def lookup(self, conn: SearchConnection,
326                      details: SearchDetails) -> nres.SearchResults:
327         """ Find results for the search in the database.
328         """
329         bind_params = _details_to_bind_params(details)
330         t = conn.t.placex
331
332         rows: List[SaRow] = []
333
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)) \
341                            .limit(LIMIT_PARAM)
342
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))
349             else:
350                 sql = _base_query().where(sa.or_(*(sa.and_(t.c.class_ == cls, t.c.type == typ)
351                                                    for cls, typ in classtype)))
352
353             if self.countries:
354                 sql = sql.where(t.c.country_code.in_(self.countries.values))
355
356             if details.viewbox is not None and details.bounded_viewbox:
357                 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
358
359             rows.extend(await conn.execute(sql, bind_params))
360         else:
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])
369
370                     if details.viewbox is not None and details.bounded_viewbox:
371                         sql = sql.where(table.c.centroid.intersects(VIEWBOX_PARAM))
372
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,
376                                                                     NEAR_RADIUS_PARAM))
377
378                     if self.countries:
379                         sql = sql.where(t.c.country_code.in_(self.countries.values))
380
381                     sql = sql.limit(LIMIT_PARAM)
382                     rows.extend(await conn.execute(sql, bind_params))
383
384         results = nres.SearchResults()
385         for row in rows:
386             result = nres.create_from_placex_row(row, nres.SearchResult)
387             assert result
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)
391
392         return results
393
394
395 class CountrySearch(AbstractSearch):
396     """ Search for a country name or country code.
397     """
398     def __init__(self, sdata: SearchData) -> None:
399         super().__init__(sdata.penalty)
400         self.countries = sdata.countries
401
402
403     async def lookup(self, conn: SearchConnection,
404                      details: SearchDetails) -> nres.SearchResults:
405         """ Find results for the search in the database.
406         """
407         t = conn.t.placex
408
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)
413
414         if details.geometry_output:
415             sql = _add_geometry_columns(sql, t.c.geometry, details)
416
417         if details.excluded:
418             sql = sql.where(_exclude_places(t))
419
420         if details.viewbox is not None and details.bounded_viewbox:
421             sql = sql.where(lambda: t.c.geometry.intersects(VIEWBOX_PARAM))
422
423         if details.near is not None and details.near_radius is not None:
424             sql = sql.where(_within_near(t))
425
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)
429             assert result
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)
433
434         return results or await self.lookup_in_country_table(conn, details)
435
436
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.
440         """
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.
444         if details.excluded:
445             return nres.SearchResults()
446
447         t = conn.t.country_name
448         tgrid = conn.t.country_grid
449
450         sql = sa.select(tgrid.c.country_code,
451                         tgrid.c.geometry.ST_Centroid().ST_Collect().ST_Centroid()
452                               .label('centroid'))\
453                 .where(tgrid.c.country_code.in_(self.countries.values))\
454                 .group_by(tgrid.c.country_code)
455
456         if details.viewbox is not None and details.bounded_viewbox:
457             sql = sql.where(tgrid.c.geometry.intersects(VIEWBOX_PARAM))
458         if details.near is not None and details.near_radius is not None:
459             sql = sql.where(_within_near(tgrid))
460
461         sub = sql.subquery('grid')
462
463         sql = sa.select(t.c.country_code,
464                         (t.c.name
465                          + sa.func.coalesce(t.c.derived_name,
466                                             sa.cast('', type_=conn.t.types.Composite))
467                         ).label('name'),
468                         sub.c.centroid)\
469                 .join(sub, t.c.country_code == sub.c.country_code)
470
471         results = nres.SearchResults()
472         for row in await conn.execute(sql, _details_to_bind_params(details)):
473             result = nres.create_from_country_row(row, nres.SearchResult)
474             assert result
475             result.accuracy = self.penalty + self.countries.get_penalty(row.country_code, 5.0)
476             results.append(result)
477
478         return results
479
480
481
482 class PostcodeSearch(AbstractSearch):
483     """ Search for a postcode.
484     """
485     def __init__(self, extra_penalty: float, sdata: SearchData) -> None:
486         super().__init__(sdata.penalty + extra_penalty)
487         self.countries = sdata.countries
488         self.postcodes = sdata.postcodes
489         self.lookups = sdata.lookups
490         self.rankings = sdata.rankings
491
492
493     async def lookup(self, conn: SearchConnection,
494                      details: SearchDetails) -> nres.SearchResults:
495         """ Find results for the search in the database.
496         """
497         t = conn.t.postcode
498         pcs = self.postcodes.values
499
500         sql = sa.select(t.c.place_id, t.c.parent_place_id,
501                         t.c.rank_search, t.c.rank_address,
502                         t.c.postcode, t.c.country_code,
503                         t.c.geometry.label('centroid'))\
504                 .where(t.c.postcode.in_(pcs))
505
506         if details.geometry_output:
507             sql = _add_geometry_columns(sql, t.c.geometry, details)
508
509         penalty: SaExpression = sa.literal(self.penalty)
510
511         if details.viewbox is not None:
512             if details.bounded_viewbox:
513                 sql = sql.where(t.c.geometry.intersects(VIEWBOX_PARAM))
514             else:
515                 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
516                                    (t.c.geometry.intersects(VIEWBOX2_PARAM), 1.0),
517                                    else_=2.0)
518
519         if details.near is not None:
520             if details.near_radius is not None:
521                 sql = sql.where(_within_near(t))
522             sql = sql.order_by(t.c.geometry.ST_Distance(NEAR_PARAM))
523
524         if self.countries:
525             sql = sql.where(t.c.country_code.in_(self.countries.values))
526
527         if details.excluded:
528             sql = sql.where(_exclude_places(t))
529
530         if self.lookups:
531             assert len(self.lookups) == 1
532             assert self.lookups[0].lookup_type == 'restrict'
533             tsearch = conn.t.search_name
534             sql = sql.where(tsearch.c.place_id == t.c.parent_place_id)\
535                      .where(sa.func.array_cat(tsearch.c.name_vector,
536                                               tsearch.c.nameaddress_vector,
537                                               type_=ARRAY(sa.Integer))
538                                     .contains(self.lookups[0].tokens))
539
540         for ranking in self.rankings:
541             penalty += ranking.sql_penalty(conn.t.search_name)
542         penalty += sa.case(*((t.c.postcode == v, p) for v, p in self.postcodes),
543                        else_=1.0)
544
545
546         sql = sql.add_columns(penalty.label('accuracy'))
547         sql = sql.order_by('accuracy').limit(LIMIT_PARAM)
548
549         results = nres.SearchResults()
550         for row in await conn.execute(sql, _details_to_bind_params(details)):
551             result = nres.create_from_postcode_row(row, nres.SearchResult)
552             assert result
553             result.accuracy = row.accuracy
554             results.append(result)
555
556         return results
557
558
559
560 class PlaceSearch(AbstractSearch):
561     """ Generic search for an address or named place.
562     """
563     def __init__(self, extra_penalty: float, sdata: SearchData, expected_count: int) -> None:
564         super().__init__(sdata.penalty + extra_penalty)
565         self.countries = sdata.countries
566         self.postcodes = sdata.postcodes
567         self.housenumbers = sdata.housenumbers
568         self.qualifiers = sdata.qualifiers
569         self.lookups = sdata.lookups
570         self.rankings = sdata.rankings
571         self.expected_count = expected_count
572
573
574     async def lookup(self, conn: SearchConnection,
575                      details: SearchDetails) -> nres.SearchResults:
576         """ Find results for the search in the database.
577         """
578         t = conn.t.placex
579         tsearch = conn.t.search_name
580
581         sql: SaLambdaSelect = sa.lambda_stmt(lambda:
582                   sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name,
583                             t.c.class_, t.c.type,
584                             t.c.address, t.c.extratags, t.c.admin_level,
585                             t.c.housenumber, t.c.postcode, t.c.country_code,
586                             t.c.wikipedia,
587                             t.c.parent_place_id, t.c.rank_address, t.c.rank_search,
588                             t.c.centroid,
589                             t.c.geometry.ST_Expand(0).label('bbox'))
590                    .where(t.c.place_id == tsearch.c.place_id))
591
592
593         if details.geometry_output:
594             sql = _add_geometry_columns(sql, t.c.geometry, details)
595
596         penalty: SaExpression = sa.literal(self.penalty)
597         for ranking in self.rankings:
598             penalty += ranking.sql_penalty(tsearch)
599
600         for lookup in self.lookups:
601             sql = sql.where(lookup.sql_condition(tsearch))
602
603         if self.countries:
604             sql = sql.where(tsearch.c.country_code.in_(self.countries.values))
605
606         if self.postcodes:
607             # if a postcode is given, don't search for state or country level objects
608             sql = sql.where(tsearch.c.address_rank > 9)
609             tpc = conn.t.postcode
610             pcs = self.postcodes.values
611             if self.expected_count > 1000:
612                 # Many results expected. Restrict by postcode.
613                 sql = sql.where(sa.select(tpc.c.postcode)
614                                   .where(tpc.c.postcode.in_(pcs))
615                                   .where(tsearch.c.centroid.ST_DWithin(tpc.c.geometry, 0.12))
616                                   .exists())
617
618             # Less results, only have a preference for close postcodes
619             pc_near = sa.select(sa.func.min(tpc.c.geometry.ST_Distance(tsearch.c.centroid)))\
620                       .where(tpc.c.postcode.in_(pcs))\
621                       .scalar_subquery()
622             penalty += sa.case((t.c.postcode.in_(pcs), 0.0),
623                                else_=sa.func.coalesce(pc_near, 2.0))
624
625         if details.viewbox is not None:
626             if details.bounded_viewbox:
627                 if details.viewbox.area < 0.2:
628                     sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX_PARAM))
629                 else:
630                     sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX_PARAM))
631             elif self.expected_count >= 10000:
632                 if details.viewbox.area < 0.5:
633                     sql = sql.where(tsearch.c.centroid.intersects(VIEWBOX2_PARAM))
634                 else:
635                     sql = sql.where(tsearch.c.centroid.ST_Intersects_no_index(VIEWBOX2_PARAM))
636             else:
637                 penalty += sa.case((t.c.geometry.intersects(VIEWBOX_PARAM), 0.0),
638                                    (t.c.geometry.intersects(VIEWBOX2_PARAM), 1.0),
639                                    else_=2.0)
640
641         if details.near is not None:
642             if details.near_radius is not None:
643                 if details.near_radius < 0.1:
644                     sql = sql.where(tsearch.c.centroid.ST_DWithin(NEAR_PARAM, NEAR_RADIUS_PARAM))
645                 else:
646                     sql = sql.where(tsearch.c.centroid.ST_DWithin_no_index(NEAR_PARAM,
647                                                                            NEAR_RADIUS_PARAM))
648             sql = sql.add_columns((-tsearch.c.centroid.ST_Distance(NEAR_PARAM))
649                                       .label('importance'))
650             sql = sql.order_by(sa.desc(sa.text('importance')))
651         else:
652             if self.expected_count < 10000\
653                or (details.viewbox is not None and details.viewbox.area < 0.5):
654                 sql = sql.order_by(
655                         penalty - sa.case((tsearch.c.importance > 0, tsearch.c.importance),
656                                     else_=0.75001-(sa.cast(tsearch.c.search_rank, sa.Float())/40)))
657             sql = sql.add_columns(t.c.importance)
658
659
660         sql = sql.add_columns(penalty.label('accuracy'))
661
662         if self.expected_count < 10000:
663             sql = sql.order_by(sa.text('accuracy'))
664
665         if self.housenumbers:
666             hnr_regexp = f"\\m({'|'.join(self.housenumbers.values)})\\M"
667             sql = sql.where(tsearch.c.address_rank.between(16, 30))\
668                      .where(sa.or_(tsearch.c.address_rank < 30,
669                                    t.c.housenumber.op('~*')(hnr_regexp)))
670
671             # Cross check for housenumbers, need to do that on a rather large
672             # set. Worst case there are 40.000 main streets in OSM.
673             inner = sql.limit(10000).subquery()
674
675             # Housenumbers from placex
676             thnr = conn.t.placex.alias('hnr')
677             pid_list = array_agg(thnr.c.place_id) # type: ignore[no-untyped-call]
678             place_sql = sa.select(pid_list)\
679                           .where(thnr.c.parent_place_id == inner.c.place_id)\
680                           .where(thnr.c.housenumber.op('~*')(hnr_regexp))\
681                           .where(thnr.c.linked_place_id == None)\
682                           .where(thnr.c.indexed_status == 0)
683
684             if details.excluded:
685                 place_sql = place_sql.where(thnr.c.place_id.not_in(sa.bindparam('excluded')))
686             if self.qualifiers:
687                 place_sql = place_sql.where(self.qualifiers.sql_restrict(thnr))
688
689             numerals = [int(n) for n in self.housenumbers.values
690                         if n.isdigit() and len(n) < 8]
691             interpol_sql: SaColumn
692             tiger_sql: SaColumn
693             if numerals and \
694                (not self.qualifiers or ('place', 'house') in self.qualifiers.values):
695                 # Housenumbers from interpolations
696                 interpol_sql = _make_interpolation_subquery(conn.t.osmline, inner,
697                                                             numerals, details)
698                 # Housenumbers from Tiger
699                 tiger_sql = sa.case((inner.c.country_code == 'us',
700                                      _make_interpolation_subquery(conn.t.tiger, inner,
701                                                                   numerals, details)
702                                     ), else_=None)
703             else:
704                 interpol_sql = sa.null()
705                 tiger_sql = sa.null()
706
707             unsort = sa.select(inner, place_sql.scalar_subquery().label('placex_hnr'),
708                                interpol_sql.label('interpol_hnr'),
709                                tiger_sql.label('tiger_hnr')).subquery('unsort')
710             sql = sa.select(unsort)\
711                     .order_by(sa.case((unsort.c.placex_hnr != None, 1),
712                                       (unsort.c.interpol_hnr != None, 2),
713                                       (unsort.c.tiger_hnr != None, 3),
714                                       else_=4),
715                               unsort.c.accuracy)
716         else:
717             sql = sql.where(t.c.linked_place_id == None)\
718                      .where(t.c.indexed_status == 0)
719             if self.qualifiers:
720                 sql = sql.where(self.qualifiers.sql_restrict(t))
721             if details.excluded:
722                 sql = sql.where(_exclude_places(tsearch))
723             if details.min_rank > 0:
724                 sql = sql.where(sa.or_(tsearch.c.address_rank >= MIN_RANK_PARAM,
725                                        tsearch.c.search_rank >= MIN_RANK_PARAM))
726             if details.max_rank < 30:
727                 sql = sql.where(sa.or_(tsearch.c.address_rank <= MAX_RANK_PARAM,
728                                        tsearch.c.search_rank <= MAX_RANK_PARAM))
729             if details.layers is not None:
730                 sql = sql.where(_filter_by_layer(t, details.layers))
731
732         sql = sql.limit(LIMIT_PARAM)
733
734         results = nres.SearchResults()
735         for row in await conn.execute(sql, _details_to_bind_params(details)):
736             result = nres.create_from_placex_row(row, nres.SearchResult)
737             assert result
738             result.bbox = Bbox.from_wkb(row.bbox)
739             result.accuracy = row.accuracy
740             if not details.excluded or not result.place_id in details.excluded:
741                 results.append(result)
742
743             if self.housenumbers and row.rank_address < 30:
744                 if row.placex_hnr:
745                     subs = _get_placex_housenumbers(conn, row.placex_hnr, details)
746                 elif row.interpol_hnr:
747                     subs = _get_osmline(conn, row.interpol_hnr, numerals, details)
748                 elif row.tiger_hnr:
749                     subs = _get_tiger(conn, row.tiger_hnr, numerals, row.osm_id, details)
750                 else:
751                     subs = None
752
753                 if subs is not None:
754                     async for sub in subs:
755                         assert sub.housenumber
756                         sub.accuracy = result.accuracy
757                         if not any(nr in self.housenumbers.values
758                                    for nr in sub.housenumber.split(';')):
759                             sub.accuracy += 0.6
760                         results.append(sub)
761
762                 result.accuracy += 1.0 # penalty for missing housenumber
763
764         return results