1 /* contrib/earthdistance/earthdistance--1.1--1.2.sql */
3 -- complain if script is sourced in psql, rather than via ALTER EXTENSION
4 \echo Use "ALTER EXTENSION earthdistance UPDATE TO '1.2'" to load this file. \quit
6 CREATE OR REPLACE FUNCTION earth() RETURNS float8
7 LANGUAGE SQL IMMUTABLE PARALLEL SAFE
8 RETURN '6378168'::float8;
10 CREATE OR REPLACE FUNCTION sec_to_gc(float8)
16 WHEN $1 < '0'::float8 THEN '0'::float8
17 WHEN $1 / ('2'::float8 * earth()) > '1'::float8 THEN pi() * earth()
18 ELSE '2'::float8 * earth() * asin($1 / ('2'::float8 * earth()))
21 CREATE OR REPLACE FUNCTION gc_to_sec(float8)
27 WHEN $1 < '0'::float8 THEN '0'::float8
28 WHEN $1 / earth() > pi() THEN '2'::float8 * earth()
29 ELSE '2'::float8 * earth() * sin($1 / ('2'::float8 * earth()))
32 CREATE OR REPLACE FUNCTION ll_to_earth(float8, float8)
37 RETURN @extschema:cube@.cube(@extschema:cube@.cube(@extschema:cube@.cube(
38 earth() * cos(radians($1)) * cos(radians($2))),
39 earth() * cos(radians($1)) * sin(radians($2))),
40 earth() * sin(radians($1)))::earth;
42 CREATE OR REPLACE FUNCTION latitude(earth)
48 WHEN @extschema:cube@.cube_ll_coord($1, 3) / earth() < '-1'::float8 THEN '-90'::float8
49 WHEN @extschema:cube@.cube_ll_coord($1, 3) / earth() > '1'::float8 THEN '90'::float8
50 ELSE degrees(asin(@extschema:cube@.cube_ll_coord($1, 3) / earth()))
53 CREATE OR REPLACE FUNCTION longitude(earth)
58 RETURN degrees(atan2(@extschema:cube@.cube_ll_coord($1, 2),
59 @extschema:cube@.cube_ll_coord($1, 1)));
61 CREATE OR REPLACE FUNCTION earth_distance(earth, earth)
66 RETURN sec_to_gc(@extschema:cube@.cube_distance($1, $2));
68 CREATE OR REPLACE FUNCTION earth_box(earth, float8)
69 RETURNS @extschema:cube@.cube
73 RETURN @extschema:cube@.cube_enlarge($1, gc_to_sec($2), 3);