pgLatLon

view latlon--0.14--0.15.sql @ 76:4f11ccf36fb6

Replaced huge SQL query in GeoJSON_to_ecluster function by several smaller SQL statements to enhance performance
author jbe
date Mon Nov 30 19:25:57 2020 +0100 (2020-11-30)
parents 1cdb3da50176
children
line source
1 SET LOCAL search_path TO @extschema@;
4 ------------------------------------------------------------
5 -- dummy input/output functions for dummy index key types --
6 ------------------------------------------------------------
8 CREATE OR REPLACE FUNCTION ekey_point_in_dummy(cstring)
9 RETURNS ekey_point
10 LANGUAGE C IMMUTABLE STRICT
11 AS '$libdir/latlon-v0010', 'pgl_notimpl';
13 CREATE OR REPLACE FUNCTION ekey_point_out_dummy(ekey_point)
14 RETURNS cstring
15 LANGUAGE C IMMUTABLE STRICT
16 AS '$libdir/latlon-v0010', 'pgl_notimpl';
18 CREATE OR REPLACE FUNCTION ekey_area_in_dummy(cstring)
19 RETURNS ekey_area
20 LANGUAGE C IMMUTABLE STRICT
21 AS '$libdir/latlon-v0010', 'pgl_notimpl';
23 CREATE OR REPLACE FUNCTION ekey_area_out_dummy(ekey_area)
24 RETURNS cstring
25 LANGUAGE C IMMUTABLE STRICT
26 AS '$libdir/latlon-v0010', 'pgl_notimpl';
29 --------------------------
30 -- text input functions --
31 --------------------------
33 CREATE OR REPLACE FUNCTION epoint_in(cstring)
34 RETURNS epoint
35 LANGUAGE C IMMUTABLE STRICT
36 AS '$libdir/latlon-v0010', 'pgl_epoint_in';
38 CREATE OR REPLACE FUNCTION epoint_with_sample_count_in(cstring)
39 RETURNS epoint_with_sample_count
40 LANGUAGE C IMMUTABLE STRICT
41 AS '$libdir/latlon-v0010', 'pgl_epoint_with_sample_count_in';
43 CREATE OR REPLACE FUNCTION ebox_in(cstring)
44 RETURNS ebox
45 LANGUAGE C IMMUTABLE STRICT
46 AS '$libdir/latlon-v0010', 'pgl_ebox_in';
48 CREATE OR REPLACE FUNCTION ecircle_in(cstring)
49 RETURNS ecircle
50 LANGUAGE C IMMUTABLE STRICT
51 AS '$libdir/latlon-v0010', 'pgl_ecircle_in';
53 CREATE OR REPLACE FUNCTION ecluster_in(cstring)
54 RETURNS ecluster
55 LANGUAGE C IMMUTABLE STRICT
56 AS '$libdir/latlon-v0010', 'pgl_ecluster_in';
59 ---------------------------
60 -- text output functions --
61 ---------------------------
63 CREATE OR REPLACE FUNCTION epoint_out(epoint)
64 RETURNS cstring
65 LANGUAGE C IMMUTABLE STRICT
66 AS '$libdir/latlon-v0010', 'pgl_epoint_out';
68 CREATE OR REPLACE FUNCTION epoint_with_sample_count_out(epoint_with_sample_count)
69 RETURNS cstring
70 LANGUAGE C IMMUTABLE STRICT
71 AS '$libdir/latlon-v0010', 'pgl_epoint_with_sample_count_out';
73 CREATE OR REPLACE FUNCTION ebox_out(ebox)
74 RETURNS cstring
75 LANGUAGE C IMMUTABLE STRICT
76 AS '$libdir/latlon-v0010', 'pgl_ebox_out';
78 CREATE OR REPLACE FUNCTION ecircle_out(ecircle)
79 RETURNS cstring
80 LANGUAGE C IMMUTABLE STRICT
81 AS '$libdir/latlon-v0010', 'pgl_ecircle_out';
83 CREATE OR REPLACE FUNCTION ecluster_out(ecluster)
84 RETURNS cstring
85 LANGUAGE C IMMUTABLE STRICT
86 AS '$libdir/latlon-v0010', 'pgl_ecluster_out';
89 --------------------------
90 -- binary I/O functions --
91 --------------------------
93 CREATE OR REPLACE FUNCTION epoint_recv(internal)
94 RETURNS epoint
95 LANGUAGE C IMMUTABLE STRICT
96 AS '$libdir/latlon-v0010', 'pgl_epoint_recv';
98 CREATE OR REPLACE FUNCTION ebox_recv(internal)
99 RETURNS ebox
100 LANGUAGE C IMMUTABLE STRICT
101 AS '$libdir/latlon-v0010', 'pgl_ebox_recv';
103 CREATE OR REPLACE FUNCTION ecircle_recv(internal)
104 RETURNS ecircle
105 LANGUAGE C IMMUTABLE STRICT
106 AS '$libdir/latlon-v0010', 'pgl_ecircle_recv';
108 CREATE OR REPLACE FUNCTION epoint_send(epoint)
109 RETURNS bytea
110 LANGUAGE C IMMUTABLE STRICT
111 AS '$libdir/latlon-v0010', 'pgl_epoint_send';
113 CREATE OR REPLACE FUNCTION ebox_send(ebox)
114 RETURNS bytea
115 LANGUAGE C IMMUTABLE STRICT
116 AS '$libdir/latlon-v0010', 'pgl_ebox_send';
118 CREATE OR REPLACE FUNCTION ecircle_send(ecircle)
119 RETURNS bytea
120 LANGUAGE C IMMUTABLE STRICT
121 AS '$libdir/latlon-v0010', 'pgl_ecircle_send';
124 --------------------
125 -- B-tree support --
126 --------------------
128 -- begin of B-tree support for epoint
130 CREATE OR REPLACE FUNCTION epoint_btree_lt(epoint, epoint)
131 RETURNS boolean
132 LANGUAGE C IMMUTABLE STRICT
133 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_lt';
135 CREATE OR REPLACE FUNCTION epoint_btree_le(epoint, epoint)
136 RETURNS boolean
137 LANGUAGE C IMMUTABLE STRICT
138 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_le';
140 CREATE OR REPLACE FUNCTION epoint_btree_eq(epoint, epoint)
141 RETURNS boolean
142 LANGUAGE C IMMUTABLE STRICT
143 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_eq';
145 CREATE OR REPLACE FUNCTION epoint_btree_ne(epoint, epoint)
146 RETURNS boolean
147 LANGUAGE C IMMUTABLE STRICT
148 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_ne';
150 CREATE OR REPLACE FUNCTION epoint_btree_ge(epoint, epoint)
151 RETURNS boolean
152 LANGUAGE C IMMUTABLE STRICT
153 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_ge';
155 CREATE OR REPLACE FUNCTION epoint_btree_gt(epoint, epoint)
156 RETURNS boolean
157 LANGUAGE C IMMUTABLE STRICT
158 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_gt';
160 CREATE OR REPLACE FUNCTION epoint_btree_cmp(epoint, epoint)
161 RETURNS int4
162 LANGUAGE C IMMUTABLE STRICT
163 AS '$libdir/latlon-v0010', 'pgl_btree_epoint_cmp';
165 -- end of B-tree support for epoint
167 -- begin of B-tree support for ebox
169 CREATE OR REPLACE FUNCTION ebox_btree_lt(ebox, ebox)
170 RETURNS boolean
171 LANGUAGE C IMMUTABLE STRICT
172 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_lt';
174 CREATE OR REPLACE FUNCTION ebox_btree_le(ebox, ebox)
175 RETURNS boolean
176 LANGUAGE C IMMUTABLE STRICT
177 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_le';
179 CREATE OR REPLACE FUNCTION ebox_btree_eq(ebox, ebox)
180 RETURNS boolean
181 LANGUAGE C IMMUTABLE STRICT
182 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_eq';
184 CREATE OR REPLACE FUNCTION ebox_btree_ne(ebox, ebox)
185 RETURNS boolean
186 LANGUAGE C IMMUTABLE STRICT
187 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_ne';
189 CREATE OR REPLACE FUNCTION ebox_btree_ge(ebox, ebox)
190 RETURNS boolean
191 LANGUAGE C IMMUTABLE STRICT
192 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_ge';
194 CREATE OR REPLACE FUNCTION ebox_btree_gt(ebox, ebox)
195 RETURNS boolean
196 LANGUAGE C IMMUTABLE STRICT
197 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_gt';
199 CREATE OR REPLACE FUNCTION ebox_btree_cmp(ebox, ebox)
200 RETURNS int4
201 LANGUAGE C IMMUTABLE STRICT
202 AS '$libdir/latlon-v0010', 'pgl_btree_ebox_cmp';
204 -- end of B-tree support for ebox
206 -- begin of B-tree support for ecircle
208 CREATE OR REPLACE FUNCTION ecircle_btree_lt(ecircle, ecircle)
209 RETURNS boolean
210 LANGUAGE C IMMUTABLE STRICT
211 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_lt';
213 CREATE OR REPLACE FUNCTION ecircle_btree_le(ecircle, ecircle)
214 RETURNS boolean
215 LANGUAGE C IMMUTABLE STRICT
216 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_le';
218 CREATE OR REPLACE FUNCTION ecircle_btree_eq(ecircle, ecircle)
219 RETURNS boolean
220 LANGUAGE C IMMUTABLE STRICT
221 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_eq';
223 CREATE OR REPLACE FUNCTION ecircle_btree_ne(ecircle, ecircle)
224 RETURNS boolean
225 LANGUAGE C IMMUTABLE STRICT
226 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_ne';
228 CREATE OR REPLACE FUNCTION ecircle_btree_ge(ecircle, ecircle)
229 RETURNS boolean
230 LANGUAGE C IMMUTABLE STRICT
231 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_ge';
233 CREATE OR REPLACE FUNCTION ecircle_btree_gt(ecircle, ecircle)
234 RETURNS boolean
235 LANGUAGE C IMMUTABLE STRICT
236 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_gt';
238 CREATE OR REPLACE FUNCTION ecircle_btree_cmp(ecircle, ecircle)
239 RETURNS int4
240 LANGUAGE C IMMUTABLE STRICT
241 AS '$libdir/latlon-v0010', 'pgl_btree_ecircle_cmp';
243 -- end of B-tree support for ecircle
246 ----------------
247 -- type casts --
248 ----------------
250 CREATE OR REPLACE FUNCTION cast_epoint_to_ebox(epoint)
251 RETURNS ebox
252 LANGUAGE C IMMUTABLE STRICT
253 AS '$libdir/latlon-v0010', 'pgl_epoint_to_ebox';
255 CREATE OR REPLACE FUNCTION cast_epoint_to_ecircle(epoint)
256 RETURNS ecircle
257 LANGUAGE C IMMUTABLE STRICT
258 AS '$libdir/latlon-v0010', 'pgl_epoint_to_ecircle';
260 CREATE OR REPLACE FUNCTION cast_epoint_to_ecluster(epoint)
261 RETURNS ecluster
262 LANGUAGE C IMMUTABLE STRICT
263 AS '$libdir/latlon-v0010', 'pgl_epoint_to_ecluster';
265 CREATE OR REPLACE FUNCTION cast_ebox_to_ecluster(ebox)
266 RETURNS ecluster
267 LANGUAGE C IMMUTABLE STRICT
268 AS '$libdir/latlon-v0010', 'pgl_ebox_to_ecluster';
271 ---------------------------
272 -- constructor functions --
273 ---------------------------
275 CREATE OR REPLACE FUNCTION epoint(float8, float8)
276 RETURNS epoint
277 LANGUAGE C IMMUTABLE STRICT
278 AS '$libdir/latlon-v0010', 'pgl_create_epoint';
280 CREATE OR REPLACE FUNCTION epoint_latlon(float8, float8)
281 RETURNS epoint
282 LANGUAGE SQL IMMUTABLE STRICT AS $$
283 SELECT @extschema@.epoint($1, $2)
284 $$;
286 CREATE OR REPLACE FUNCTION epoint_lonlat(float8, float8)
287 RETURNS epoint
288 LANGUAGE SQL IMMUTABLE STRICT AS $$
289 SELECT @extschema@.epoint($2, $1)
290 $$;
292 CREATE OR REPLACE FUNCTION epoint_with_sample_count(epoint, int4)
293 RETURNS epoint_with_sample_count
294 LANGUAGE C IMMUTABLE STRICT
295 AS '$libdir/latlon-v0010', 'pgl_create_epoint_with_sample_count';
297 CREATE OR REPLACE FUNCTION empty_ebox()
298 RETURNS ebox
299 LANGUAGE C IMMUTABLE STRICT
300 AS '$libdir/latlon-v0010', 'pgl_create_empty_ebox';
302 CREATE OR REPLACE FUNCTION ebox(float8, float8, float8, float8)
303 RETURNS ebox
304 LANGUAGE C IMMUTABLE STRICT
305 AS '$libdir/latlon-v0010', 'pgl_create_ebox';
307 CREATE OR REPLACE FUNCTION ebox(epoint, epoint)
308 RETURNS ebox
309 LANGUAGE C IMMUTABLE STRICT
310 AS '$libdir/latlon-v0010', 'pgl_create_ebox_from_epoints';
312 CREATE OR REPLACE FUNCTION ecircle(float8, float8, float8)
313 RETURNS ecircle
314 LANGUAGE C IMMUTABLE STRICT
315 AS '$libdir/latlon-v0010', 'pgl_create_ecircle';
317 CREATE OR REPLACE FUNCTION ecircle(epoint, float8)
318 RETURNS ecircle
319 LANGUAGE C IMMUTABLE STRICT
320 AS '$libdir/latlon-v0010', 'pgl_create_ecircle_from_epoint';
322 CREATE OR REPLACE FUNCTION ecluster_concat(ecluster[])
323 RETURNS ecluster
324 LANGUAGE sql IMMUTABLE STRICT AS $$
325 SELECT pg_catalog.array_to_string($1, ' ')::@extschema@.ecluster
326 $$;
328 CREATE OR REPLACE FUNCTION ecluster_concat(ecluster, ecluster)
329 RETURNS ecluster
330 LANGUAGE sql IMMUTABLE STRICT AS $$
331 SELECT (
332 $1::pg_catalog.text OPERATOR(pg_catalog.||) ' ' OPERATOR(pg_catalog.||)
333 $2::pg_catalog.text
334 )::@extschema@.ecluster
335 $$;
337 CREATE OR REPLACE FUNCTION ecluster_create_multipoint(epoint[])
338 RETURNS ecluster
339 LANGUAGE sql IMMUTABLE STRICT AS $$
340 SELECT
341 pg_catalog.array_to_string(
342 pg_catalog.array_agg(
343 'point (' OPERATOR(pg_catalog.||) unnest OPERATOR(pg_catalog.||) ')'
344 ),
345 ' '
346 )::@extschema@.ecluster
347 FROM pg_catalog.unnest($1)
348 $$;
350 CREATE OR REPLACE FUNCTION ecluster_create_path(epoint[])
351 RETURNS ecluster
352 LANGUAGE sql IMMUTABLE STRICT AS $$
353 SELECT CASE WHEN "str" OPERATOR(pg_catalog.=) '' THEN
354 'empty'::@extschema@.ecluster
355 ELSE
356 (
357 'path (' OPERATOR(pg_catalog.||) "str" OPERATOR(pg_catalog.||) ')'
358 )::@extschema@.ecluster
359 END
360 FROM pg_catalog.array_to_string($1, ' ') AS "str"
361 $$;
363 CREATE OR REPLACE FUNCTION ecluster_create_outline(epoint[])
364 RETURNS ecluster
365 LANGUAGE sql IMMUTABLE STRICT AS $$
366 SELECT CASE WHEN "str" OPERATOR(pg_catalog.=) '' THEN
367 'empty'::@extschema@.ecluster
368 ELSE
369 (
370 'outline (' OPERATOR(pg_catalog.||) "str" OPERATOR(pg_catalog.||) ')'
371 )::@extschema@.ecluster
372 END
373 FROM pg_catalog.array_to_string($1, ' ') AS "str"
374 $$;
376 CREATE OR REPLACE FUNCTION ecluster_create_polygon(epoint[])
377 RETURNS ecluster
378 LANGUAGE sql IMMUTABLE STRICT AS $$
379 SELECT CASE WHEN "str" OPERATOR(pg_catalog.=) '' THEN
380 'empty'::@extschema@.ecluster
381 ELSE
382 (
383 'polygon (' OPERATOR(pg_catalog.||) pg_catalog.array_to_string($1, ' ')
384 OPERATOR(pg_catalog.||) ')'
385 )::@extschema@.ecluster
386 END
387 FROM pg_catalog.array_to_string($1, ' ') AS "str"
388 $$;
391 ----------------------
392 -- getter functions --
393 ----------------------
395 CREATE OR REPLACE FUNCTION latitude(epoint)
396 RETURNS float8
397 LANGUAGE C IMMUTABLE STRICT
398 AS '$libdir/latlon-v0010', 'pgl_epoint_lat';
400 CREATE OR REPLACE FUNCTION longitude(epoint)
401 RETURNS float8
402 LANGUAGE C IMMUTABLE STRICT
403 AS '$libdir/latlon-v0010', 'pgl_epoint_lon';
405 CREATE OR REPLACE FUNCTION min_latitude(ebox)
406 RETURNS float8
407 LANGUAGE C IMMUTABLE STRICT
408 AS '$libdir/latlon-v0010', 'pgl_ebox_lat_min';
410 CREATE OR REPLACE FUNCTION max_latitude(ebox)
411 RETURNS float8
412 LANGUAGE C IMMUTABLE STRICT
413 AS '$libdir/latlon-v0010', 'pgl_ebox_lat_max';
415 CREATE OR REPLACE FUNCTION min_longitude(ebox)
416 RETURNS float8
417 LANGUAGE C IMMUTABLE STRICT
418 AS '$libdir/latlon-v0010', 'pgl_ebox_lon_min';
420 CREATE OR REPLACE FUNCTION max_longitude(ebox)
421 RETURNS float8
422 LANGUAGE C IMMUTABLE STRICT
423 AS '$libdir/latlon-v0010', 'pgl_ebox_lon_max';
425 CREATE OR REPLACE FUNCTION center(ecircle)
426 RETURNS epoint
427 LANGUAGE C IMMUTABLE STRICT
428 AS '$libdir/latlon-v0010', 'pgl_ecircle_center';
430 CREATE OR REPLACE FUNCTION radius(ecircle)
431 RETURNS float8
432 LANGUAGE C IMMUTABLE STRICT
433 AS '$libdir/latlon-v0010', 'pgl_ecircle_radius';
435 CREATE OR REPLACE FUNCTION ecluster_extract_points(ecluster)
436 RETURNS SETOF epoint
437 LANGUAGE sql IMMUTABLE STRICT AS $$
438 SELECT "match"[2]::@extschema@.epoint
439 FROM pg_catalog.regexp_matches(
440 $1::pg_catalog.text, e'(^| )point \\(([^)]+)\\)', 'g'
441 ) AS "match"
442 $$;
444 CREATE OR REPLACE FUNCTION ecluster_extract_paths(ecluster)
445 RETURNS SETOF epoint[]
446 LANGUAGE sql IMMUTABLE STRICT AS $$
447 SELECT (
448 SELECT pg_catalog.array_agg("m2"[1]::@extschema@.epoint)
449 FROM pg_catalog.regexp_matches("m1"[2], e'[^ ]+ [^ ]+', 'g') AS "m2"
450 )
451 FROM pg_catalog.regexp_matches(
452 $1::pg_catalog.text, e'(^| )path \\(([^)]+)\\)', 'g'
453 ) AS "m1"
454 $$;
456 CREATE OR REPLACE FUNCTION ecluster_extract_outlines(ecluster)
457 RETURNS SETOF epoint[]
458 LANGUAGE sql IMMUTABLE STRICT AS $$
459 SELECT (
460 SELECT pg_catalog.array_agg("m2"[1]::@extschema@.epoint)
461 FROM pg_catalog.regexp_matches("m1"[2], e'[^ ]+ [^ ]+', 'g') AS "m2"
462 )
463 FROM pg_catalog.regexp_matches(
464 $1::pg_catalog.text, e'(^| )outline \\(([^)]+)\\)', 'g'
465 ) AS "m1"
466 $$;
468 CREATE OR REPLACE FUNCTION ecluster_extract_polygons(ecluster)
469 RETURNS SETOF epoint[]
470 LANGUAGE sql IMMUTABLE STRICT AS $$
471 SELECT (
472 SELECT pg_catalog.array_agg("m2"[1]::@extschema@.epoint)
473 FROM pg_catalog.regexp_matches("m1"[2], e'[^ ]+ [^ ]+', 'g') AS "m2"
474 )
475 FROM pg_catalog.regexp_matches(
476 $1::pg_catalog.text, e'(^| )polygon \\(([^)]+)\\)', 'g'
477 ) AS "m1"
478 $$;
481 ---------------
482 -- operators --
483 ---------------
485 CREATE OR REPLACE FUNCTION epoint_ebox_overlap_proc(epoint, ebox)
486 RETURNS boolean
487 LANGUAGE C IMMUTABLE STRICT
488 AS '$libdir/latlon-v0010', 'pgl_epoint_ebox_overlap';
490 CREATE OR REPLACE FUNCTION epoint_ecircle_overlap_proc(epoint, ecircle)
491 RETURNS boolean
492 LANGUAGE C IMMUTABLE STRICT
493 AS '$libdir/latlon-v0010', 'pgl_epoint_ecircle_overlap';
495 CREATE OR REPLACE FUNCTION epoint_ecluster_overlap_proc(epoint, ecluster)
496 RETURNS boolean
497 LANGUAGE C IMMUTABLE STRICT
498 AS '$libdir/latlon-v0010', 'pgl_epoint_ecluster_overlap';
500 CREATE OR REPLACE FUNCTION epoint_ecluster_may_overlap_proc(epoint, ecluster)
501 RETURNS boolean
502 LANGUAGE C IMMUTABLE STRICT
503 AS '$libdir/latlon-v0010', 'pgl_epoint_ecluster_may_overlap';
505 CREATE OR REPLACE FUNCTION ebox_overlap_proc(ebox, ebox)
506 RETURNS boolean
507 LANGUAGE C IMMUTABLE STRICT
508 AS '$libdir/latlon-v0010', 'pgl_ebox_overlap';
510 CREATE OR REPLACE FUNCTION ebox_ecircle_may_overlap_proc(ebox, ecircle)
511 RETURNS boolean
512 LANGUAGE C IMMUTABLE STRICT
513 AS '$libdir/latlon-v0010', 'pgl_ebox_ecircle_may_overlap';
515 CREATE OR REPLACE FUNCTION ebox_ecluster_may_overlap_proc(ebox, ecluster)
516 RETURNS boolean
517 LANGUAGE C IMMUTABLE STRICT
518 AS '$libdir/latlon-v0010', 'pgl_ebox_ecluster_may_overlap';
520 CREATE OR REPLACE FUNCTION ecircle_overlap_proc(ecircle, ecircle)
521 RETURNS boolean
522 LANGUAGE C IMMUTABLE STRICT
523 AS '$libdir/latlon-v0010', 'pgl_ecircle_overlap';
525 CREATE OR REPLACE FUNCTION ecircle_ecluster_overlap_proc(ecircle, ecluster)
526 RETURNS boolean
527 LANGUAGE C IMMUTABLE STRICT
528 AS '$libdir/latlon-v0010', 'pgl_ecircle_ecluster_overlap';
530 CREATE OR REPLACE FUNCTION ecircle_ecluster_may_overlap_proc(ecircle, ecluster)
531 RETURNS boolean
532 LANGUAGE C IMMUTABLE STRICT
533 AS '$libdir/latlon-v0010', 'pgl_ecircle_ecluster_may_overlap';
535 CREATE OR REPLACE FUNCTION ecluster_overlap_proc(ecluster, ecluster)
536 RETURNS boolean
537 LANGUAGE C IMMUTABLE STRICT
538 AS '$libdir/latlon-v0010', 'pgl_ecluster_overlap';
540 CREATE OR REPLACE FUNCTION ecluster_may_overlap_proc(ecluster, ecluster)
541 RETURNS boolean
542 LANGUAGE C IMMUTABLE STRICT
543 AS '$libdir/latlon-v0010', 'pgl_ecluster_may_overlap';
545 CREATE OR REPLACE FUNCTION ecluster_contains_proc(ecluster, ecluster)
546 RETURNS boolean
547 LANGUAGE C IMMUTABLE STRICT
548 AS '$libdir/latlon-v0010', 'pgl_ecluster_contains';
550 CREATE OR REPLACE FUNCTION epoint_distance_proc(epoint, epoint)
551 RETURNS float8
552 LANGUAGE C IMMUTABLE STRICT
553 AS '$libdir/latlon-v0010', 'pgl_epoint_distance';
555 CREATE OR REPLACE FUNCTION epoint_ecircle_distance_proc(epoint, ecircle)
556 RETURNS float8
557 LANGUAGE C IMMUTABLE STRICT
558 AS '$libdir/latlon-v0010', 'pgl_epoint_ecircle_distance';
560 CREATE OR REPLACE FUNCTION epoint_ecluster_distance_proc(epoint, ecluster)
561 RETURNS float8
562 LANGUAGE C IMMUTABLE STRICT
563 AS '$libdir/latlon-v0010', 'pgl_epoint_ecluster_distance';
565 CREATE OR REPLACE FUNCTION ecircle_distance_proc(ecircle, ecircle)
566 RETURNS float8
567 LANGUAGE C IMMUTABLE STRICT
568 AS '$libdir/latlon-v0010', 'pgl_ecircle_distance';
570 CREATE OR REPLACE FUNCTION ecircle_ecluster_distance_proc(ecircle, ecluster)
571 RETURNS float8
572 LANGUAGE C IMMUTABLE STRICT
573 AS '$libdir/latlon-v0010', 'pgl_ecircle_ecluster_distance';
575 CREATE OR REPLACE FUNCTION ecluster_distance_proc(ecluster, ecluster)
576 RETURNS float8
577 LANGUAGE C IMMUTABLE STRICT
578 AS '$libdir/latlon-v0010', 'pgl_ecluster_distance';
580 CREATE OR REPLACE FUNCTION fair_distance_operator_proc(ecluster, epoint_with_sample_count)
581 RETURNS float8
582 LANGUAGE C IMMUTABLE STRICT
583 AS '$libdir/latlon-v0010', 'pgl_ecluster_epoint_sc_fair_distance';
585 CREATE OR REPLACE FUNCTION epoint_ebox_overlap_commutator(ebox, epoint)
586 RETURNS boolean
587 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
589 CREATE OR REPLACE FUNCTION epoint_ecircle_overlap_commutator(ecircle, epoint)
590 RETURNS boolean
591 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
593 CREATE OR REPLACE FUNCTION epoint_ecluster_overlap_commutator(ecluster, epoint)
594 RETURNS boolean
595 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
597 CREATE OR REPLACE FUNCTION ecircle_ecluster_overlap_commutator(ecluster, ecircle)
598 RETURNS boolean
599 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&) $1';
601 CREATE OR REPLACE FUNCTION ebox_ecircle_overlap_castwrap(ebox, ecircle)
602 RETURNS boolean
603 LANGUAGE sql IMMUTABLE
604 AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.&&) $2';
606 CREATE OR REPLACE FUNCTION ebox_ecircle_overlap_castwrap(ecircle, ebox)
607 RETURNS boolean
608 LANGUAGE sql IMMUTABLE
609 AS 'SELECT $1 OPERATOR(@extschema@.&&) $2::@extschema@.ecluster';
611 CREATE OR REPLACE FUNCTION ebox_ecluster_overlap_castwrap(ebox, ecluster)
612 RETURNS boolean
613 LANGUAGE sql IMMUTABLE
614 AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.&&) $2';
616 CREATE OR REPLACE FUNCTION ebox_ecluster_overlap_castwrap(ecluster, ebox)
617 RETURNS boolean
618 LANGUAGE sql IMMUTABLE
619 AS 'SELECT $1 OPERATOR(@extschema@.&&) $2::@extschema@.ecluster';
621 CREATE OR REPLACE FUNCTION epoint_ecluster_may_overlap_commutator(ecluster, epoint)
622 RETURNS boolean
623 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
625 CREATE OR REPLACE FUNCTION ebox_ecircle_may_overlap_commutator(ecircle, ebox)
626 RETURNS boolean
627 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
629 CREATE OR REPLACE FUNCTION ebox_ecluster_may_overlap_commutator(ecluster, ebox)
630 RETURNS boolean
631 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
633 CREATE OR REPLACE FUNCTION ecircle_ecluster_may_overlap_commutator(ecluster, ecircle)
634 RETURNS boolean
635 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.&&+) $1';
637 CREATE OR REPLACE FUNCTION ecluster_contains_commutator(ecluster, ecluster)
638 RETURNS boolean
639 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.@>) $1';
641 CREATE OR REPLACE FUNCTION ebox_contains_castwrap(ebox, ebox)
642 RETURNS boolean
643 LANGUAGE sql IMMUTABLE AS $$
644 SELECT
645 $1::@extschema@.ecluster OPERATOR(@extschema@.@>) $2::@extschema@.ecluster
646 $$;
648 CREATE OR REPLACE FUNCTION ebox_contains_swapped_castwrap(ebox, ebox)
649 RETURNS boolean
650 LANGUAGE sql IMMUTABLE AS $$
651 SELECT
652 $2::@extschema@.ecluster OPERATOR(@extschema@.@>) $1::@extschema@.ecluster
653 $$;
655 CREATE OR REPLACE FUNCTION ebox_ecluster_contains_castwrap(ebox, ecluster)
656 RETURNS boolean
657 LANGUAGE sql IMMUTABLE
658 AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.@>) $2';
660 CREATE OR REPLACE FUNCTION ebox_ecluster_contains_castwrap(ecluster, ebox)
661 RETURNS boolean
662 LANGUAGE sql IMMUTABLE
663 AS 'SELECT $2::@extschema@.ecluster OPERATOR(@extschema@.@>) $1';
665 CREATE OR REPLACE FUNCTION ecluster_ebox_contains_castwrap(ecluster, ebox)
666 RETURNS boolean
667 LANGUAGE sql IMMUTABLE
668 AS 'SELECT $1 OPERATOR(@extschema@.@>) $2::@extschema@.ecluster';
670 CREATE OR REPLACE FUNCTION ecluster_ebox_contains_castwrap(ebox, ecluster)
671 RETURNS boolean
672 LANGUAGE sql IMMUTABLE
673 AS 'SELECT $2 OPERATOR(@extschema@.@>) $1::@extschema@.ecluster';
675 CREATE OR REPLACE FUNCTION epoint_ecircle_distance_commutator(ecircle, epoint)
676 RETURNS float8
677 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.<->) $1';
679 CREATE OR REPLACE FUNCTION epoint_ecluster_distance_commutator(ecluster, epoint)
680 RETURNS float8
681 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.<->) $1';
683 CREATE OR REPLACE FUNCTION ecircle_ecluster_distance_commutator(ecluster, ecircle)
684 RETURNS float8
685 LANGUAGE sql IMMUTABLE AS 'SELECT $2 OPERATOR(@extschema@.<->) $1';
687 CREATE OR REPLACE FUNCTION epoint_ebox_distance_castwrap(epoint, ebox)
688 RETURNS float8
689 LANGUAGE sql IMMUTABLE
690 AS 'SELECT $1 OPERATOR(@extschema@.<->) $2::@extschema@.ecluster';
692 CREATE OR REPLACE FUNCTION epoint_ebox_distance_castwrap(ebox, epoint)
693 RETURNS float8
694 LANGUAGE sql IMMUTABLE
695 AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2';
697 CREATE OR REPLACE FUNCTION ebox_distance_castwrap(ebox, ebox)
698 RETURNS float8
699 LANGUAGE sql IMMUTABLE AS $$
700 SELECT
701 $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2::@extschema@.ecluster
702 $$;
704 CREATE OR REPLACE FUNCTION ebox_ecircle_distance_castwrap(ebox, ecircle)
705 RETURNS float8
706 LANGUAGE sql IMMUTABLE
707 AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2';
709 CREATE OR REPLACE FUNCTION ebox_ecircle_distance_castwrap(ecircle, ebox)
710 RETURNS float8
711 LANGUAGE sql IMMUTABLE
712 AS 'SELECT $1 OPERATOR(@extschema@.<->) $2::@extschema@.ecluster';
714 CREATE OR REPLACE FUNCTION ebox_ecluster_distance_castwrap(ebox, ecluster)
715 RETURNS float8
716 LANGUAGE sql IMMUTABLE
717 AS 'SELECT $1::@extschema@.ecluster OPERATOR(@extschema@.<->) $2';
719 CREATE OR REPLACE FUNCTION ebox_ecluster_distance_castwrap(ecluster, ebox)
720 RETURNS float8
721 LANGUAGE sql IMMUTABLE
722 AS 'SELECT $1 OPERATOR(@extschema@.<->) $2::@extschema@.ecluster';
725 ----------------
726 -- GiST index --
727 ----------------
729 CREATE OR REPLACE FUNCTION pgl_gist_consistent(internal, internal, smallint, oid, internal)
730 RETURNS boolean
731 LANGUAGE C STRICT
732 AS '$libdir/latlon-v0010', 'pgl_gist_consistent';
734 CREATE OR REPLACE FUNCTION pgl_gist_union(internal, internal)
735 RETURNS internal
736 LANGUAGE C STRICT
737 AS '$libdir/latlon-v0010', 'pgl_gist_union';
739 CREATE OR REPLACE FUNCTION pgl_gist_compress_epoint(internal)
740 RETURNS internal
741 LANGUAGE C STRICT
742 AS '$libdir/latlon-v0010', 'pgl_gist_compress_epoint';
744 CREATE OR REPLACE FUNCTION pgl_gist_compress_ecircle(internal)
745 RETURNS internal
746 LANGUAGE C STRICT
747 AS '$libdir/latlon-v0010', 'pgl_gist_compress_ecircle';
749 CREATE OR REPLACE FUNCTION pgl_gist_compress_ecluster(internal)
750 RETURNS internal
751 LANGUAGE C STRICT
752 AS '$libdir/latlon-v0010', 'pgl_gist_compress_ecluster';
754 CREATE OR REPLACE FUNCTION pgl_gist_decompress(internal)
755 RETURNS internal
756 LANGUAGE C STRICT
757 AS '$libdir/latlon-v0010', 'pgl_gist_decompress';
759 CREATE OR REPLACE FUNCTION pgl_gist_penalty(internal, internal, internal)
760 RETURNS internal
761 LANGUAGE C STRICT
762 AS '$libdir/latlon-v0010', 'pgl_gist_penalty';
764 CREATE OR REPLACE FUNCTION pgl_gist_picksplit(internal, internal)
765 RETURNS internal
766 LANGUAGE C STRICT
767 AS '$libdir/latlon-v0010', 'pgl_gist_picksplit';
769 CREATE OR REPLACE FUNCTION pgl_gist_same(internal, internal, internal)
770 RETURNS internal
771 LANGUAGE C STRICT
772 AS '$libdir/latlon-v0010', 'pgl_gist_same';
774 CREATE OR REPLACE FUNCTION pgl_gist_distance(internal, internal, smallint, oid)
775 RETURNS internal
776 LANGUAGE C STRICT
777 AS '$libdir/latlon-v0010', 'pgl_gist_distance';
780 ---------------------
781 -- alias functions --
782 ---------------------
784 CREATE OR REPLACE FUNCTION distance(epoint, epoint)
785 RETURNS float8
786 LANGUAGE sql IMMUTABLE AS 'SELECT $1 OPERATOR(@extschema@.<->) $2';
788 CREATE OR REPLACE FUNCTION distance(ecluster, epoint)
789 RETURNS float8
790 LANGUAGE sql IMMUTABLE AS 'SELECT $1 OPERATOR(@extschema@.<->) $2';
792 CREATE OR REPLACE FUNCTION distance_within(epoint, epoint, float8)
793 RETURNS boolean
794 LANGUAGE sql IMMUTABLE
795 AS 'SELECT $1 OPERATOR(@extschema@.&&) @extschema@.ecircle($2, $3)';
797 CREATE OR REPLACE FUNCTION distance_within(ecluster, epoint, float8)
798 RETURNS boolean
799 LANGUAGE sql IMMUTABLE
800 AS 'SELECT $1 OPERATOR(@extschema@.&&) @extschema@.ecircle($2, $3)';
802 CREATE OR REPLACE FUNCTION fair_distance(ecluster, epoint, int4 = 10000)
803 RETURNS float8
804 LANGUAGE sql IMMUTABLE AS $$
805 SELECT
806 $1 OPERATOR(@extschema@.<=>) @extschema@.epoint_with_sample_count($2, $3)
807 $$;
810 --------------------------------
811 -- other data storage formats --
812 --------------------------------
814 CREATE OR REPLACE FUNCTION coords_to_epoint(float8, float8, text = 'epoint')
815 RETURNS epoint
816 LANGUAGE plpgsql IMMUTABLE STRICT AS $$
817 DECLARE
818 "result" @extschema@.epoint;
819 BEGIN
820 IF $3 OPERATOR(pg_catalog.=) 'epoint_lonlat' THEN
821 -- avoid dynamic command execution for better performance
822 RETURN @extschema@.epoint($2, $1);
823 END IF;
824 IF
825 $3 OPERATOR(pg_catalog.=) 'epoint' OR
826 $3 OPERATOR(pg_catalog.=) 'epoint_latlon'
827 THEN
828 -- avoid dynamic command execution for better performance
829 RETURN @extschema@.epoint($1, $2);
830 END IF;
831 EXECUTE
832 'SELECT ' OPERATOR(pg_catalog.||) $3 OPERATOR(pg_catalog.||) '($1, $2)' INTO STRICT "result" USING $1, $2;
833 RETURN "result";
834 END;
835 $$;
837 CREATE OR REPLACE FUNCTION GeoJSON_LinearRing_vertices(jsonb, text = 'epoint_lonlat')
838 RETURNS SETOF jsonb
839 LANGUAGE sql IMMUTABLE STRICT AS $$
840 SELECT "result" FROM
841 ( SELECT pg_catalog.jsonb_array_length($1) - 1 )
842 AS "lastindex_row" ("lastindex")
843 CROSS JOIN LATERAL pg_catalog.jsonb_array_elements(
844 CASE WHEN
845 @extschema@.coords_to_epoint(
846 ($1 OPERATOR(pg_catalog.->) 0 OPERATOR(pg_catalog.->>) 0)
847 ::pg_catalog.float8,
848 ($1 OPERATOR(pg_catalog.->) 0 OPERATOR(pg_catalog.->>) 1)
849 ::pg_catalog.float8,
850 $2
851 ) OPERATOR(pg_catalog.=) @extschema@.coords_to_epoint(
852 ($1 OPERATOR(pg_catalog.->) "lastindex" OPERATOR(pg_catalog.->>) 0)
853 ::pg_catalog.float8,
854 ($1 OPERATOR(pg_catalog.->) "lastindex" OPERATOR(pg_catalog.->>) 1)
855 ::pg_catalog.float8,
856 $2
857 )
858 THEN
859 $1 - "lastindex"
860 ELSE
861 $1
862 END
863 ) AS "result_row" ("result")
864 $$;
866 CREATE OR REPLACE FUNCTION GeoJSON_to_epoint(jsonb, text = 'epoint_lonlat')
867 RETURNS epoint
868 LANGUAGE sql IMMUTABLE STRICT AS $$
869 SELECT CASE
870 WHEN $1 OPERATOR(pg_catalog.->>) 'type' OPERATOR(pg_catalog.=) 'Point' THEN
871 @extschema@.coords_to_epoint(
872 ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 0)
873 ::pg_catalog.float8,
874 ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 1)
875 ::pg_catalog.float8,
876 $2
877 )
878 WHEN $1->>'type' = 'Feature' THEN
879 @extschema@.GeoJSON_to_epoint($1 OPERATOR(pg_catalog.->) 'geometry', $2)
880 ELSE
881 NULL
882 END
883 $$;
885 CREATE OR REPLACE FUNCTION GeoJSON_to_ecluster(jsonb, text = 'epoint_lonlat')
886 RETURNS ecluster
887 LANGUAGE plpgsql IMMUTABLE STRICT AS $$
888 DECLARE
889 "tp" TEXT = $1 OPERATOR(pg_catalog.->>) 'type';
890 BEGIN
891 IF "tp" = 'Point' THEN RETURN
892 @extschema@.coords_to_epoint(
893 ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 0)
894 ::pg_catalog.float8,
895 ($1 OPERATOR(pg_catalog.->) 'coordinates' OPERATOR(pg_catalog.->>) 1)
896 ::pg_catalog.float8,
897 $2
898 )::@extschema@.ecluster;
899 END IF;
900 raise notice 'DEBUG2';
901 IF "tp" = 'MultiPoint' THEN RETURN
902 ( SELECT @extschema@.ecluster_create_multipoint(pg_catalog.array_agg(
903 @extschema@.coords_to_epoint(
904 ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
905 ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
906 $2
907 )
908 ))
909 FROM pg_catalog.jsonb_array_elements(
910 $1 OPERATOR(pg_catalog.->) 'coordinates'
911 ) AS "coord"
912 );
913 END IF;
914 IF "tp" = 'LineString' THEN RETURN
915 ( SELECT @extschema@.ecluster_create_path(pg_catalog.array_agg(
916 @extschema@.coords_to_epoint(
917 ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
918 ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
919 $2
920 )
921 ))
922 FROM pg_catalog.jsonb_array_elements(
923 $1 OPERATOR(pg_catalog.->) 'coordinates'
924 ) AS "coord"
925 );
926 END IF;
927 IF "tp" = 'MultiLineString' THEN RETURN
928 ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
929 ( SELECT @extschema@.ecluster_create_path(pg_catalog.array_agg(
930 @extschema@.coords_to_epoint(
931 ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
932 ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
933 $2
934 )
935 ))
936 FROM pg_catalog.jsonb_array_elements("coord_array") AS "coord"
937 )
938 ))
939 FROM pg_catalog.jsonb_array_elements(
940 $1 OPERATOR(pg_catalog.->) 'coordinates'
941 ) AS "coord_array"
942 );
943 END IF;
944 IF "tp" = 'Polygon' THEN RETURN
945 ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
946 ( SELECT @extschema@.ecluster_create_polygon(pg_catalog.array_agg(
947 @extschema@.coords_to_epoint(
948 ("coord" OPERATOR(pg_catalog.->>) 0)::pg_catalog.float8,
949 ("coord" OPERATOR(pg_catalog.->>) 1)::pg_catalog.float8,
950 $2
951 )
952 ))
953 FROM @extschema@.GeoJSON_LinearRing_vertices("coord_array", $2)
954 AS "coord"
955 )
956 ))
957 FROM pg_catalog.jsonb_array_elements(
958 $1 OPERATOR(pg_catalog.->) 'coordinates'
959 ) AS "coord_array"
960 );
961 END IF;
962 IF "tp" = 'MultiPolygon' THEN RETURN
963 ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
964 ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
965 ( SELECT @extschema@.ecluster_create_polygon(
966 pg_catalog.array_agg(
967 @extschema@.coords_to_epoint(
968 ("coord" OPERATOR(pg_catalog.->>) 0)
969 ::pg_catalog.float8,
970 ("coord" OPERATOR(pg_catalog.->>) 1)
971 ::pg_catalog.float8,
972 $2
973 )
974 )
975 )
976 FROM @extschema@.GeoJSON_LinearRing_vertices(
977 "coord_array", $2
978 ) AS "coord"
979 )
980 ))
981 FROM pg_catalog.jsonb_array_elements("coord_array_array")
982 AS "coord_array"
983 )
984 ))
985 FROM jsonb_array_elements(
986 $1 OPERATOR(pg_catalog.->) 'coordinates'
987 ) AS "coord_array_array"
988 );
989 END IF;
990 IF "tp" = 'GeometryCollection' THEN RETURN
991 ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
992 @extschema@.GeoJSON_to_ecluster("geometry", $2)
993 ))
994 FROM pg_catalog.jsonb_array_elements(
995 $1 OPERATOR(pg_catalog.->) 'geometries'
996 ) AS "geometry"
997 );
998 END IF;
999 IF "tp" = 'Feature' THEN RETURN
1000 @extschema@.GeoJSON_to_ecluster(
1001 $1 OPERATOR(pg_catalog.->) 'geometry', $2
1002 );
1003 END IF;
1004 IF "tp" = 'FeatureCollection' THEN RETURN
1005 ( SELECT @extschema@.ecluster_concat(pg_catalog.array_agg(
1006 @extschema@.GeoJSON_to_ecluster("feature", $2)
1007 ))
1008 FROM pg_catalog.jsonb_array_elements(
1009 $1 OPERATOR(pg_catalog.->) 'features'
1010 ) AS "feature"
1011 );
1012 END IF;
1013 RETURN NULL;
1014 END;
1015 $$;

Impressum / About Us