]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/db/sqlalchemy_types.py
88cae29f3a4e8982c60b32c33efc668ab88d7053
[nominatim.git] / nominatim / db / sqlalchemy_types.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 Custom types for SQLAlchemy.
9 """
10 from typing import Callable, Any
11
12 import sqlalchemy as sa
13 import sqlalchemy.types as types
14
15 from nominatim.typing import SaColumn
16
17 class Geometry(types.UserDefinedType[Any]):
18     """ Simplified type decorator for PostGIS geometry. This type
19         only supports geometries in 4326 projection.
20     """
21     cache_ok = True
22
23     def __init__(self, subtype: str = 'Geometry'):
24         self.subtype = subtype
25
26
27     def get_col_spec(self) -> str:
28         return f'GEOMETRY({self.subtype}, 4326)'
29
30
31     def bind_processor(self, dialect: sa.Dialect) -> Callable[[Any], str]:
32         def process(value: Any) -> str:
33             assert isinstance(value, str)
34             return value
35         return process
36
37
38     def result_processor(self, dialect: sa.Dialect, coltype: object) -> Callable[[Any], str]:
39         def process(value: Any) -> str:
40             assert isinstance(value, str)
41             return value
42         return process
43
44
45     def bind_expression(self, bindvalue: sa.BindParameter[Any]) -> SaColumn:
46         return sa.func.ST_GeomFromText(bindvalue, type_=self)
47
48
49     class comparator_factory(types.UserDefinedType.Comparator):
50
51         def is_line_like(self) -> SaColumn:
52             return sa.func.ST_GeometryType(self, type_=sa.String).in_(('ST_LineString',
53                                                                        'ST_MultiLineString'))
54
55         def is_area(self) -> SaColumn:
56             return sa.func.ST_GeometryType(self, type_=sa.String).in_(('ST_Polygon',
57                                                                        'ST_MultiPolygon'))
58
59
60         def ST_DWithin(self, other: SaColumn, distance: SaColumn) -> SaColumn:
61             return sa.func.ST_DWithin(self, other, distance, type_=sa.Float)
62
63
64         def ST_Distance(self, other: SaColumn) -> SaColumn:
65             return sa.func.ST_Distance(self, other, type_=sa.Float)
66
67
68         def ST_Contains(self, other: SaColumn) -> SaColumn:
69             return sa.func.ST_Contains(self, other, type_=sa.Float)
70
71
72         def ST_ClosestPoint(self, other: SaColumn) -> SaColumn:
73             return sa.func.ST_ClosestPoint(self, other, type_=Geometry)
74
75
76         def ST_Buffer(self, other: SaColumn) -> SaColumn:
77             return sa.func.ST_Buffer(self, other, type_=Geometry)
78
79
80         def ST_Expand(self, other: SaColumn) -> SaColumn:
81             return sa.func.ST_Expand(self, other, type_=Geometry)
82
83
84         def ST_Centroid(self) -> SaColumn:
85             return sa.func.ST_Centroid(self, type_=Geometry)
86
87
88         def ST_LineInterpolatePoint(self, other: SaColumn) -> SaColumn:
89             return sa.func.ST_LineInterpolatePoint(self, other, type_=Geometry)
90
91
92         def ST_LineLocatePoint(self, other: SaColumn) -> SaColumn:
93             return sa.func.ST_LineLocatePoint(self, other, type_=sa.Float)