PostGIS  2.4.9dev-r@@SVN_REVISION@@
geography_btree.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
22  *
23  **********************************************************************/
24 
25 
26 #include "postgres.h"
27 #include "access/hash.h"
28 
29 #include "../postgis_config.h"
30 
31 #include "liblwgeom.h" /* For standard geometry types. */
32 #include "liblwgeom_internal.h" /* For FP comparators. */
33 #include "lwgeom_pg.h" /* For debugging macros. */
34 #include "gserialized_gist.h"
35 #include "geography.h" /* For utility functions. */
36 
37 Datum geography_lt(PG_FUNCTION_ARGS);
38 Datum geography_le(PG_FUNCTION_ARGS);
39 Datum geography_eq(PG_FUNCTION_ARGS);
40 Datum geography_ge(PG_FUNCTION_ARGS);
41 Datum geography_gt(PG_FUNCTION_ARGS);
42 Datum geography_cmp(PG_FUNCTION_ARGS);
43 
44 /*
45 ** BTree support function. Based on two geographies return true if
46 ** they are "less than" and false otherwise.
47 */
49 Datum geography_lt(PG_FUNCTION_ARGS)
50 {
51  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
52  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
53  int cmp = gserialized_cmp(g1, g2);
54  PG_FREE_IF_COPY(g1, 0);
55  PG_FREE_IF_COPY(g2, 1);
56  if (cmp < 0)
57  PG_RETURN_BOOL(TRUE);
58  else
59  PG_RETURN_BOOL(FALSE);
60 }
61 
62 /*
63 ** BTree support function. Based on two geographies return true if
64 ** they are "less than or equal" and false otherwise.
65 */
67 Datum geography_le(PG_FUNCTION_ARGS)
68 {
69  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
70  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
71  int cmp = gserialized_cmp(g1, g2);
72  PG_FREE_IF_COPY(g1, 0);
73  PG_FREE_IF_COPY(g2, 1);
74  if (cmp <= 0)
75  PG_RETURN_BOOL(TRUE);
76  else
77  PG_RETURN_BOOL(FALSE);
78 }
79 
80 /*
81 ** BTree support function. Based on two geographies return true if
82 ** they are "greater than" and false otherwise.
83 */
85 Datum geography_gt(PG_FUNCTION_ARGS)
86 {
87  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
88  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
89  int cmp = gserialized_cmp(g1, g2);
90  PG_FREE_IF_COPY(g1, 0);
91  PG_FREE_IF_COPY(g2, 1);
92  if (cmp > 0)
93  PG_RETURN_BOOL(TRUE);
94  else
95  PG_RETURN_BOOL(FALSE);
96 }
97 
98 /*
99 ** BTree support function. Based on two geographies return true if
100 ** they are "greater than or equal" and false otherwise.
101 */
103 Datum geography_ge(PG_FUNCTION_ARGS)
104 {
105  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
106  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
107  int cmp = gserialized_cmp(g1, g2);
108  PG_FREE_IF_COPY(g1, 0);
109  PG_FREE_IF_COPY(g2, 1);
110  if (cmp >= 0)
111  PG_RETURN_BOOL(TRUE);
112  else
113  PG_RETURN_BOOL(FALSE);
114 }
115 
116 /*
117 ** BTree support function. Based on two geographies return true if
118 ** they are "equal" and false otherwise.
119 */
121 Datum geography_eq(PG_FUNCTION_ARGS)
122 {
123  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
124  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
125  int cmp = gserialized_cmp(g1, g2);
126  PG_FREE_IF_COPY(g1, 0);
127  PG_FREE_IF_COPY(g2, 1);
128  if (cmp == 0)
129  PG_RETURN_BOOL(TRUE);
130  else
131  PG_RETURN_BOOL(FALSE);
132 }
133 
134 /*
135 ** BTree support function. Based on two geographies return true if
136 ** they are "equal" and false otherwise.
137 */
139 Datum geography_cmp(PG_FUNCTION_ARGS)
140 {
141  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
142  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
143  int ret = gserialized_cmp(g1, g2);
144  PG_FREE_IF_COPY(g1, 0);
145  PG_FREE_IF_COPY(g2, 1);
146  PG_RETURN_INT32(ret);
147 }
int gserialized_cmp(const GSERIALIZED *g1, const GSERIALIZED *g2)
Return -1 if g1 is "less than" g2, 1 if g1 is "greater than" g2 and 0 if g1 and g2 are the "same"...
Definition: g_serialized.c:294
Datum geography_eq(PG_FUNCTION_ARGS)
Datum geography_gt(PG_FUNCTION_ARGS)
Datum geography_le(PG_FUNCTION_ARGS)
Datum geography_ge(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(geography_lt)
Datum geography_lt(PG_FUNCTION_ARGS)
Datum geography_cmp(PG_FUNCTION_ARGS)
#define FALSE
Definition: dbfopen.c:168
#define TRUE
Definition: dbfopen.c:169
This library is the generic geometry handling section of PostGIS.