From 9ccaaf676ff17f1102542adb1ff05e3a9c873712 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 13 Feb 2003 05:31:06 +0000 Subject: [PATCH] The attached patches change earthdistance to use the new cube functions in one of the earth functions so that latitude and longitude to cartesian coordinates conversion will be more accurrate. (Previously a text string was built to provide as input which limited the accuracy to the number of digits printed.) The new functions were included in a recent patch to contrib/cube that has not as yet been accepted as of yet. I also added check constraints to the domain 'earth' since they are now working in 7.4. Bruno Wolff III --- contrib/earthdistance/README.earthdistance | 29 ++++++++++------------ contrib/earthdistance/earthdistance.sql.in | 17 +++++++------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/contrib/earthdistance/README.earthdistance b/contrib/earthdistance/README.earthdistance index 913179d489..4658d3a1da 100644 --- a/contrib/earthdistance/README.earthdistance +++ b/contrib/earthdistance/README.earthdistance @@ -39,25 +39,22 @@ bounding box usable for index searches. The functions are all 'sql' functions. If you want to make these functions executable by other people you will also have to make the referenced -cube functions executable. cube(text), cube_distance(cube,cube), -cube_ll_coord(cube,int) and cube_enlarge(cube,float8,int) are used indirectly -by the earth distance functions. is_point(cube) and cube_dim(cube) are used -in suggested constraints for data in domain earth. cube_ur_coord(cube,int) -is used in the regression tests and might be useful for looking at bounding -box coordinates in user applications. +cube functions executable. cube(text), cube(float8), cube(cube,float8), +cube_distance(cube,cube), cube_ll_coord(cube,int) and +cube_enlarge(cube,float8,int) are used indirectly by the earth distance +functions. is_point(cube) and cube_dim(cube) are used in constraints for data +in domain earth. cube_ur_coord(cube,int) is used in the regression tests and +might be useful for looking at bounding box coordinates in user applications. -A domain of type cube named earth is defined. Since check constraints -are not supported for domains yet, this isn't as useful as it might be. -However the checks that should be supplied to all data of type earth are: - -constraint not_point check(is_point(earth)) -constraint not_3d check(cube_dim(earth) <= 3) -constraint on_surface check(abs(cube_distance(earth, '(0)'::cube) / - earth() - 1) < '10e-12'::float8); +A domain of type cube named earth is defined. +There are constraints on it defined to make sure the cube is a point, +that it does not have more than 3 dimensions and that it is very near +the surface of a sphere centered about the origin with the radius of +the Earth. The following functions are provided: -earth() - Returns the radius of the earth in meters. +earth() - Returns the radius of the Earth in meters. sec_to_gc(float8) - Converts the normal straight line (secant) distance between between two points on the surface of the Earth to the great circle distance @@ -118,7 +115,7 @@ Subject: [QUESTIONS] Re: Spatial data, R-Trees > look at to create these? Here's the setup for adding an operator '<@>' to give distance in -statute miles between two points on the earth's surface. Coordinates +statute miles between two points on the Earth's surface. Coordinates are in degrees. Points are taken as (longitude, latitude) and not vice versa as longitude is closer to the intuitive idea of x-axis and latitude to y-axis. diff --git a/contrib/earthdistance/earthdistance.sql.in b/contrib/earthdistance/earthdistance.sql.in index de381774ad..6ee9a87b4f 100644 --- a/contrib/earthdistance/earthdistance.sql.in +++ b/contrib/earthdistance/earthdistance.sql.in @@ -24,13 +24,16 @@ AS 'SELECT \'6378168\'::float8'; -- Define domain for locations on the surface of the earth using a cube -- datatype with constraints. cube provides 3D indexing. --- Check constraints aren't currently supported. +-- The cube is restricted to be a point, no more than 3 dimensions +-- (for less than 3 dimensions 0 is assumed for the missing coordinates) +-- and that the point must be very near the surface of the sphere +-- centered about the origin with the radius of the earth. -CREATE DOMAIN earth AS cube; --- CONSTRAINT not_point check(is_point(earth)) --- CONSTRAINT not_3d check(cube_dim(earth) <= 3) --- CONSTRAINT on_surface check(abs(cube_distance(earth, '(0)'::cube) / --- earth() - 1) < '10e-12'::float8); +CREATE DOMAIN earth AS cube + CONSTRAINT not_point check(cube_is_point(value)) + CONSTRAINT not_3d check(cube_dim(value) <= 3) + CONSTRAINT on_surface check(abs(cube_distance(value, '(0)'::cube) / + earth() - 1) < '10e-7'::float8); CREATE OR REPLACE FUNCTION sec_to_gc(float8) RETURNS float8 @@ -48,7 +51,7 @@ CREATE OR REPLACE FUNCTION ll_to_earth(float8, float8) RETURNS earth LANGUAGE 'sql' IMMUTABLE STRICT -AS 'SELECT cube(\'(\'||earth()*cos(radians($1))*cos(radians($2))||\',\'||earth()*cos(radians($1))*sin(radians($2))||\',\'||earth()*sin(radians($1))||\')\')'; +AS 'SELECT cube(cube(cube(earth()*cos(radians($1))*cos(radians($2))),earth()*cos(radians($1))*sin(radians($2))),earth()*sin(radians($1)))'; CREATE OR REPLACE FUNCTION latitude(earth) RETURNS float8