1- /* $PostgreSQL: pgsql/contrib/earthdistance/earthdistance.c,v 1.13 2006/10/19 20:08:03 tgl Exp $ */
1+ /* $PostgreSQL: pgsql/contrib/earthdistance/earthdistance.c,v 1.14 2008/04/20 01:05:52 tgl Exp $ */
22
33#include "postgres.h"
44
55#include <math.h>
66
7- #include "utils/geo_decls.h" /* forPt */
7+ #include "utils/geo_decls.h" /* forPoint */
88
99#ifndef M_PI
1010#define M_PI 3.14159265358979323846
1414PG_MODULE_MAGIC ;
1515
1616/* Earth's radius is in statute miles. */
17- const double EARTH_RADIUS = 3958.747716 ;
18- const double TWO_PI = 2.0 * M_PI ;
17+ static const double EARTH_RADIUS = 3958.747716 ;
18+ static const double TWO_PI = 2.0 * M_PI ;
1919
20- double * geo_distance (Point * pt1 ,Point * pt2 );
20+ PG_FUNCTION_INFO_V1 (geo_distance );
21+
22+ Datum geo_distance (PG_FUNCTION_ARGS );
2123
2224
2325/******************************************************
@@ -45,21 +47,22 @@ degtorad(double degrees)
4547 * x-coordinate is longitude in degrees west of Greenwich
4648 * y-coordinate is latitude in degrees above equator
4749 *
48- * returns:double
50+ * returns:float8
4951 * distance between the points in miles on earth's surface
5052 ******************************************************/
5153
52- double *
53- geo_distance (Point * pt1 , Point * pt2 )
54+ Datum
55+ geo_distance (PG_FUNCTION_ARGS )
5456{
55-
57+ Point * pt1 = PG_GETARG_POINT_P (0 );
58+ Point * pt2 = PG_GETARG_POINT_P (1 );
59+ float8 result ;
5660double long1 ,
5761lat1 ,
5862long2 ,
5963lat2 ;
6064double longdiff ;
6165double sino ;
62- double * resultp = palloc (sizeof (double ));
6366
6467/* convert degrees to radians */
6568
@@ -78,7 +81,7 @@ geo_distance(Point *pt1, Point *pt2)
7881cos (lat1 )* cos (lat2 )* sin (longdiff /2. )* sin (longdiff /2. ));
7982if (sino > 1. )
8083sino = 1. ;
81- * resultp = 2. * EARTH_RADIUS * asin (sino );
84+ result = 2. * EARTH_RADIUS * asin (sino );
8285
83- return resultp ;
86+ PG_RETURN_FLOAT8 ( result ) ;
8487}