-
Notifications
You must be signed in to change notification settings - Fork 5
/
type_tpv.sql
287 lines (254 loc) · 6.01 KB
/
type_tpv.sql
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
drop type if exists tpv cascade;
-- TPV = time, position, velocity
create type tpv as (
-- position
geom geometry(point, 3857),
accuracy float,
-- velocity
heading float,
speed float,
-- time
ts timestamptz,
-- helpers
source text,
osm_id bigint
);
-- convenience functions
create or replace function _tpv_from_json(
p_tpv jsonb
)
returns tpv
as $$
select (
ST_Transform(
ST_SetSRID(
ST_MakePoint(
p_tpv -> 'lon',
p_tpv -> 'lat'
),
4326
),
3857
),
p_tpv -> 'acc',
p_tpv -> 'hdg',
p_tpv -> 'spd',
to_timestamp((p_tpv -> 'ts') :: float / 1000.0),
p_tpv -> 'src',
(p_tpv -> 'osm_id') :: numeric
) :: tpv
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( jsonb as tpv );
create cast ( jsonb as tpv )
with function _tpv_from_json(jsonb);
create or replace function _tpv_from_geometry(
p_geom geometry
)
returns tpv
as $$
select (
ST_Force2D(
ST_Transform(
p_geom,
3857
)
),
null,
null,
null,
null,
null,
null
) :: tpv
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( geometry as tpv );
create cast ( geometry as tpv )
with function _tpv_from_geometry(geometry);
create or replace function _tpvarray_from_geometry(
p_geom geometry
)
returns tpv []
as $$
select array_agg(
(
ST_Force2D(z.geom),
null,
null,
null,
to_timestamp(ST_Z(z.geom)),
null,
null
) :: tpv
)
from ST_DumpPoints(
ST_Transform(
p_geom,
3857
)
) z
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( geometry as tpv [] );
create cast ( geometry as tpv [] )
with function _tpvarray_from_geometry(geometry);
create or replace function _tpv_from_geography(
p_geom geography
)
returns tpv
as $$
select (
ST_Force2D(
ST_Transform(
p_geom :: geometry,
3857
)
),
null,
null,
null,
null,
null,
null
) :: tpv
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( geography as tpv );
create cast ( geography as tpv )
with function _tpv_from_geography(geography);
create or replace function _geometry_from_tpv(
p_tpv tpv
)
returns geometry
as $$
select p_tpv.geom
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( tpv as geometry );
create cast ( tpv as geometry )
with function _geometry_from_tpv(tpv);
create or replace function _geography_from_tpv(
p_tpv tpv
)
returns geography
as $$
select ST_Transform(p_tpv.geom, 4326) :: geography
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( tpv as geography );
create cast ( tpv as geography )
with function _geography_from_tpv(tpv);
create or replace function _geometry_from_tpvarray(
p_tpv tpv []
)
returns geometry
as $$
select ST_MakeLine(
geom
order by ts
)
from unnest(p_tpv) as t
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( tpv [] as geometry );
create cast ( tpv [] as geometry )
with function _geometry_from_tpvarray(tpv []);
create or replace function _geography_from_tpvarray(
p_tpv tpv []
)
returns geography
as $$
select ST_Transform(
ST_MakeLine(
geom
order by ts
),
4326
) :: geography
from unnest(p_tpv) as t
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( tpv [] as geography );
create cast ( tpv [] as geography )
with function _geography_from_tpvarray(tpv []);
create or replace function _tpvarray_from_jsonb(
p_jsonb jsonb
)
returns tpv []
as $$
select array(
(select (
ST_Transform(
ST_SetSRID(
ST_MakePoint(
p_tpv -> 'lon',
p_tpv -> 'lat'
),
4326
),
3857
),
p_tpv -> 'acc',
p_tpv -> 'hdg',
p_tpv -> 'spd',
to_timestamp((p_tpv -> 'ts') / 1000.0),
p_tpv -> 'src',
(p_tpv -> 'osm_id') :: numeric
) :: tpv
from
jsonb_array_elements(p_jsonb) as p_tpv
)
) :: tpv []
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( jsonb as tpv [] );
create cast ( jsonb as tpv [] )
with function _tpvarray_from_jsonb(jsonb);
create or replace function _jsonb_from_tpv(
p_tpv tpv
)
returns jsonb
as $$
select jsonb_strip_nulls(
jsonb_build_object(
'lon', round(ST_X(p_tpv :: geography :: geometry) * 1e12) / 1e12,
'lat', round(ST_Y(p_tpv :: geography :: geometry) * 1e12) / 1e12,
'acc', p_tpv.accuracy,
'hdg', p_tpv.heading,
'spd', p_tpv.speed,
'ts', extract(epoch from p_tpv.ts) * 1000.,
'src', p_tpv.source,
'osm_id', p_tpv.osm_id
)
)
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( tpv as jsonb );
create cast ( tpv as jsonb )
with function _jsonb_from_tpv(tpv);
create or replace function ST_Distance(
p_tpv_1 tpv,
p_tpv_2 tpv
)
returns float
as $$
select ST_Distance(
p_tpv_1 :: geography,
p_tpv_2 :: geography
)
$$ language 'sql' immutable strict parallel safe;
create or replace function _jsonb_from_tpvarray(
p_tpvarray tpv []
)
returns jsonb
as $$
select jsonb_strip_nulls(
jsonb_agg(
jsonb_build_object(
'lon', round(ST_X(p_tpv :: geography :: geometry) * 1e12) / 1e12,
'lat', round(ST_Y(p_tpv :: geography :: geometry) * 1e12) / 1e12,
'acc', p_tpv.accuracy,
'hdg', p_tpv.heading,
'spd', p_tpv.speed,
'ts', extract(epoch from p_tpv.ts) * 1000.,
'src', p_tpv.source,
'osm_id', p_tpv.osm_id
)
)
)
from unnest(p_tpvarray) p_tpv
$$ language 'sql' immutable strict parallel safe;
drop cast if exists ( tpv [] as jsonb );
create cast ( tpv [] as jsonb )
with function _jsonb_from_tpvarray(tpv []);