/
gserialized_gist_nd.c
1855 lines (1582 loc) · 51 KB
/
gserialized_gist_nd.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
* Copyright 2017 Darafei Praliaskouski <me@komzpa.net>
*
**********************************************************************/
/*
** R-Tree Bibliography
**
** [1] A. Guttman. R-tree: a dynamic index structure for spatial searching.
** Proceedings of the ACM SIGMOD Conference, pp 47-57, June 1984.
** [2] C.-H. Ang and T. C. Tan. New linear node splitting algorithm for
** R-Trees. Advances in Spatial Databases - 5th International Symposium,
** 1997
** [3] N. Beckmann, H.-P. Kriegel, R. Schneider, B. Seeger. The R*tree: an
** efficient and robust access method for points and rectangles.
** Proceedings of the ACM SIGMOD Conference. June 1990.
*/
#include "postgres.h"
#include "access/gist.h" /* For GiST */
#include "access/itup.h"
#include "access/skey.h"
#include "../postgis_config.h"
#include "liblwgeom.h" /* For standard geometry types. */
#include "lwgeom_pg.h" /* For debugging macros. */
#include "gserialized_gist.h" /* For utility functions. */
#include "geography.h"
#include <assert.h>
/*
** When is a node split not so good? If more than 90% of the entries
** end up in one of the children.
*/
#define LIMIT_RATIO 0.1
/*
** For debugging
*/
#if POSTGIS_DEBUG_LEVEL > 0
static int geog_counter_leaf = 0;
static int geog_counter_internal = 0;
#endif
/*
** ND Index key type stub prototypes
*/
Datum gidx_out(PG_FUNCTION_ARGS);
Datum gidx_in(PG_FUNCTION_ARGS);
/*
** ND GiST prototypes
*/
Datum gserialized_gist_consistent(PG_FUNCTION_ARGS);
Datum gserialized_gist_compress(PG_FUNCTION_ARGS);
Datum gserialized_gist_decompress(PG_FUNCTION_ARGS);
Datum gserialized_gist_penalty(PG_FUNCTION_ARGS);
Datum gserialized_gist_picksplit(PG_FUNCTION_ARGS);
Datum gserialized_gist_union(PG_FUNCTION_ARGS);
Datum gserialized_gist_same(PG_FUNCTION_ARGS);
Datum gserialized_gist_distance(PG_FUNCTION_ARGS);
Datum gserialized_gist_geog_distance(PG_FUNCTION_ARGS);
/*
** ND Operator prototypes
*/
Datum gserialized_overlaps(PG_FUNCTION_ARGS);
Datum gserialized_gidx_geom_overlaps(PG_FUNCTION_ARGS);
Datum gserialized_gidx_gidx_overlaps(PG_FUNCTION_ARGS);
Datum gserialized_contains(PG_FUNCTION_ARGS);
Datum gserialized_gidx_geom_contains(PG_FUNCTION_ARGS);
Datum gserialized_gidx_gidx_contains(PG_FUNCTION_ARGS);
Datum gserialized_within(PG_FUNCTION_ARGS);
Datum gserialized_gidx_geom_within(PG_FUNCTION_ARGS);
Datum gserialized_gidx_gidx_within(PG_FUNCTION_ARGS);
Datum gserialized_same(PG_FUNCTION_ARGS);
Datum gserialized_gidx_geom_same(PG_FUNCTION_ARGS);
Datum gserialized_gidx_gidx_same(PG_FUNCTION_ARGS);
Datum gserialized_distance_nd(PG_FUNCTION_ARGS);
/*
** GIDX true/false test function type
*/
typedef bool (*gidx_predicate)(GIDX *a, GIDX *b);
/* Allocate a new copy of GIDX */
GIDX *
gidx_copy(GIDX *b)
{
GIDX *c = (GIDX *)palloc(VARSIZE(b));
POSTGIS_DEBUGF(5, "copied gidx (%p) to gidx (%p)", b, c);
memcpy((void *)c, (void *)b, VARSIZE(b));
return c;
}
/* Ensure all minimums are below maximums. */
void
gidx_validate(GIDX *b)
{
uint32_t i;
Assert(b);
POSTGIS_DEBUGF(5, "validating gidx (%s)", gidx_to_string(b));
for (i = 0; i < GIDX_NDIMS(b); i++)
{
if (GIDX_GET_MIN(b, i) > GIDX_GET_MAX(b, i))
{
float tmp;
tmp = GIDX_GET_MIN(b, i);
GIDX_SET_MIN(b, i, GIDX_GET_MAX(b, i));
GIDX_SET_MAX(b, i, tmp);
}
}
return;
}
/* An "unknown" GIDX is used to represent the bounds of an EMPTY
geometry or other-wise unindexable geometry (like one with NaN
or Inf bounds) */
inline bool
gidx_is_unknown(const GIDX *a)
{
size_t size = VARSIZE(a) - VARHDRSZ;
/* "unknown" gidx objects have a too-small size of one float */
if (size <= 0.0)
return true;
return false;
}
void
gidx_set_unknown(GIDX *a)
{
SET_VARSIZE(a, VARHDRSZ);
}
/* Enlarge b_union to contain b_new. */
void
gidx_merge(GIDX **b_union, GIDX *b_new)
{
int i, dims_union, dims_new;
Assert(b_union);
Assert(*b_union);
Assert(b_new);
/* Can't merge an unknown into any thing */
if (gidx_is_unknown(b_new))
return;
/* Merge of unknown and known is known */
if (gidx_is_unknown(*b_union))
{
*b_union = b_new;
return;
}
dims_union = GIDX_NDIMS(*b_union);
dims_new = GIDX_NDIMS(b_new);
POSTGIS_DEBUGF(4, "merging gidx (%s) into gidx (%s)", gidx_to_string(b_new), gidx_to_string(*b_union));
/* Shrink unshared dimensions */
if (dims_new < dims_union)
{
POSTGIS_DEBUGF(5, "reallocating b_union from %d dims to %d dims", dims_union, dims_new);
*b_union = (GIDX *)repalloc(*b_union, GIDX_SIZE(dims_new));
SET_VARSIZE(*b_union, VARSIZE(b_new));
dims_union = dims_new;
}
for (i = 0; i < dims_union; i++)
{
/* Adjust minimums */
GIDX_SET_MIN(*b_union, i, Min(GIDX_GET_MIN(*b_union, i), GIDX_GET_MIN(b_new, i)));
/* Adjust maximums */
GIDX_SET_MAX(*b_union, i, Max(GIDX_GET_MAX(*b_union, i), GIDX_GET_MAX(b_new, i)));
}
POSTGIS_DEBUGF(5, "merge complete (%s)", gidx_to_string(*b_union));
assert(gidx_contains(*b_union, b_new));
return;
}
/* Calculate the volume (in n-d units) of the GIDX */
static float
gidx_volume(GIDX *a)
{
float result;
uint32_t i;
if (!a || gidx_is_unknown(a))
return 0.0;
result = GIDX_GET_MAX(a, 0) - GIDX_GET_MIN(a, 0);
for (i = 1; i < GIDX_NDIMS(a); i++)
result *= (GIDX_GET_MAX(a, i) - GIDX_GET_MIN(a, i));
POSTGIS_DEBUGF(5, "calculated volume of %s as %.8g", gidx_to_string(a), result);
return result;
}
/* Calculate the edge of the GIDX */
static float
gidx_edge(GIDX *a)
{
float result;
uint32_t i;
if (!a || gidx_is_unknown(a))
return 0.0;
result = GIDX_GET_MAX(a, 0) - GIDX_GET_MIN(a, 0);
for (i = 1; i < GIDX_NDIMS(a); i++)
result += (GIDX_GET_MAX(a, i) - GIDX_GET_MIN(a, i));
POSTGIS_DEBUGF(5, "calculated edge of %s as %.8g", gidx_to_string(a), result);
return result;
}
/* Ensure the first argument has the higher dimensionality. */
static void
gidx_dimensionality_check(GIDX **a, GIDX **b)
{
if (GIDX_NDIMS(*a) < GIDX_NDIMS(*b))
{
GIDX *tmp = *b;
*b = *a;
*a = tmp;
}
}
/* Calculate the volume of the union of the boxes. Avoids creating an intermediate box. */
static float
gidx_union_volume(GIDX *a, GIDX *b)
{
float result;
int i;
int ndims_a, ndims_b;
POSTGIS_DEBUG(5, "entered function");
if (!a && !b)
{
elog(ERROR, "gidx_union_volume received two null arguments");
return 0.0;
}
if (!a || gidx_is_unknown(a))
return gidx_volume(b);
if (!b || gidx_is_unknown(b))
return gidx_volume(a);
if (gidx_is_unknown(a) && gidx_is_unknown(b))
return 0.0;
/* Ensure 'a' has the most dimensions. */
gidx_dimensionality_check(&a, &b);
ndims_a = GIDX_NDIMS(a);
ndims_b = GIDX_NDIMS(b);
/* Initialize with maximal length of first dimension. */
result = Max(GIDX_GET_MAX(a, 0), GIDX_GET_MAX(b, 0)) - Min(GIDX_GET_MIN(a, 0), GIDX_GET_MIN(b, 0));
/* Multiply by maximal length of remaining dimensions. */
for (i = 1; i < ndims_b; i++)
result *= (Max(GIDX_GET_MAX(a, i), GIDX_GET_MAX(b, i)) - Min(GIDX_GET_MIN(a, i), GIDX_GET_MIN(b, i)));
/* Add in dimensions of higher dimensional box. */
for (i = ndims_b; i < ndims_a; i++)
result *= (GIDX_GET_MAX(a, i) - GIDX_GET_MIN(a, i));
POSTGIS_DEBUGF(5, "volume( %s union %s ) = %.12g", gidx_to_string(a), gidx_to_string(b), result);
return result;
}
/* Calculate the edge of the union of the boxes. Avoids creating an intermediate box. */
static float
gidx_union_edge(GIDX *a, GIDX *b)
{
float result;
int i;
int ndims_a, ndims_b;
POSTGIS_DEBUG(5, "entered function");
if (!a && !b)
{
elog(ERROR, "gidx_union_edge received two null arguments");
return 0.0;
}
if (!a || gidx_is_unknown(a))
return gidx_volume(b);
if (!b || gidx_is_unknown(b))
return gidx_volume(a);
if (gidx_is_unknown(a) && gidx_is_unknown(b))
return 0.0;
/* Ensure 'a' has the most dimensions. */
gidx_dimensionality_check(&a, &b);
ndims_a = GIDX_NDIMS(a);
ndims_b = GIDX_NDIMS(b);
/* Initialize with maximal length of first dimension. */
result = Max(GIDX_GET_MAX(a, 0), GIDX_GET_MAX(b, 0)) - Min(GIDX_GET_MIN(a, 0), GIDX_GET_MIN(b, 0));
/* Add maximal length of remaining dimensions. */
for (i = 1; i < ndims_b; i++)
result += (Max(GIDX_GET_MAX(a, i), GIDX_GET_MAX(b, i)) - Min(GIDX_GET_MIN(a, i), GIDX_GET_MIN(b, i)));
/* Add in dimensions of higher dimensional box. */
for (i = ndims_b; i < ndims_a; i++)
result += (GIDX_GET_MAX(a, i) - GIDX_GET_MIN(a, i));
POSTGIS_DEBUGF(5, "edge( %s union %s ) = %.12g", gidx_to_string(a), gidx_to_string(b), result);
return result;
}
/* Calculate the volume of the intersection of the boxes. */
static float
gidx_inter_volume(GIDX *a, GIDX *b)
{
uint32_t i;
float result;
POSTGIS_DEBUG(5, "entered function");
if (!a || !b)
{
elog(ERROR, "gidx_inter_volume received a null argument");
return 0.0;
}
if (gidx_is_unknown(a) || gidx_is_unknown(b))
return 0.0;
/* Ensure 'a' has the most dimensions. */
gidx_dimensionality_check(&a, &b);
/* Initialize with minimal length of first dimension. */
result = Min(GIDX_GET_MAX(a, 0), GIDX_GET_MAX(b, 0)) - Max(GIDX_GET_MIN(a, 0), GIDX_GET_MIN(b, 0));
/* If they are disjoint (max < min) then return zero. */
if (result < 0.0)
return 0.0;
/* Continue for remaining dimensions. */
for (i = 1; i < GIDX_NDIMS(b); i++)
{
float width = Min(GIDX_GET_MAX(a, i), GIDX_GET_MAX(b, i)) - Max(GIDX_GET_MIN(a, i), GIDX_GET_MIN(b, i));
if (width < 0.0)
return 0.0;
/* Multiply by minimal length of remaining dimensions. */
result *= width;
}
POSTGIS_DEBUGF(5, "volume( %s intersection %s ) = %.12g", gidx_to_string(a), gidx_to_string(b), result);
return result;
}
/*
** Overlapping GIDX box test.
**
** Box(A) Overlaps Box(B) IFF for every dimension d:
** min(A,d) <= max(B,d) && max(A,d) => min(B,d)
**
** Any missing dimension is assumed by convention to
** overlap whatever finite range available on the
** other operand. See
** http://lists.osgeo.org/pipermail/postgis-devel/2015-February/024757.html
**
** Empty boxes never overlap.
*/
bool
gidx_overlaps(GIDX *a, GIDX *b)
{
int i, dims_a, dims_b;
POSTGIS_DEBUG(5, "entered function");
if (!a || !b)
return false;
if (gidx_is_unknown(a) || gidx_is_unknown(b))
return false;
dims_a = GIDX_NDIMS(a);
dims_b = GIDX_NDIMS(b);
/* For all shared dimensions min(a) > max(b) and min(b) > max(a)
Unshared dimensions do not matter */
for (i = 0; i < Min(dims_a, dims_b); i++)
{
/* If the missing dimension was not padded with -+FLT_MAX */
if (GIDX_GET_MAX(a, i) != FLT_MAX && GIDX_GET_MAX(b, i) != FLT_MAX)
{
if (GIDX_GET_MIN(a, i) > GIDX_GET_MAX(b, i))
return false;
if (GIDX_GET_MIN(b, i) > GIDX_GET_MAX(a, i))
return false;
}
}
return true;
}
/*
** Containment GIDX test.
**
** Box(A) CONTAINS Box(B) IFF (pt(A)LL < pt(B)LL) && (pt(A)UR > pt(B)UR)
*/
bool
gidx_contains(GIDX *a, GIDX *b)
{
uint32_t i, dims_a, dims_b;
if (!a || !b)
return false;
if (gidx_is_unknown(a) || gidx_is_unknown(b))
return false;
dims_a = GIDX_NDIMS(a);
dims_b = GIDX_NDIMS(b);
/* For all shared dimensions min(a) > min(b) and max(a) < max(b)
Unshared dimensions do not matter */
for (i = 0; i < Min(dims_a, dims_b); i++)
{
/* If the missing dimension was not padded with -+FLT_MAX */
if (GIDX_GET_MAX(a, i) != FLT_MAX && GIDX_GET_MAX(b, i) != FLT_MAX)
{
if (GIDX_GET_MIN(a, i) > GIDX_GET_MIN(b, i))
return false;
if (GIDX_GET_MAX(a, i) < GIDX_GET_MAX(b, i))
return false;
}
}
return true;
}
/*
** Equality GIDX test.
**
** Box(A) EQUALS Box(B) IFF (pt(A)LL == pt(B)LL) && (pt(A)UR == pt(B)UR)
*/
bool
gidx_equals(GIDX *a, GIDX *b)
{
uint32_t i, dims_a, dims_b;
if (!a && !b)
return true;
if (!a || !b)
return false;
if (gidx_is_unknown(a) && gidx_is_unknown(b))
return true;
if (gidx_is_unknown(a) || gidx_is_unknown(b))
return false;
dims_a = GIDX_NDIMS(a);
dims_b = GIDX_NDIMS(b);
/* For all shared dimensions min(a) == min(b), max(a) == max(b)
Unshared dimensions do not matter */
for (i = 0; i < Min(dims_a, dims_b); i++)
{
/* If the missing dimension was not padded with -+FLT_MAX */
if (GIDX_GET_MAX(a, i) != FLT_MAX && GIDX_GET_MAX(b, i) != FLT_MAX)
{
if (GIDX_GET_MIN(a, i) != GIDX_GET_MIN(b, i))
return false;
if (GIDX_GET_MAX(a, i) != GIDX_GET_MAX(b, i))
return false;
}
}
return true;
}
/**
* Support function. Based on two datums return true if
* they satisfy the predicate and false otherwise.
*/
static int
gserialized_datum_predicate(Datum gs1, Datum gs2, gidx_predicate predicate)
{
/* Put aside some stack memory and use it for GIDX pointers. */
char boxmem1[GIDX_MAX_SIZE];
char boxmem2[GIDX_MAX_SIZE];
GIDX *gidx1 = (GIDX *)boxmem1;
GIDX *gidx2 = (GIDX *)boxmem2;
POSTGIS_DEBUG(3, "entered function");
/* Must be able to build box for each arguement (ie, not empty geometry)
and predicate function to return true. */
if ((gserialized_datum_get_gidx_p(gs1, gidx1) == LW_SUCCESS) &&
(gserialized_datum_get_gidx_p(gs2, gidx2) == LW_SUCCESS) && predicate(gidx1, gidx2))
{
POSTGIS_DEBUGF(3, "got boxes %s and %s", gidx_to_string(gidx1), gidx_to_string(gidx2));
return LW_TRUE;
}
return LW_FALSE;
}
static int
gserialized_datum_predicate_gidx_geom(GIDX *gidx1, Datum gs2, gidx_predicate predicate)
{
/* Put aside some stack memory and use it for GIDX pointers. */
char boxmem2[GIDX_MAX_SIZE];
GIDX *gidx2 = (GIDX *)boxmem2;
POSTGIS_DEBUG(3, "entered function");
/* Must be able to build box for gs2 arguement (ie, not empty geometry)
and predicate function to return true. */
if ((gserialized_datum_get_gidx_p(gs2, gidx2) == LW_SUCCESS) && predicate(gidx1, gidx2))
{
POSTGIS_DEBUGF(3, "got boxes %s and %s", gidx_to_string(gidx1), gidx_to_string(gidx2));
return LW_TRUE;
}
return LW_FALSE;
}
static int
gserialized_datum_predicate_geom_gidx(Datum gs1, GIDX *gidx2, gidx_predicate predicate)
{
/* Put aside some stack memory and use it for GIDX pointers. */
char boxmem2[GIDX_MAX_SIZE];
GIDX *gidx1 = (GIDX *)boxmem2;
POSTGIS_DEBUG(3, "entered function");
/* Must be able to build box for gs2 arguement (ie, not empty geometry)
and predicate function to return true. */
if ((gserialized_datum_get_gidx_p(gs1, gidx1) == LW_SUCCESS) && predicate(gidx1, gidx2))
{
POSTGIS_DEBUGF(3, "got boxes %s and %s", gidx_to_string(gidx1), gidx_to_string(gidx2));
return LW_TRUE;
}
return LW_FALSE;
}
/**
* Calculate the box->box distance.
*/
static double
gidx_distance(const GIDX *a, const GIDX *b, int m_is_time)
{
int ndims, i;
double sum = 0;
/* Base computation on least available dimensions */
ndims = Min(GIDX_NDIMS(b), GIDX_NDIMS(a));
for (i = 0; i < ndims; ++i)
{
double d;
double amin = GIDX_GET_MIN(a, i);
double amax = GIDX_GET_MAX(a, i);
double bmin = GIDX_GET_MIN(b, i);
double bmax = GIDX_GET_MAX(b, i);
POSTGIS_DEBUGF(3, "A %g - %g", amin, amax);
POSTGIS_DEBUGF(3, "B %g - %g", bmin, bmax);
if ((amin <= bmax && amax >= bmin))
{
/* overlaps */
d = 0;
}
else if (i == 4 && m_is_time)
{
return FLT_MAX;
}
else if (bmax < amin)
{
/* is "left" */
d = amin - bmax;
}
else
{
/* is "right" */
assert(bmin > amax);
d = bmin - amax;
}
if (!isfinite(d))
{
/* Can happen if coordinates are corrupted/NaN */
continue;
}
sum += d * d;
POSTGIS_DEBUGF(3, "dist %g, squared %g, grows sum to %g", d, d * d, sum);
}
return sqrt(sum);
}
static double
gidx_distance_m(const GIDX *a, const GIDX *b)
{
int mdim_a, mdim_b;
double d, amin, amax, bmin, bmax;
/* Base computation on least available dimensions */
mdim_a = GIDX_NDIMS(a) - 1;
mdim_b = GIDX_NDIMS(b) - 1;
amin = GIDX_GET_MIN(a, mdim_a);
amax = GIDX_GET_MAX(a, mdim_a);
bmin = GIDX_GET_MIN(b, mdim_b);
bmax = GIDX_GET_MAX(b, mdim_b);
if ((amin <= bmax && amax >= bmin))
{
/* overlaps */
d = 0;
}
else if (bmax < amin)
{
/* is "left" */
d = amin - bmax;
}
else
{
/* is "right" */
assert(bmin > amax);
d = bmin - amax;
}
return d;
}
/**
* Return a #GSERIALIZED with an expanded bounding box.
*/
GSERIALIZED *
gserialized_expand(GSERIALIZED *g, double distance)
{
char boxmem[GIDX_MAX_SIZE];
GIDX *gidx = (GIDX *)boxmem;
float fdistance = (float)distance;
/* Get our bounding box out of the geography, return right away if
input is an EMPTY geometry. */
if (gserialized_get_gidx_p(g, gidx) == LW_FAILURE)
return g;
gidx_expand(gidx, fdistance);
return gserialized_set_gidx(g, gidx);
}
/***********************************************************************
* GiST N-D Index Operator Functions
*/
/*
* do "real" n-d distance
*/
PG_FUNCTION_INFO_V1(gserialized_distance_nd);
Datum gserialized_distance_nd(PG_FUNCTION_ARGS)
{
char b1mem[GIDX_MAX_SIZE];
GIDX *b1 = (GIDX *)b1mem;
char b2mem[GIDX_MAX_SIZE];
GIDX *b2 = (GIDX *)b2mem;
/* Feature-to-feature distance */
GSERIALIZED *geom1 = PG_GETARG_GSERIALIZED_P(0);
GSERIALIZED *geom2 = PG_GETARG_GSERIALIZED_P(1);
LWGEOM *lw1 = lwgeom_from_gserialized(geom1);
LWGEOM *lw2 = lwgeom_from_gserialized(geom2);
LWGEOM *closest;
double distance;
/* Find an exact shortest line w/ the dimensions we support */
if (lwgeom_has_z(lw1) && lwgeom_has_z(lw2))
{
closest = lwgeom_closest_line_3d(lw1, lw2);
distance = lwgeom_length(closest);
}
else
{
closest = lwgeom_closest_line(lw1, lw2);
distance = lwgeom_length_2d(closest);
}
/* Can only add the M term if both objects have M */
if (lwgeom_has_m(lw1) && lwgeom_has_m(lw2))
{
double m1, m2;
int usebox = false;
/* Un-sqrt the distance so we can add extra terms */
distance = distance * distance;
if (lwgeom_get_type(lw1) == POINTTYPE)
{
POINT4D p;
lwpoint_getPoint4d_p((LWPOINT *)lw1, &p);
m1 = p.m;
}
else if (lwgeom_get_type(lw1) == LINETYPE)
{
LWPOINT *lwp1 = lwline_get_lwpoint(lwgeom_as_lwline(closest), 0);
m1 = lwgeom_interpolate_point(lw1, lwp1);
lwpoint_free(lwp1);
}
else
usebox = true;
if (lwgeom_get_type(lw2) == POINTTYPE)
{
POINT4D p;
lwpoint_getPoint4d_p((LWPOINT *)lw2, &p);
m2 = p.m;
}
else if (lwgeom_get_type(lw2) == LINETYPE)
{
LWPOINT *lwp2 = lwline_get_lwpoint(lwgeom_as_lwline(closest), 1);
m2 = lwgeom_interpolate_point(lw2, lwp2);
lwpoint_free(lwp2);
}
else
usebox = true;
if (usebox)
{
double d;
gserialized_get_gidx_p(geom1, b1);
gserialized_get_gidx_p(geom2, b2);
d = gidx_distance_m(b1, b2);
distance += d * d;
}
else
distance += (m2 - m1) * (m2 - m1);
distance = sqrt(distance);
}
lwgeom_free(closest);
PG_FREE_IF_COPY(geom1, 0);
PG_FREE_IF_COPY(geom2, 1);
PG_RETURN_FLOAT8(distance);
}
/*
** '~~' and operator function. Based on two serialized return true if
** the first is contained by the second.
*/
PG_FUNCTION_INFO_V1(gserialized_within);
Datum gserialized_within(PG_FUNCTION_ARGS)
{
if (gserialized_datum_predicate(PG_GETARG_DATUM(1), PG_GETARG_DATUM(0), gidx_contains))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
#if POSTGIS_PGSQL_VERSION > 94
/*
** '~~' and operator function. Based on a GIDX and a serialized return true if
** the first is contained by the second.
*/
PG_FUNCTION_INFO_V1(gserialized_gidx_geom_within);
Datum gserialized_gidx_geom_within(PG_FUNCTION_ARGS)
{
GIDX *gidx = (GIDX *)PG_GETARG_POINTER(0);
if (gserialized_datum_predicate_geom_gidx(PG_GETARG_DATUM(1), gidx, gidx_contains))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
** '~~' and operator function. Based on two GIDX return true if
** the first is contained by the second.
*/
PG_FUNCTION_INFO_V1(gserialized_gidx_gidx_within);
Datum gserialized_gidx_gidx_within(PG_FUNCTION_ARGS)
{
if (gidx_contains((GIDX *)PG_GETARG_POINTER(1), (GIDX *)PG_GETARG_POINTER(0)))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
#endif
/*
** '@@' and operator function. Based on two serialized return true if
** the first contains the second.
*/
PG_FUNCTION_INFO_V1(gserialized_contains);
Datum gserialized_contains(PG_FUNCTION_ARGS)
{
if (gserialized_datum_predicate(PG_GETARG_DATUM(0), PG_GETARG_DATUM(1), gidx_contains))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
** '@@' and operator function. Based on a GIDX and a serialized return true if
** the first contains the second.
*/
PG_FUNCTION_INFO_V1(gserialized_gidx_geom_contains);
Datum gserialized_gidx_geom_contains(PG_FUNCTION_ARGS)
{
GIDX *gidx = (GIDX *)PG_GETARG_POINTER(0);
if (gserialized_datum_predicate_gidx_geom(gidx, PG_GETARG_DATUM(1), gidx_contains))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
** '@@' and operator function. Based on two GIDX return true if
** the first contains the second.
*/
PG_FUNCTION_INFO_V1(gserialized_gidx_gidx_contains);
Datum gserialized_gidx_gidx_contains(PG_FUNCTION_ARGS)
{
if (gidx_contains((GIDX *)PG_GETARG_POINTER(0), (GIDX *)PG_GETARG_POINTER(1)))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
** '~=' and operator function. Based on two serialized return true if
** the first equals the second.
*/
PG_FUNCTION_INFO_V1(gserialized_same);
Datum gserialized_same(PG_FUNCTION_ARGS)
{
if (gserialized_datum_predicate(PG_GETARG_DATUM(0), PG_GETARG_DATUM(1), gidx_equals))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
PG_FUNCTION_INFO_V1(gserialized_gidx_geom_same);
Datum gserialized_gidx_geom_same(PG_FUNCTION_ARGS)
{
GIDX *gidx = (GIDX *)PG_GETARG_POINTER(0);
if (gserialized_datum_predicate_gidx_geom(gidx, PG_GETARG_DATUM(1), gidx_equals))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
** '~=' and operator function. Based on two GIDX return true if
** the first equals the second.
*/
PG_FUNCTION_INFO_V1(gserialized_gidx_gidx_same);
Datum gserialized_gidx_gidx_same(PG_FUNCTION_ARGS)
{
if (gidx_equals((GIDX *)PG_GETARG_POINTER(0), (GIDX *)PG_GETARG_POINTER(1)))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
** '&&&' operator function. Based on two serialized return true if
** they overlap and false otherwise.
*/
PG_FUNCTION_INFO_V1(gserialized_overlaps);
Datum gserialized_overlaps(PG_FUNCTION_ARGS)
{
if (gserialized_datum_predicate(PG_GETARG_DATUM(0), PG_GETARG_DATUM(1), gidx_overlaps))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/*
* This is the cross-operator for the geographies
*/
PG_FUNCTION_INFO_V1(gserialized_gidx_geog_overlaps);
Datum gserialized_gidx_geog_overlaps(PG_FUNCTION_ARGS)
{
GIDX *gidx = (GIDX *)PG_GETARG_POINTER(0);
if (gserialized_datum_predicate_gidx_geom(gidx, PG_GETARG_DATUM(1), gidx_overlaps))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
PG_FUNCTION_INFO_V1(gserialized_gidx_geom_overlaps);
Datum gserialized_gidx_geom_overlaps(PG_FUNCTION_ARGS)
{
GIDX *gidx = (GIDX *)PG_GETARG_POINTER(0);
if (gserialized_datum_predicate_gidx_geom(gidx, PG_GETARG_DATUM(1), gidx_overlaps))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
PG_FUNCTION_INFO_V1(gserialized_gidx_gidx_overlaps);
Datum gserialized_gidx_gidx_overlaps(PG_FUNCTION_ARGS)
{
if (gidx_overlaps((GIDX *)PG_GETARG_POINTER(0), (GIDX *)PG_GETARG_POINTER(1)))
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(false);
}
/***********************************************************************
* GiST Index Support Functions
*/
/*
** GiST support function. Given a geography, return a "compressed"
** version. In this case, we convert the geography into a geocentric
** bounding box. If the geography already has the box embedded in it
** we pull that out and hand it back.
*/
PG_FUNCTION_INFO_V1(gserialized_gist_compress);
Datum gserialized_gist_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry_in = (GISTENTRY *)PG_GETARG_POINTER(0);
GISTENTRY *entry_out = NULL;
char gidxmem[GIDX_MAX_SIZE];
GIDX *bbox_out = (GIDX *)gidxmem;
int result = LW_SUCCESS;
uint32_t i;
POSTGIS_DEBUG(4, "[GIST] 'compress' function called");
/*
** Not a leaf key? There's nothing to do.
** Return the input unchanged.
*/
if (!entry_in->leafkey)
{
POSTGIS_DEBUG(4, "[GIST] non-leafkey entry, returning input unaltered");
PG_RETURN_POINTER(entry_in);
}
POSTGIS_DEBUG(4, "[GIST] processing leafkey input");
entry_out = palloc(sizeof(GISTENTRY));
/*
** Null key? Make a copy of the input entry and
** return.
*/
if (!DatumGetPointer(entry_in->key))
{
POSTGIS_DEBUG(4, "[GIST] leafkey is null");
gistentryinit(*entry_out, (Datum)0, entry_in->rel, entry_in->page, entry_in->offset, false);
POSTGIS_DEBUG(4, "[GIST] returning copy of input");
PG_RETURN_POINTER(entry_out);
}
/* Extract our index key from the GiST entry. */
result = gserialized_datum_get_gidx_p(entry_in->key, bbox_out);
/* Is the bounding box valid (non-empty, non-infinite) ?
* If not, use the "unknown" GIDX. */
if (result == LW_FAILURE)
{
POSTGIS_DEBUG(4, "[GIST] empty geometry!");
gidx_set_unknown(bbox_out);
gistentryinit(*entry_out,
PointerGetDatum(gidx_copy(bbox_out)),
entry_in->rel,
entry_in->page,
entry_in->offset,
false);
PG_RETURN_POINTER(entry_out);
}