-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomparator.py
364 lines (300 loc) · 11.4 KB
/
comparator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from sqlalchemy import types as sqltypes
from sqlalchemy.types import UserDefinedType
from sqlalchemy.dialects.postgresql import DOUBLE_PRECISION
from sqlalchemy.sql import operators
try:
from sqlalchemy.sql.functions import _FunctionGenerator
except ImportError: # SQLA < 0.9 # pragma: no cover
from sqlalchemy.sql.expression import _FunctionGenerator
EVER_EQUAL_TO = operators.custom_op("?=")
ALWAYS_EQUAL_TO = operators.custom_op("%=")
EVER_DIFFERENT_FROM = operators.custom_op("?<>")
ALWAYS_DIFFERENT_FROM = operators.custom_op("@<>")
EVER_LESS_THAN = operators.custom_op("?<")
EVER_GREATER_THAN = operators.custom_op("?>")
EVER_LESS_THAN_OR_EQUAL_TO = operators.custom_op("?<=")
EVER_GREATER_THAN_OR_EQUAL_TO = operators.custom_op("?>=")
ALWAYS_LESS_THAN = operators.custom_op("%<")
ALWAYS_GREATER_THAN = operators.custom_op("%>")
ALWAYS_LESS_THAN_OR_EQUAL_TO = operators.custom_op("%<=")
ALWAYS_GREATER_THAN_OR_EQUAL_TO = operators.custom_op("%>=")
TEMPORAL_EQUAL = operators.custom_op("#=")
TEMPORAL_NOT_EQUAL = operators.custom_op("#<>")
TEMPORAL_LESS_THAN = operators.custom_op("#<")
TEMPORAL_GREATER_THAN = operators.custom_op("#>")
TEMPORAL_LESS_THAN_OR_EQUAL_TO = operators.custom_op("#<=")
TEMPORAL_GREATER_THAN_OR_EQUAL_TO = operators.custom_op("#>=")
BBOX_ALWAYS_STRICTLY_LESS_THAN = operators.custom_op("<<")
BBOX_ALWAYS_STRICTLY_GREATER_THAN = operators.custom_op(">>")
BBOX_NEVER_GREATER_THAN = operators.custom_op("&<")
BBOX_NEVER_LESS_THAN = operators.custom_op("&>")
BBOX_STRICTLY_TO_LEFT = operators.custom_op("<<")
BBOX_STRICTLY_TO_RIGHT = operators.custom_op(">>")
BBOX_NOT_TO_THE_RIGHT_OF = operators.custom_op("&<")
BBOX_NOT_TO_THE_LEFT_OF = operators.custom_op("&>")
BBOX_STRICTLY_BELOW = operators.custom_op("<<|")
BBOX_STRICTLY_ABOVE = operators.custom_op("|>>")
BBOX_DOES_NOT_EXTEND_TO_LEFT = operators.custom_op("&>")
BBOX_DOES_NOT_EXTEND_TO_RIGHT = operators.custom_op("&<")
BBOX_DOES_NOT_EXTEND_BELOW = operators.custom_op("&<|")
BBOX_DOES_NOT_EXTEND_ABOVE = operators.custom_op("|&>")
BBOX_STRICTLY_IN_FRONT = operators.custom_op("<</")
BBOX_STRICTLY_IN_BACK = operators.custom_op("/>>")
BBOX_DOES_NOT_EXTEND_IN_FRONT = operators.custom_op("&</")
BBOX_DOES_NOT_EXTEND_IN_BACK = operators.custom_op("/&>")
BBOX_ALWAYS_BEFORE = operators.custom_op("<<#")
BBOX_ALWAYS_AFTER = operators.custom_op("#>>")
BBOX_NEVER_AFTER = operators.custom_op("&<#")
BBOX_NEVER_BEFORE = operators.custom_op("#&>")
BBOX_CONTAINS = operators.custom_op("@>")
BBOX_CONTAINED = operators.custom_op("<@")
BBOXES_OVERLAP = operators.custom_op("&&")
BBOXES_EQUAL = operators.custom_op("~=")
SMALLEST_DISTANCE_EVER_BETWEEN = operators.custom_op("|=|")
DISTANCE = operators.custom_op("<->")
class Comparator(UserDefinedType.Comparator):
"""
A custom comparator base class. It adds the ability to call spatial and
temporal functions on columns that use this kind of comparator. It also
defines functions that map to operators supported by ``TBool``, ``TInt``,
``TFloat`` and ``TGeomPoint`` columns.
"""
def __lshift__(self, other):
return self.bbox_strictly_to_left(other)
def __rshift__(self, other):
return self.bbox_strictly_to_right(other)
def ever_equal_to(self, other):
"""
The "?=" operator.
Is lhs ever equal to the rhs?
The function does not take into account whether the bounds are inclusive or not.
"""
return self.operate(EVER_EQUAL_TO, other, is_comparision=True)
def always_equal_to(self, other):
"""
The "%=" operator.
Is lhs always equal to the rhs?
The function does not take into account whether the bounds are inclusive or not.
"""
return self.operate(ALWAYS_EQUAL_TO, other, is_comparision=True)
def ever_different_from(self, other):
"""
The "?<>" operator.
"""
return self.operate(EVER_DIFFERENT_FROM, other, is_comparision=True)
def always_different_from(self, other):
"""
The "@<>" operator.
"""
return self.operate(ALWAYS_DIFFERENT_FROM, other, is_comparision=True)
def ever_less_than(self, other):
"""
The "?<" operator.
"""
return self.operate(EVER_LESS_THAN, other, is_comparision=True)
def ever_greater_than(self, other):
"""
The "?>" operator.
"""
return self.operate(EVER_GREATER_THAN, other, is_comparision=True)
def ever_less_than_or_equal_to(self, other):
"""
The "?<=" operator.
"""
return self.operate(EVER_LESS_THAN_OR_EQUAL_TO, other, is_comparision=True)
def ever_greater_than_or_equal_to(self, other):
"""
The "?>=" operator.
"""
return self.operate(EVER_GREATER_THAN_OR_EQUAL_TO, other, is_comparision=True)
def always_less_than(self, other):
"""
The "%<" operator.
"""
return self.operate(ALWAYS_LESS_THAN, other, is_comparision=True)
def always_greater_than(self, other):
"""
The "%>" operator.
"""
return self.operate(ALWAYS_GREATER_THAN, other, is_comparision=True)
def always_less_than_or_equal_to(self, other):
"""
The "%<=" operator.
"""
return self.operate(ALWAYS_LESS_THAN_OR_EQUAL_TO, other, is_comparision=True)
def always_greater_than_or_equal_to(self, other):
"""
The "%>=" operator.
"""
return self.operate(ALWAYS_GREATER_THAN_OR_EQUAL_TO, other, is_comparision=True)
def temporal_equal(self, other):
"""
The "#=" operator.
"""
from mobilitydb_sqlalchemy.types.TBool import TBool
return self.operate(TEMPORAL_EQUAL, other, result_type=TBool)
def temporal_not_equal(self, other):
"""
The "#<>" operator.
"""
from mobilitydb_sqlalchemy.types.TBool import TBool
return self.operate(TEMPORAL_NOT_EQUAL, other, result_type=TBool)
def temporal_less_than(self, other):
"""
The "#<" operator.
"""
from mobilitydb_sqlalchemy.types.TBool import TBool
return self.operate(TEMPORAL_LESS_THAN, other, result_type=TBool)
def temporal_greater_than(self, other):
"""
The "#>" operator.
"""
from mobilitydb_sqlalchemy.types.TBool import TBool
return self.operate(TEMPORAL_GREATER_THAN, other, result_type=TBool)
def temporal_less_than_or_equal_to(self, other):
"""
The "#<=" operator.
"""
from mobilitydb_sqlalchemy.types.TBool import TBool
return self.operate(TEMPORAL_LESS_THAN_OR_EQUAL_TO, other, result_type=TBool)
def temporal_greater_than_or_equal_to(self, other):
"""
The "#>=" operator.
"""
from mobilitydb_sqlalchemy.types.TBool import TBool
return self.operate(TEMPORAL_GREATER_THAN_OR_EQUAL_TO, other, result_type=TBool)
def bbox_always_strictly_less_than(self, other):
"""
The "<<" operator.
"""
return self.operate(BBOX_ALWAYS_STRICTLY_LESS_THAN, other, is_comparision=True)
def bbox_always_strictly_greater_than(self, other):
"""
The ">>" operator.
"""
return self.operate(
BBOX_ALWAYS_STRICTLY_GREATER_THAN, other, is_comparision=True
)
def bbox_never_greater_than(self, other):
"""
The "&<" operator.
"""
return self.operate(BBOX_NEVER_GREATER_THAN, other, is_comparision=True)
def bbox_never_less_than(self, other):
"""
The "&>" operator.
"""
return self.operate(BBOX_NEVER_LESS_THAN, other, is_comparision=True)
def bbox_strictly_to_left(self, other):
"""
The "<<" operator.
"""
return self.operate(BBOX_STRICTLY_TO_LEFT, other, is_comparision=True)
def bbox_strictly_to_right(self, other):
"""
The ">>" operator.
"""
return self.operate(BBOX_STRICTLY_TO_RIGHT, other, is_comparision=True)
def bbox_strictly_below(self, other):
"""
The "<<|" operator.
"""
return self.operate(BBOX_STRICTLY_BELOW, other, is_comparision=True)
def bbox_strictly_above(self, other):
"""
The "|>>" operator.
"""
return self.operate(BBOX_STRICTLY_ABOVE, other, is_comparision=True)
def bbox_does_not_extend_to_left(self, other):
"""
The "&>" operator.
"""
return self.operate(BBOX_DOES_NOT_EXTEND_TO_LEFT, other, is_comparision=True)
def bbox_does_not_extend_to_right(self, other):
"""
The "&<" operator.
"""
return self.operate(BBOX_DOES_NOT_EXTEND_TO_RIGHT, other, is_comparision=True)
def bbox_does_not_extend_below(self, other):
"""
The "&<|" operator.
"""
return self.operate(BBOX_DOES_NOT_EXTEND_BELOW, other, is_comparision=True)
def bbox_does_not_extend_above(self, other):
"""
The "|&>" operator.
"""
return self.operate(BBOX_DOES_NOT_EXTEND_ABOVE, other, is_comparision=True)
def bbox_strictly_in_front(self, other):
"""
The "<</" operator.
"""
return self.operate(BBOX_STRICTLY_IN_FRONT, other, is_comparision=True)
def bbox_strictly_in_back(self, other):
"""
The "/>>" operator.
"""
return self.operate(BBOX_STRICTLY_IN_BACK, other, is_comparision=True)
def bbox_does_not_extend_in_front(self, other):
"""
The "&</" operator.
"""
return self.operate(BBOX_DOES_NOT_EXTEND_IN_FRONT, other, is_comparision=True)
def bbox_does_not_extend_in_back(self, other):
"""
The "/&>" operator.
"""
return self.operate(BBOX_DOES_NOT_EXTEND_IN_BACK, other, is_comparision=True)
def bbox_always_before(self, other):
"""
The "<<#" operator.
"""
return self.operate(BBOX_ALWAYS_BEFORE, other, is_comparision=True)
def bbox_always_after(self, other):
"""
The "<<#" operator.
"""
return self.operate(BBOX_ALWAYS_AFTER, other, is_comparision=True)
def bbox_never_after(self, other):
"""
The "&<#" operator.
"""
return self.operate(BBOX_NEVER_AFTER, other, is_comparision=True)
def bbox_never_before(self, other):
"""
The "#&>" operator.
"""
return self.operate(BBOX_NEVER_BEFORE, other, is_comparision=True)
def bbox_contains(self, other):
"""
The "@>" operator.
"""
return self.operate(BBOX_CONTAINS, other, is_comparision=True)
def bbox_contained(self, other):
"""
The "<@" operator.
"""
return self.operate(BBOX_CONTAINED, other, is_comparision=True)
def bboxes_overlap(self, other):
"""
The "&&" operator.
"""
return self.operate(BBOXES_OVERLAP, other, is_comparision=True)
def bboxes_equal(self, other):
"""
The "~=" operator.
"""
return self.operate(BBOXES_EQUAL, other, is_comparision=True)
def smallest_distance_ever_between(self, other):
"""
The "|=|" operator.
"""
return self.operate(
SMALLEST_DISTANCE_EVER_BETWEEN, other, result_type=sqltypes.Float
)
def distance(self, other):
"""
The "<->" operator.
"""
from mobilitydb_sqlalchemy.types.TFloat import TFloat
return self.operate(DISTANCE, other, result_type=TFloat)