X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/37488ee82b0fc99ab7d86d0d8eafc4257a63c8ff..c41f2fed2133668dc3179813261d39d3ff69cbdd:/nominatim/db/sqlalchemy_functions.py?ds=sidebyside diff --git a/nominatim/db/sqlalchemy_functions.py b/nominatim/db/sqlalchemy_functions.py index 6eed8803..cb04f762 100644 --- a/nominatim/db/sqlalchemy_functions.py +++ b/nominatim/db/sqlalchemy_functions.py @@ -15,19 +15,107 @@ from sqlalchemy.ext.compiler import compiles from nominatim.typing import SaColumn -# pylint: disable=abstract-method,missing-function-docstring,consider-using-f-string +# pylint: disable=all -def select_index_placex_geometry_reverse_lookuppolygon(table: str) -> 'sa.TextClause': - """ Create an expression with the necessary conditions over a placex - table that the index 'idx_placex_geometry_reverse_lookupPolygon' - can be used. +class PlacexGeometryReverseLookuppolygon(sa.sql.functions.GenericFunction[Any]): + """ Check for conditions that allow partial index use on + 'idx_placex_geometry_reverse_lookupPolygon'. + + Needs to be constant, so that the query planner picks them up correctly + in prepared statements. """ - return sa.text(f"ST_GeometryType({table}.geometry) in ('ST_Polygon', 'ST_MultiPolygon')" - f" AND {table}.rank_address between 4 and 25" - f" AND {table}.type != 'postcode'" - f" AND {table}.name is not null" - f" AND {table}.indexed_status = 0" - f" AND {table}.linked_place_id is null") + name = 'PlacexGeometryReverseLookuppolygon' + inherit_cache = True + + +@compiles(PlacexGeometryReverseLookuppolygon) # type: ignore[no-untyped-call, misc] +def _default_intersects(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + return ("(ST_GeometryType(placex.geometry) in ('ST_Polygon', 'ST_MultiPolygon')" + " AND placex.rank_address between 4 and 25" + " AND placex.type != 'postcode'" + " AND placex.name is not null" + " AND placex.indexed_status = 0" + " AND placex.linked_place_id is null)") + + +@compiles(PlacexGeometryReverseLookuppolygon, 'sqlite') # type: ignore[no-untyped-call, misc] +def _sqlite_intersects(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + return ("(ST_GeometryType(placex.geometry) in ('POLYGON', 'MULTIPOLYGON')" + " AND placex.rank_address between 4 and 25" + " AND placex.type != 'postcode'" + " AND placex.name is not null" + " AND placex.indexed_status = 0" + " AND placex.linked_place_id is null)") + + +class IntersectsReverseDistance(sa.sql.functions.GenericFunction[Any]): + name = 'IntersectsReverseDistance' + inherit_cache = True + + def __init__(self, table: sa.Table, geom: SaColumn) -> None: + super().__init__(table.c.geometry, # type: ignore[no-untyped-call] + table.c.rank_search, geom) + self.tablename = table.name + + +@compiles(IntersectsReverseDistance) # type: ignore[no-untyped-call, misc] +def default_reverse_place_diameter(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + table = element.tablename + return f"({table}.rank_address between 4 and 25"\ + f" AND {table}.type != 'postcode'"\ + f" AND {table}.name is not null"\ + f" AND {table}.linked_place_id is null"\ + f" AND {table}.osm_type = 'N'" + \ + " AND ST_Buffer(%s, reverse_place_diameter(%s)) && %s)" % \ + tuple(map(lambda c: compiler.process(c, **kw), element.clauses)) + + +@compiles(IntersectsReverseDistance, 'sqlite') # type: ignore[no-untyped-call, misc] +def sqlite_reverse_place_diameter(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + geom1, rank, geom2 = list(element.clauses) + table = element.tablename + + return (f"({table}.rank_address between 4 and 25"\ + f" AND {table}.type != 'postcode'"\ + f" AND {table}.name is not null"\ + f" AND {table}.linked_place_id is null"\ + f" AND {table}.osm_type = 'N'"\ + " AND MbrIntersects(%s, ST_Expand(%s, 14.0 * exp(-0.2 * %s) - 0.03))"\ + f" AND {table}.place_id IN"\ + " (SELECT place_id FROM placex_place_node_areas"\ + " WHERE ROWID IN (SELECT ROWID FROM SpatialIndex"\ + " WHERE f_table_name = 'placex_place_node_areas'"\ + " AND search_frame = %s)))") % ( + compiler.process(geom1, **kw), + compiler.process(geom2, **kw), + compiler.process(rank, **kw), + compiler.process(geom2, **kw)) + + +class IsBelowReverseDistance(sa.sql.functions.GenericFunction[Any]): + name = 'IsBelowReverseDistance' + inherit_cache = True + + +@compiles(IsBelowReverseDistance) # type: ignore[no-untyped-call, misc] +def default_is_below_reverse_distance(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + dist, rank = list(element.clauses) + return "%s < reverse_place_diameter(%s)" % (compiler.process(dist, **kw), + compiler.process(rank, **kw)) + + +@compiles(IsBelowReverseDistance, 'sqlite') # type: ignore[no-untyped-call, misc] +def sqlite_is_below_reverse_distance(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + dist, rank = list(element.clauses) + return "%s < 14.0 * exp(-0.2 * %s) - 0.03" % (compiler.process(dist, **kw), + compiler.process(rank, **kw)) + def select_index_placex_geometry_reverse_lookupplacenode(table: str) -> 'sa.TextClause': """ Create an expression with the necessary conditions over a placex @@ -41,11 +129,39 @@ def select_index_placex_geometry_reverse_lookupplacenode(table: str) -> 'sa.Text f" AND {table}.osm_type = 'N'") -class CrosscheckNames(sa.sql.functions.GenericFunction[bool]): +class IsAddressPoint(sa.sql.functions.GenericFunction[Any]): + name = 'IsAddressPoint' + inherit_cache = True + + def __init__(self, table: sa.Table) -> None: + super().__init__(table.c.rank_address, # type: ignore[no-untyped-call] + table.c.housenumber, table.c.name) + + +@compiles(IsAddressPoint) # type: ignore[no-untyped-call, misc] +def default_is_address_point(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + rank, hnr, name = list(element.clauses) + return "(%s = 30 AND (%s IS NOT NULL OR %s ? 'addr:housename'))" % ( + compiler.process(rank, **kw), + compiler.process(hnr, **kw), + compiler.process(name, **kw)) + + +@compiles(IsAddressPoint, 'sqlite') # type: ignore[no-untyped-call, misc] +def sqlite_is_address_point(element: SaColumn, + compiler: 'sa.Compiled', **kw: Any) -> str: + rank, hnr, name = list(element.clauses) + return "(%s = 30 AND coalesce(%s, json_extract(%s, '$.addr:housename')) IS NOT NULL)" % ( + compiler.process(rank, **kw), + compiler.process(hnr, **kw), + compiler.process(name, **kw)) + + +class CrosscheckNames(sa.sql.functions.GenericFunction[Any]): """ Check if in the given list of names in parameters 1 any of the names from the JSON array in parameter 2 are contained. """ - type = sa.Boolean() name = 'CrosscheckNames' inherit_cache = True