Debian Codegraph @

contrib/earthdistance/sql/earthdistance.sql

raw
1 --
2 -- Test earthdistance extension
3 --
4 -- In this file we also do some testing of extension create/drop scenarios.
5 -- That's really exercising the core database's dependency logic, so ideally
6 -- we'd do it in the core regression tests, but we can't for lack of suitable
7 -- guaranteed-available extensions. earthdistance is a good test case because
8 -- it has a dependency on the cube extension.
9 --
10
11 CREATE EXTENSION earthdistance; -- fail, must install cube first
12 CREATE EXTENSION cube;
13 CREATE EXTENSION earthdistance;
14
15 --
16 -- The radius of the Earth we are using.
17 --
18
19 SELECT earth()::numeric(20,5);
20
21 --
22 -- Convert straight line distances to great circle distances.
23 --
24 SELECT (pi()*earth())::numeric(20,5);
25 SELECT sec_to_gc(0)::numeric(20,5);
26 SELECT sec_to_gc(2*earth())::numeric(20,5);
27 SELECT sec_to_gc(10*earth())::numeric(20,5);
28 SELECT sec_to_gc(-earth())::numeric(20,5);
29 SELECT sec_to_gc(1000)::numeric(20,5);
30 SELECT sec_to_gc(10000)::numeric(20,5);
31 SELECT sec_to_gc(100000)::numeric(20,5);
32 SELECT sec_to_gc(1000000)::numeric(20,5);
33
34 --
35 -- Convert great circle distances to straight line distances.
36 --
37
38 SELECT gc_to_sec(0)::numeric(20,5);
39 SELECT gc_to_sec(sec_to_gc(2*earth()))::numeric(20,5);
40 SELECT gc_to_sec(10*earth())::numeric(20,5);
41 SELECT gc_to_sec(pi()*earth())::numeric(20,5);
42 SELECT gc_to_sec(-1000)::numeric(20,5);
43 SELECT gc_to_sec(1000)::numeric(20,5);
44 SELECT gc_to_sec(10000)::numeric(20,5);
45 SELECT gc_to_sec(100000)::numeric(20,5);
46 SELECT gc_to_sec(1000000)::numeric(20,5);
47
48 --
49 -- Set coordinates using latitude and longitude.
50 -- Extract each coordinate separately so we can round them.
51 --
52
53 SELECT cube_ll_coord(ll_to_earth(0,0),1)::numeric(20,5),
54 cube_ll_coord(ll_to_earth(0,0),2)::numeric(20,5),
55 cube_ll_coord(ll_to_earth(0,0),3)::numeric(20,5);
56 SELECT cube_ll_coord(ll_to_earth(360,360),1)::numeric(20,5),
57 cube_ll_coord(ll_to_earth(360,360),2)::numeric(20,5),
58 cube_ll_coord(ll_to_earth(360,360),3)::numeric(20,5);
59 SELECT cube_ll_coord(ll_to_earth(180,180),1)::numeric(20,5),
60 cube_ll_coord(ll_to_earth(180,180),2)::numeric(20,5),
61 cube_ll_coord(ll_to_earth(180,180),3)::numeric(20,5);
62 SELECT cube_ll_coord(ll_to_earth(180,360),1)::numeric(20,5),
63 cube_ll_coord(ll_to_earth(180,360),2)::numeric(20,5),
64 cube_ll_coord(ll_to_earth(180,360),3)::numeric(20,5);
65 SELECT cube_ll_coord(ll_to_earth(-180,-360),1)::numeric(20,5),
66 cube_ll_coord(ll_to_earth(-180,-360),2)::numeric(20,5),
67 cube_ll_coord(ll_to_earth(-180,-360),3)::numeric(20,5);
68 SELECT cube_ll_coord(ll_to_earth(0,180),1)::numeric(20,5),
69 cube_ll_coord(ll_to_earth(0,180),2)::numeric(20,5),
70 cube_ll_coord(ll_to_earth(0,180),3)::numeric(20,5);
71 SELECT cube_ll_coord(ll_to_earth(0,-180),1)::numeric(20,5),
72 cube_ll_coord(ll_to_earth(0,-180),2)::numeric(20,5),
73 cube_ll_coord(ll_to_earth(0,-180),3)::numeric(20,5);
74 SELECT cube_ll_coord(ll_to_earth(90,0),1)::numeric(20,5),
75 cube_ll_coord(ll_to_earth(90,0),2)::numeric(20,5),
76 cube_ll_coord(ll_to_earth(90,0),3)::numeric(20,5);
77 SELECT cube_ll_coord(ll_to_earth(90,180),1)::numeric(20,5),
78 cube_ll_coord(ll_to_earth(90,180),2)::numeric(20,5),
79 cube_ll_coord(ll_to_earth(90,180),3)::numeric(20,5);
80 SELECT cube_ll_coord(ll_to_earth(-90,0),1)::numeric(20,5),
81 cube_ll_coord(ll_to_earth(-90,0),2)::numeric(20,5),
82 cube_ll_coord(ll_to_earth(-90,0),3)::numeric(20,5);
83 SELECT cube_ll_coord(ll_to_earth(-90,180),1)::numeric(20,5),
84 cube_ll_coord(ll_to_earth(-90,180),2)::numeric(20,5),
85 cube_ll_coord(ll_to_earth(-90,180),3)::numeric(20,5);
86
87 --
88 -- Test getting the latitude of a location.
89 --
90
91 SELECT latitude(ll_to_earth(0,0))::numeric(20,10);
92 SELECT latitude(ll_to_earth(45,0))::numeric(20,10);
93 SELECT latitude(ll_to_earth(90,0))::numeric(20,10);
94 SELECT latitude(ll_to_earth(-45,0))::numeric(20,10);
95 SELECT latitude(ll_to_earth(-90,0))::numeric(20,10);
96 SELECT latitude(ll_to_earth(0,90))::numeric(20,10);
97 SELECT latitude(ll_to_earth(45,90))::numeric(20,10);
98 SELECT latitude(ll_to_earth(90,90))::numeric(20,10);
99 SELECT latitude(ll_to_earth(-45,90))::numeric(20,10);
100 SELECT latitude(ll_to_earth(-90,90))::numeric(20,10);
101 SELECT latitude(ll_to_earth(0,180))::numeric(20,10);
102 SELECT latitude(ll_to_earth(45,180))::numeric(20,10);
103 SELECT latitude(ll_to_earth(90,180))::numeric(20,10);
104 SELECT latitude(ll_to_earth(-45,180))::numeric(20,10);
105 SELECT latitude(ll_to_earth(-90,180))::numeric(20,10);
106 SELECT latitude(ll_to_earth(0,-90))::numeric(20,10);
107 SELECT latitude(ll_to_earth(45,-90))::numeric(20,10);
108 SELECT latitude(ll_to_earth(90,-90))::numeric(20,10);
109 SELECT latitude(ll_to_earth(-45,-90))::numeric(20,10);
110 SELECT latitude(ll_to_earth(-90,-90))::numeric(20,10);
111
112 --
113 -- Test getting the longitude of a location.
114 --
115
116 SELECT longitude(ll_to_earth(0,0))::numeric(20,10);
117 SELECT longitude(ll_to_earth(45,0))::numeric(20,10);
118 SELECT longitude(ll_to_earth(90,0))::numeric(20,10);
119 SELECT longitude(ll_to_earth(-45,0))::numeric(20,10);
120 SELECT longitude(ll_to_earth(-90,0))::numeric(20,10);
121 SELECT longitude(ll_to_earth(0,90))::numeric(20,10);
122 SELECT longitude(ll_to_earth(45,90))::numeric(20,10);
123 SELECT longitude(ll_to_earth(90,90))::numeric(20,10);
124 SELECT longitude(ll_to_earth(-45,90))::numeric(20,10);
125 SELECT longitude(ll_to_earth(-90,90))::numeric(20,10);
126 SELECT longitude(ll_to_earth(0,180))::numeric(20,10);
127 SELECT longitude(ll_to_earth(45,180))::numeric(20,10);
128 SELECT longitude(ll_to_earth(90,180))::numeric(20,10);
129 SELECT longitude(ll_to_earth(-45,180))::numeric(20,10);
130 SELECT longitude(ll_to_earth(-90,180))::numeric(20,10);
131 SELECT longitude(ll_to_earth(0,-90))::numeric(20,10);
132 SELECT longitude(ll_to_earth(45,-90))::numeric(20,10);
133 SELECT longitude(ll_to_earth(90,-90))::numeric(20,10);
134 SELECT longitude(ll_to_earth(-45,-90))::numeric(20,10);
135 SELECT longitude(ll_to_earth(-90,-90))::numeric(20,10);
136
137 --
138 -- For the distance tests the following is some real life data.
139 --
140 -- Chicago has a latitude of 41.8 and a longitude of 87.6.
141 -- Albuquerque has a latitude of 35.1 and a longitude of 106.7.
142 -- (Note that latitude and longitude are specified differently
143 -- in the cube based functions than for the point based functions.)
144 --
145
146 --
147 -- Test getting the distance between two points using earth_distance.
148 --
149
150 SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,0))::numeric(20,5);
151 SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,180))::numeric(20,5);
152 SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(90,0))::numeric(20,5);
153 SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,90))::numeric(20,5);
154 SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,1))::numeric(20,5);
155 SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(1,0))::numeric(20,5);
156 SELECT earth_distance(ll_to_earth(30,0),ll_to_earth(30,1))::numeric(20,5);
157 SELECT earth_distance(ll_to_earth(30,0),ll_to_earth(31,0))::numeric(20,5);
158 SELECT earth_distance(ll_to_earth(60,0),ll_to_earth(60,1))::numeric(20,5);
159 SELECT earth_distance(ll_to_earth(60,0),ll_to_earth(61,0))::numeric(20,5);
160 SELECT earth_distance(ll_to_earth(41.8,87.6),ll_to_earth(35.1,106.7))::numeric(20,5);
161 SELECT (earth_distance(ll_to_earth(41.8,87.6),ll_to_earth(35.1,106.7))*
162 100./2.54/12./5280.)::numeric(20,5);
163
164 --
165 -- Test getting the distance between two points using geo_distance.
166 --
167
168 SELECT geo_distance('(0,0)'::point,'(0,0)'::point)::numeric(20,5);
169 SELECT geo_distance('(0,0)'::point,'(180,0)'::point)::numeric(20,5);
170 SELECT geo_distance('(0,0)'::point,'(0,90)'::point)::numeric(20,5);
171 SELECT geo_distance('(0,0)'::point,'(90,0)'::point)::numeric(20,5);
172 SELECT geo_distance('(0,0)'::point,'(1,0)'::point)::numeric(20,5);
173 SELECT geo_distance('(0,0)'::point,'(0,1)'::point)::numeric(20,5);
174 SELECT geo_distance('(0,30)'::point,'(1,30)'::point)::numeric(20,5);
175 SELECT geo_distance('(0,30)'::point,'(0,31)'::point)::numeric(20,5);
176 SELECT geo_distance('(0,60)'::point,'(1,60)'::point)::numeric(20,5);
177 SELECT geo_distance('(0,60)'::point,'(0,61)'::point)::numeric(20,5);
178 SELECT geo_distance('(87.6,41.8)'::point,'(106.7,35.1)'::point)::numeric(20,5);
179 SELECT (geo_distance('(87.6,41.8)'::point,'(106.7,35.1)'::point)*5280.*12.*2.54/100.)::numeric(20,5);
180
181 --
182 -- Test getting the distance between two points using the <@> operator.
183 --
184
185 SELECT ('(0,0)'::point <@> '(0,0)'::point)::numeric(20,5);
186 SELECT ('(0,0)'::point <@> '(180,0)'::point)::numeric(20,5);
187 SELECT ('(0,0)'::point <@> '(0,90)'::point)::numeric(20,5);
188 SELECT ('(0,0)'::point <@> '(90,0)'::point)::numeric(20,5);
189 SELECT ('(0,0)'::point <@> '(1,0)'::point)::numeric(20,5);
190 SELECT ('(0,0)'::point <@> '(0,1)'::point)::numeric(20,5);
191 SELECT ('(0,30)'::point <@> '(1,30)'::point)::numeric(20,5);
192 SELECT ('(0,30)'::point <@> '(0,31)'::point)::numeric(20,5);
193 SELECT ('(0,60)'::point <@> '(1,60)'::point)::numeric(20,5);
194 SELECT ('(0,60)'::point <@> '(0,61)'::point)::numeric(20,5);
195 SELECT ('(87.6,41.8)'::point <@> '(106.7,35.1)'::point)::numeric(20,5);
196 SELECT (('(87.6,41.8)'::point <@> '(106.7,35.1)'::point)*5280.*12.*2.54/100.)::numeric(20,5);
197
198 --
199 -- Test getting a bounding box around points.
200 --
201
202 SELECT cube_ll_coord(earth_box(ll_to_earth(0,0),112000),1)::numeric(20,5),
203 cube_ll_coord(earth_box(ll_to_earth(0,0),112000),2)::numeric(20,5),
204 cube_ll_coord(earth_box(ll_to_earth(0,0),112000),3)::numeric(20,5),
205 cube_ur_coord(earth_box(ll_to_earth(0,0),112000),1)::numeric(20,5),
206 cube_ur_coord(earth_box(ll_to_earth(0,0),112000),2)::numeric(20,5),
207 cube_ur_coord(earth_box(ll_to_earth(0,0),112000),3)::numeric(20,5);
208 SELECT cube_ll_coord(earth_box(ll_to_earth(0,0),pi()*earth()),1)::numeric(20,5),
209 cube_ll_coord(earth_box(ll_to_earth(0,0),pi()*earth()),2)::numeric(20,5),
210 cube_ll_coord(earth_box(ll_to_earth(0,0),pi()*earth()),3)::numeric(20,5),
211 cube_ur_coord(earth_box(ll_to_earth(0,0),pi()*earth()),1)::numeric(20,5),
212 cube_ur_coord(earth_box(ll_to_earth(0,0),pi()*earth()),2)::numeric(20,5),
213 cube_ur_coord(earth_box(ll_to_earth(0,0),pi()*earth()),3)::numeric(20,5);
214 SELECT cube_ll_coord(earth_box(ll_to_earth(0,0),10*earth()),1)::numeric(20,5),
215 cube_ll_coord(earth_box(ll_to_earth(0,0),10*earth()),2)::numeric(20,5),
216 cube_ll_coord(earth_box(ll_to_earth(0,0),10*earth()),3)::numeric(20,5),
217 cube_ur_coord(earth_box(ll_to_earth(0,0),10*earth()),1)::numeric(20,5),
218 cube_ur_coord(earth_box(ll_to_earth(0,0),10*earth()),2)::numeric(20,5),
219 cube_ur_coord(earth_box(ll_to_earth(0,0),10*earth()),3)::numeric(20,5);
220
221 --
222 -- Test for points that should be in bounding boxes.
223 --
224
225 SELECT earth_box(ll_to_earth(0,0),
226 earth_distance(ll_to_earth(0,0),ll_to_earth(0,1))*1.00001) @>
227 ll_to_earth(0,1);
228 SELECT earth_box(ll_to_earth(0,0),
229 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.1))*1.00001) @>
230 ll_to_earth(0,0.1);
231 SELECT earth_box(ll_to_earth(0,0),
232 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.01))*1.00001) @>
233 ll_to_earth(0,0.01);
234 SELECT earth_box(ll_to_earth(0,0),
235 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.001))*1.00001) @>
236 ll_to_earth(0,0.001);
237 SELECT earth_box(ll_to_earth(0,0),
238 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.0001))*1.00001) @>
239 ll_to_earth(0,0.0001);
240 SELECT earth_box(ll_to_earth(0,0),
241 earth_distance(ll_to_earth(0,0),ll_to_earth(0.0001,0.0001))*1.00001) @>
242 ll_to_earth(0.0001,0.0001);
243 SELECT earth_box(ll_to_earth(45,45),
244 earth_distance(ll_to_earth(45,45),ll_to_earth(45.0001,45.0001))*1.00001) @>
245 ll_to_earth(45.0001,45.0001);
246 SELECT earth_box(ll_to_earth(90,180),
247 earth_distance(ll_to_earth(90,180),ll_to_earth(90.0001,180.0001))*1.00001) @>
248 ll_to_earth(90.0001,180.0001);
249
250 --
251 -- Test for points that shouldn't be in bounding boxes. Note that we need
252 -- to make points way outside, since some points close may be in the box
253 -- but further away than the distance we are testing.
254 --
255
256 SELECT earth_box(ll_to_earth(0,0),
257 earth_distance(ll_to_earth(0,0),ll_to_earth(0,1))*.57735) @>
258 ll_to_earth(0,1);
259 SELECT earth_box(ll_to_earth(0,0),
260 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.1))*.57735) @>
261 ll_to_earth(0,0.1);
262 SELECT earth_box(ll_to_earth(0,0),
263 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.01))*.57735) @>
264 ll_to_earth(0,0.01);
265 SELECT earth_box(ll_to_earth(0,0),
266 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.001))*.57735) @>
267 ll_to_earth(0,0.001);
268 SELECT earth_box(ll_to_earth(0,0),
269 earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.0001))*.57735) @>
270 ll_to_earth(0,0.0001);
271 SELECT earth_box(ll_to_earth(0,0),
272 earth_distance(ll_to_earth(0,0),ll_to_earth(0.0001,0.0001))*.57735) @>
273 ll_to_earth(0.0001,0.0001);
274 SELECT earth_box(ll_to_earth(45,45),
275 earth_distance(ll_to_earth(45,45),ll_to_earth(45.0001,45.0001))*.57735) @>
276 ll_to_earth(45.0001,45.0001);
277 SELECT earth_box(ll_to_earth(90,180),
278 earth_distance(ll_to_earth(90,180),ll_to_earth(90.0001,180.0001))*.57735) @>
279 ll_to_earth(90.0001,180.0001);
280
281 --
282 -- Test the recommended constraints.
283 --
284
285 SELECT cube_is_point(ll_to_earth(0,0));
286 SELECT cube_dim(ll_to_earth(0,0)) <= 3;
287 SELECT abs(cube_distance(ll_to_earth(0,0), '(0)'::cube) / earth() - 1) <
288 '10e-12'::float8;
289 SELECT cube_is_point(ll_to_earth(30,60));
290 SELECT cube_dim(ll_to_earth(30,60)) <= 3;
291 SELECT abs(cube_distance(ll_to_earth(30,60), '(0)'::cube) / earth() - 1) <
292 '10e-12'::float8;
293 SELECT cube_is_point(ll_to_earth(60,90));
294 SELECT cube_dim(ll_to_earth(60,90)) <= 3;
295 SELECT abs(cube_distance(ll_to_earth(60,90), '(0)'::cube) / earth() - 1) <
296 '10e-12'::float8;
297 SELECT cube_is_point(ll_to_earth(-30,-90));
298 SELECT cube_dim(ll_to_earth(-30,-90)) <= 3;
299 SELECT abs(cube_distance(ll_to_earth(-30,-90), '(0)'::cube) / earth() - 1) <
300 '10e-12'::float8;
301
302 --
303 -- Now we are going to test extension create/drop scenarios.
304 --
305
306 -- list what's installed
307 \dT
308
309 drop extension cube; -- fail, earthdistance requires it
310
311 drop extension earthdistance;
312
313 drop type cube; -- fail, extension cube requires it
314
315 -- list what's installed
316 \dT
317
318 create table foo (f1 cube, f2 int);
319
320 drop extension cube; -- fail, foo.f1 requires it
321
322 drop table foo;
323
324 drop extension cube;
325
326 -- list what's installed
327 \dT
328 \df
329 \do
330
331 create schema c;
332
333 create extension cube with schema c;
334
335 -- list what's installed
336 \dT public.*
337 \df public.*
338 \do public.*
339 \dT c.*
340
341 create table foo (f1 c.cube, f2 int);
342
343 drop extension cube; -- fail, foo.f1 requires it
344
345 drop schema c; -- fail, cube requires it
346
347 drop extension cube cascade;
348
349 \d foo
350
351 -- list what's installed
352 \dT public.*
353 \df public.*
354 \do public.*
355 \dT c.*
356 \df c.*
357 \do c.*
358
359 drop schema c;