mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-14 14:54:06 +00:00
make reverse API work with sqlite
This commit is contained in:
@@ -15,19 +15,110 @@ 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[bool]):
|
||||
""" 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")
|
||||
type = sa.Boolean()
|
||||
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[bool]):
|
||||
type = sa.Boolean()
|
||||
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[bool]):
|
||||
type = sa.Boolean()
|
||||
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
|
||||
@@ -84,6 +175,22 @@ def sqlite_json_array_each(element: SaColumn, compiler: 'sa.Compiled', **kw: Any
|
||||
return "json_each(%s)" % compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
class JsonHasKey(sa.sql.functions.GenericFunction[bool]):
|
||||
""" Return elements of a json array as a set.
|
||||
"""
|
||||
type = sa.Boolean()
|
||||
name = 'JsonHasKey'
|
||||
inherit_cache = True
|
||||
|
||||
|
||||
@compiles(JsonHasKey) # type: ignore[no-untyped-call, misc]
|
||||
def compile_json_has_key(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
arg1, arg2 = list(element.clauses)
|
||||
return "%s->%s is not null" % (compiler.process(arg1, **kw),
|
||||
compiler.process(arg2, **kw))
|
||||
|
||||
|
||||
class Greatest(sa.sql.functions.GenericFunction[Any]):
|
||||
""" Function to compute maximum of all its input parameters.
|
||||
"""
|
||||
|
||||
@@ -13,6 +13,7 @@ import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import HSTORE, ARRAY, JSONB, array
|
||||
from sqlalchemy.dialects.sqlite import JSON as sqlite_json
|
||||
|
||||
import nominatim.db.sqlalchemy_functions #pylint: disable=unused-import
|
||||
from nominatim.db.sqlalchemy_types import Geometry
|
||||
|
||||
class PostgresTypes:
|
||||
|
||||
@@ -41,6 +41,71 @@ def _spatialite_distance_spheroid(element: SaColumn,
|
||||
return "Distance(%s, true)" % compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
class Geometry_IsLineLike(sa.sql.expression.FunctionElement[bool]):
|
||||
""" Check if the geometry is a line or multiline.
|
||||
"""
|
||||
type = sa.Boolean()
|
||||
name = 'Geometry_IsLineLike'
|
||||
inherit_cache = True
|
||||
|
||||
|
||||
@compiles(Geometry_IsLineLike) # type: ignore[no-untyped-call, misc]
|
||||
def _default_is_line_like(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
return "ST_GeometryType(%s) IN ('ST_LineString', 'ST_MultiLineString')" % \
|
||||
compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
@compiles(Geometry_IsLineLike, 'sqlite') # type: ignore[no-untyped-call, misc]
|
||||
def _sqlite_is_line_like(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
return "ST_GeometryType(%s) IN ('LINESTRING', 'MULTILINESTRING')" % \
|
||||
compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
class Geometry_IsAreaLike(sa.sql.expression.FunctionElement[bool]):
|
||||
""" Check if the geometry is a polygon or multipolygon.
|
||||
"""
|
||||
type = sa.Boolean()
|
||||
name = 'Geometry_IsLineLike'
|
||||
inherit_cache = True
|
||||
|
||||
|
||||
@compiles(Geometry_IsAreaLike) # type: ignore[no-untyped-call, misc]
|
||||
def _default_is_area_like(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
return "ST_GeometryType(%s) IN ('ST_Polygon', 'ST_MultiPolygon')" % \
|
||||
compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
@compiles(Geometry_IsAreaLike, 'sqlite') # type: ignore[no-untyped-call, misc]
|
||||
def _sqlite_is_area_like(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
return "ST_GeometryType(%s) IN ('POLYGON', 'MULTIPOLYGON')" % \
|
||||
compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
class Geometry_IntersectsBbox(sa.sql.expression.FunctionElement[bool]):
|
||||
""" Check if the bounding boxes of the given geometries intersect.
|
||||
"""
|
||||
type = sa.Boolean()
|
||||
name = 'Geometry_IntersectsBbox'
|
||||
inherit_cache = True
|
||||
|
||||
|
||||
@compiles(Geometry_IntersectsBbox) # type: ignore[no-untyped-call, misc]
|
||||
def _default_intersects(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
arg1, arg2 = list(element.clauses)
|
||||
return "%s && %s" % (compiler.process(arg1, **kw), compiler.process(arg2, **kw))
|
||||
|
||||
|
||||
@compiles(Geometry_IntersectsBbox, 'sqlite') # type: ignore[no-untyped-call, misc]
|
||||
def _sqlite_intersects(element: SaColumn,
|
||||
compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
return "MbrIntersects(%s)" % compiler.process(element.clauses, **kw)
|
||||
|
||||
|
||||
class Geometry(types.UserDefinedType): # type: ignore[type-arg]
|
||||
""" Simplified type decorator for PostGIS geometry. This type
|
||||
only supports geometries in 4326 projection.
|
||||
@@ -82,15 +147,15 @@ class Geometry(types.UserDefinedType): # type: ignore[type-arg]
|
||||
class comparator_factory(types.UserDefinedType.Comparator): # type: ignore[type-arg]
|
||||
|
||||
def intersects(self, other: SaColumn) -> 'sa.Operators':
|
||||
return self.op('&&')(other)
|
||||
return Geometry_IntersectsBbox(self, other)
|
||||
|
||||
|
||||
def is_line_like(self) -> SaColumn:
|
||||
return sa.func.ST_GeometryType(self, type_=sa.String).in_(('ST_LineString',
|
||||
'ST_MultiLineString'))
|
||||
return Geometry_IsLineLike(self)
|
||||
|
||||
|
||||
def is_area(self) -> SaColumn:
|
||||
return sa.func.ST_GeometryType(self, type_=sa.String).in_(('ST_Polygon',
|
||||
'ST_MultiPolygon'))
|
||||
return Geometry_IsAreaLike(self)
|
||||
|
||||
|
||||
def ST_DWithin(self, other: SaColumn, distance: SaColumn) -> SaColumn:
|
||||
@@ -119,7 +184,8 @@ class Geometry(types.UserDefinedType): # type: ignore[type-arg]
|
||||
|
||||
|
||||
def ST_ClosestPoint(self, other: SaColumn) -> SaColumn:
|
||||
return sa.func.ST_ClosestPoint(self, other, type_=Geometry)
|
||||
return sa.func.coalesce(sa.func.ST_ClosestPoint(self, other, type_=Geometry),
|
||||
other)
|
||||
|
||||
|
||||
def ST_Buffer(self, other: SaColumn) -> SaColumn:
|
||||
@@ -161,11 +227,13 @@ SQLITE_FUNCTION_ALIAS = (
|
||||
('ST_AsGeoJSON', sa.Text, 'AsGeoJSON'),
|
||||
('ST_AsKML', sa.Text, 'AsKML'),
|
||||
('ST_AsSVG', sa.Text, 'AsSVG'),
|
||||
('ST_LineLocatePoint', sa.Float, 'ST_Line_Locate_Point'),
|
||||
('ST_LineInterpolatePoint', sa.Float, 'ST_Line_Interpolate_Point'),
|
||||
)
|
||||
|
||||
def _add_function_alias(func: str, ftype: type, alias: str) -> None:
|
||||
_FuncDef = type(func, (sa.sql.functions.GenericFunction, ), {
|
||||
"type": ftype,
|
||||
"type": ftype(),
|
||||
"name": func,
|
||||
"identifier": func,
|
||||
"inherit_cache": True})
|
||||
@@ -181,4 +249,17 @@ for alias in SQLITE_FUNCTION_ALIAS:
|
||||
_add_function_alias(*alias)
|
||||
|
||||
|
||||
class ST_DWithin(sa.sql.functions.GenericFunction[bool]):
|
||||
type = sa.Boolean()
|
||||
name = 'ST_DWithin'
|
||||
inherit_cache = True
|
||||
|
||||
|
||||
@compiles(ST_DWithin, 'sqlite') # type: ignore[no-untyped-call, misc]
|
||||
def default_json_array_each(element: SaColumn, compiler: 'sa.Compiled', **kw: Any) -> str:
|
||||
geom1, geom2, dist = list(element.clauses)
|
||||
return "(MbrIntersects(%s, ST_Expand(%s, %s)) = 1 AND ST_Distance(%s, %s) <= %s)" % (
|
||||
compiler.process(geom1, **kw), compiler.process(geom2, **kw),
|
||||
compiler.process(dist, **kw),
|
||||
compiler.process(geom1, **kw), compiler.process(geom2, **kw),
|
||||
compiler.process(dist, **kw))
|
||||
|
||||
Reference in New Issue
Block a user