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
|
--
-- BOX
--
--
-- box logic
-- o
-- 3 o--|X
-- | o|
-- 2 +-+-+ |
-- | | | |
-- 1 | o-+-o
-- | |
-- 0 +---+
--
-- 0 1 2 3
--
-- boxes are specified by two points, given by four floats x1,y1,x2,y2
CREATE TABLE BOX_TBL (f1 box);
INSERT INTO BOX_TBL (f1) VALUES ('(2.0,2.0,0.0,0.0)');
INSERT INTO BOX_TBL (f1) VALUES ('(1.0,1.0,3.0,3.0)');
-- degenerate cases where the box is a line or a point
-- note that lines and points boxes all have zero area
INSERT INTO BOX_TBL (f1) VALUES ('(2.5, 2.5, 2.5,3.5)');
INSERT INTO BOX_TBL (f1) VALUES ('(3.0, 3.0,3.0,3.0)');
-- badly formatted box inputs
INSERT INTO BOX_TBL (f1) VALUES ('(2.3, 4.5)');
ERROR: invalid input syntax for type box: "(2.3, 4.5)"
INSERT INTO BOX_TBL (f1) VALUES ('asdfasdf(ad');
ERROR: invalid input syntax for type box: "asdfasdf(ad"
SELECT '' AS four, * FROM BOX_TBL;
four | f1
------+---------------------
| (2,2),(0,0)
| (3,3),(1,1)
| (2.5,3.5),(2.5,2.5)
| (3,3),(3,3)
(4 rows)
SELECT '' AS four, b.*, area(b.f1) as barea
FROM BOX_TBL b;
four | f1 | barea
------+---------------------+-------
| (2,2),(0,0) | 4
| (3,3),(1,1) | 4
| (2.5,3.5),(2.5,2.5) | 0
| (3,3),(3,3) | 0
(4 rows)
-- overlap
SELECT '' AS three, b.f1
FROM BOX_TBL b
WHERE b.f1 && box '(2.5,2.5,1.0,1.0)';
three | f1
-------+---------------------
| (2,2),(0,0)
| (3,3),(1,1)
| (2.5,3.5),(2.5,2.5)
(3 rows)
-- left-or-overlap (x only)
SELECT '' AS two, b1.*
FROM BOX_TBL b1
WHERE b1.f1 &< box '(2.0,2.0,2.5,2.5)';
two | f1
-----+---------------------
| (2,2),(0,0)
| (2.5,3.5),(2.5,2.5)
(2 rows)
-- right-or-overlap (x only)
SELECT '' AS two, b1.*
FROM BOX_TBL b1
WHERE b1.f1 &> box '(2.0,2.0,2.5,2.5)';
two | f1
-----+---------------------
| (2.5,3.5),(2.5,2.5)
| (3,3),(3,3)
(2 rows)
-- left of
SELECT '' AS two, b.f1
FROM BOX_TBL b
WHERE b.f1 << box '(3.0,3.0,5.0,5.0)';
two | f1
-----+---------------------
| (2,2),(0,0)
| (2.5,3.5),(2.5,2.5)
(2 rows)
-- area <=
SELECT '' AS four, b.f1
FROM BOX_TBL b
WHERE b.f1 <= box '(3.0,3.0,5.0,5.0)';
four | f1
------+---------------------
| (2,2),(0,0)
| (3,3),(1,1)
| (2.5,3.5),(2.5,2.5)
| (3,3),(3,3)
(4 rows)
-- area <
SELECT '' AS two, b.f1
FROM BOX_TBL b
WHERE b.f1 < box '(3.0,3.0,5.0,5.0)';
two | f1
-----+---------------------
| (2.5,3.5),(2.5,2.5)
| (3,3),(3,3)
(2 rows)
-- area =
SELECT '' AS two, b.f1
FROM BOX_TBL b
WHERE b.f1 = box '(3.0,3.0,5.0,5.0)';
two | f1
-----+-------------
| (2,2),(0,0)
| (3,3),(1,1)
(2 rows)
-- area >
SELECT '' AS two, b.f1
FROM BOX_TBL b -- zero area
WHERE b.f1 > box '(3.5,3.0,4.5,3.0)';
two | f1
-----+-------------
| (2,2),(0,0)
| (3,3),(1,1)
(2 rows)
-- area >=
SELECT '' AS four, b.f1
FROM BOX_TBL b -- zero area
WHERE b.f1 >= box '(3.5,3.0,4.5,3.0)';
four | f1
------+---------------------
| (2,2),(0,0)
| (3,3),(1,1)
| (2.5,3.5),(2.5,2.5)
| (3,3),(3,3)
(4 rows)
-- right of
SELECT '' AS two, b.f1
FROM BOX_TBL b
WHERE box '(3.0,3.0,5.0,5.0)' >> b.f1;
two | f1
-----+---------------------
| (2,2),(0,0)
| (2.5,3.5),(2.5,2.5)
(2 rows)
-- contained in
SELECT '' AS three, b.f1
FROM BOX_TBL b
WHERE b.f1 <@ box '(0,0,3,3)';
three | f1
-------+-------------
| (2,2),(0,0)
| (3,3),(1,1)
| (3,3),(3,3)
(3 rows)
-- contains
SELECT '' AS three, b.f1
FROM BOX_TBL b
WHERE box '(0,0,3,3)' @> b.f1;
three | f1
-------+-------------
| (2,2),(0,0)
| (3,3),(1,1)
| (3,3),(3,3)
(3 rows)
-- box equality
SELECT '' AS one, b.f1
FROM BOX_TBL b
WHERE box '(1,1,3,3)' ~= b.f1;
one | f1
-----+-------------
| (3,3),(1,1)
(1 row)
-- center of box, left unary operator
SELECT '' AS four, @@(b1.f1) AS p
FROM BOX_TBL b1;
four | p
------+---------
| (1,1)
| (2,2)
| (2.5,3)
| (3,3)
(4 rows)
-- wholly-contained
SELECT '' AS one, b1.*, b2.*
FROM BOX_TBL b1, BOX_TBL b2
WHERE b1.f1 @> b2.f1 and not b1.f1 ~= b2.f1;
one | f1 | f1
-----+-------------+-------------
| (3,3),(1,1) | (3,3),(3,3)
(1 row)
SELECT '' AS four, height(f1), width(f1) FROM BOX_TBL;
four | height | width
------+--------+-------
| 2 | 2
| 2 | 2
| 1 | 0
| 0 | 0
(4 rows)
|