Skip to content

Commit 24bebf0

Browse files
committed
I have included fixes to declare some floating point constants as double
instead of int, change the calculation method to use the haversine formula which is more accurrate for short distances, added a grant to public for geo_distance and added a regression test. I will resubmit the earth distance stuff based on cube after 7.3 is released. Bruno Wolff III
1 parent a834cbe commit 24bebf0

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

contrib/earthdistance/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Header: /cvsroot/pgsql/contrib/earthdistance/Makefile,v 1.11 2001/09/06 10:49:29 petere Exp $
1+
# $Header: /cvsroot/pgsql/contrib/earthdistance/Makefile,v 1.12 2002/09/20 03:47:22 momjian Exp $
22

33
subdir = contrib/earthdistance
44
top_builddir = ../..
@@ -7,5 +7,6 @@ include $(top_builddir)/src/Makefile.global
77
MODULES = earthdistance
88
DATA_built = earthdistance.sql
99
DOCS = README.earthdistance
10+
REGRESS = earthdistance
1011

1112
include $(top_srcdir)/contrib/contrib-global.mk

contrib/earthdistance/README.earthdistance

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
---------------------------------------------------------------------
2+
I corrected a bug in the geo_distance code where two double constants
3+
were declared as int. I changed the distance function to use the
4+
haversine formula which is more accurate for small distances.
5+
I added a regression test to the package. I added a grant statement
6+
to give execute access for geo_distance to public.
7+
8+
Bruno Wolff III
9+
September 2002
10+
---------------------------------------------------------------------
111
Date: Wed, 1 Apr 1998 15:19:32 -0600 (CST)
212
From: Hal Snyder <hal@vailsys.com>
313
To: vmehr@ctp.com

contrib/earthdistance/earthdistance.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77

88
/* Earth's radius is in statute miles. */
9-
const int EARTH_RADIUS = 3958.747716;
10-
const int TWO_PI = 2.0 * M_PI;
9+
const double EARTH_RADIUS = 3958.747716;
10+
const double TWO_PI = 2.0 * M_PI;
1111

1212
double *geo_distance(Point *pt1, Point *pt2);
1313

@@ -50,6 +50,7 @@ geo_distance(Point *pt1, Point *pt2)
5050
long2,
5151
lat2;
5252
double longdiff;
53+
double sino;
5354
double *resultp = palloc(sizeof(double));
5455

5556
/* convert degrees to radians */
@@ -65,8 +66,10 @@ geo_distance(Point *pt1, Point *pt2)
6566
if (longdiff > M_PI)
6667
longdiff = TWO_PI - longdiff;
6768

68-
*resultp = EARTH_RADIUS * acos
69-
(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(longdiff));
69+
sino = sqrt(sin(fabs(lat1-lat2)/2.)*sin(fabs(lat1-lat2)/2.) +
70+
cos(lat1) * cos(lat2) * sin(longdiff/2.)*sin(longdiff/2.));
71+
if (sino > 1.) sino = 1.;
72+
*resultp = 2. * EARTH_RADIUS * asin(sino);
7073

7174
return resultp;
7275
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
begin;
12

23
--------------- geo_distance
34

4-
DROP FUNCTION geo_distance (point, point);
5-
CREATE FUNCTION geo_distance (point, point) RETURNS float8
6-
AS 'MODULE_PATHNAME' LANGUAGE 'c'
7-
WITH (isstrict);
8-
9-
SELECT geo_distance ('(1,2)'::point, '(3,4)'::point);
5+
CREATE OR REPLACE FUNCTION geo_distance (point, point) RETURNS float8
6+
LANGUAGE 'c' IMMUTABLE STRICT AS 'MODULE_PATHNAME';
107

118
--------------- geo_distance as operator <@>
129

13-
DROP OPERATOR <@> (point, point);
1410
CREATE OPERATOR <@> (
1511
leftarg = point,
1612
rightarg = point,
1713
procedure = geo_distance,
1814
commutator = <@>
1915
);
2016

21-
-- ( 87.6, 41.8) is in Chicago
22-
-- (106.7, 35.1) is in Albuquerque
23-
-- The cities are about 1100 miles apart
24-
SELECT '(87.6,41.8)'::point <@> '(106.7,35.1)'::point;
17+
--
18+
-- By default this function is made executable by anyone. To restrict
19+
-- access by default, comment out the following grant command.
20+
--
21+
22+
grant execute on function geo_distance(point, point) to public;
23+
24+
commit;

0 commit comments

Comments
 (0)