gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.2KB

  1. /* contrib/earthdistance/earthdistance--1.1.sql */
  2. -- complain if script is sourced in psql, rather than via CREATE EXTENSION
  3. \echo Use "CREATE EXTENSION earthdistance" to load this file. \quit
  4. -- earth() returns the radius of the earth in meters. This is the only
  5. -- place you need to change things for the cube base distance functions
  6. -- in order to use different units (or a better value for the Earth's radius).
  7. CREATE FUNCTION earth() RETURNS float8
  8. LANGUAGE SQL IMMUTABLE PARALLEL SAFE
  9. AS 'SELECT ''6378168''::float8';
  10. -- Astronomers may want to change the earth function so that distances will be
  11. -- returned in degrees. To do this comment out the above definition and
  12. -- uncomment the one below. Note that doing this will break the regression
  13. -- tests.
  14. --
  15. -- CREATE FUNCTION earth() RETURNS float8
  16. -- LANGUAGE SQL IMMUTABLE
  17. -- AS 'SELECT 180/pi()';
  18. -- Define domain for locations on the surface of the earth using a cube
  19. -- datatype with constraints. cube provides 3D indexing.
  20. -- The cube is restricted to be a point, no more than 3 dimensions
  21. -- (for less than 3 dimensions 0 is assumed for the missing coordinates)
  22. -- and that the point must be very near the surface of the sphere
  23. -- centered about the origin with the radius of the earth.
  24. CREATE DOMAIN earth AS cube
  25. CONSTRAINT not_point check(cube_is_point(value))
  26. CONSTRAINT not_3d check(cube_dim(value) <= 3)
  27. CONSTRAINT on_surface check(abs(cube_distance(value, '(0)'::cube) /
  28. earth() - '1'::float8) < '10e-7'::float8);
  29. CREATE FUNCTION sec_to_gc(float8)
  30. RETURNS float8
  31. LANGUAGE SQL
  32. IMMUTABLE STRICT
  33. PARALLEL SAFE
  34. AS 'SELECT CASE WHEN $1 < 0 THEN 0::float8 WHEN $1/(2*earth()) > 1 THEN pi()*earth() ELSE 2*earth()*asin($1/(2*earth())) END';
  35. CREATE FUNCTION gc_to_sec(float8)
  36. RETURNS float8
  37. LANGUAGE SQL
  38. IMMUTABLE STRICT
  39. PARALLEL SAFE
  40. AS 'SELECT CASE WHEN $1 < 0 THEN 0::float8 WHEN $1/earth() > pi() THEN 2*earth() ELSE 2*earth()*sin($1/(2*earth())) END';
  41. CREATE FUNCTION ll_to_earth(float8, float8)
  42. RETURNS earth
  43. LANGUAGE SQL
  44. IMMUTABLE STRICT
  45. PARALLEL SAFE
  46. AS 'SELECT cube(cube(cube(earth()*cos(radians($1))*cos(radians($2))),earth()*cos(radians($1))*sin(radians($2))),earth()*sin(radians($1)))::earth';
  47. CREATE FUNCTION latitude(earth)
  48. RETURNS float8
  49. LANGUAGE SQL
  50. IMMUTABLE STRICT
  51. PARALLEL SAFE
  52. AS 'SELECT CASE WHEN cube_ll_coord($1, 3)/earth() < -1 THEN -90::float8 WHEN cube_ll_coord($1, 3)/earth() > 1 THEN 90::float8 ELSE degrees(asin(cube_ll_coord($1, 3)/earth())) END';
  53. CREATE FUNCTION longitude(earth)
  54. RETURNS float8
  55. LANGUAGE SQL
  56. IMMUTABLE STRICT
  57. PARALLEL SAFE
  58. AS 'SELECT degrees(atan2(cube_ll_coord($1, 2), cube_ll_coord($1, 1)))';
  59. CREATE FUNCTION earth_distance(earth, earth)
  60. RETURNS float8
  61. LANGUAGE SQL
  62. IMMUTABLE STRICT
  63. PARALLEL SAFE
  64. AS 'SELECT sec_to_gc(cube_distance($1, $2))';
  65. CREATE FUNCTION earth_box(earth, float8)
  66. RETURNS cube
  67. LANGUAGE SQL
  68. IMMUTABLE STRICT
  69. PARALLEL SAFE
  70. AS 'SELECT cube_enlarge($1, gc_to_sec($2), 3)';
  71. --------------- geo_distance
  72. CREATE FUNCTION geo_distance (point, point)
  73. RETURNS float8
  74. LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE AS 'MODULE_PATHNAME';
  75. --------------- geo_distance as operator <@>
  76. CREATE OPERATOR <@> (
  77. LEFTARG = point,
  78. RIGHTARG = point,
  79. PROCEDURE = geo_distance,
  80. COMMUTATOR = <@>
  81. );
上海开阖软件有限公司 沪ICP备12045867号-1