1 /* contrib/earthdistance/earthdistance.c */
7 #include "utils/geo_decls.h" /* for Point */
10 #define M_PI 3.14159265358979323846
15 /* Earth's radius is in statute miles. */
16 static const double EARTH_RADIUS = 3958.747716;
17 static const double TWO_PI = 2.0 * M_PI;
20 /******************************************************
22 * degtorad - convert degrees to radians
24 * arg: double, angle in degrees
26 * returns: double, same angle in radians
27 ******************************************************/
30 degtorad(double degrees)
32 return (degrees / 360.0) * TWO_PI;
35 /******************************************************
37 * geo_distance_internal - distance between points
40 * a pair of points - for each point,
41 * x-coordinate is longitude in degrees west of Greenwich
42 * y-coordinate is latitude in degrees above equator
45 * distance between the points in miles on earth's surface
46 ******************************************************/
49 geo_distance_internal(Point *pt1, Point *pt2)
58 /* convert degrees to radians */
60 long1 = degtorad(pt1->x);
61 lat1 = degtorad(pt1->y);
63 long2 = degtorad(pt2->x);
64 lat2 = degtorad(pt2->y);
66 /* compute difference in longitudes - want < 180 degrees */
67 longdiff = fabs(long1 - long2);
69 longdiff = TWO_PI - longdiff;
71 sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
72 cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
76 return 2. * EARTH_RADIUS * asin(sino);
80 /******************************************************
82 * geo_distance - distance between points
85 * a pair of points - for each point,
86 * x-coordinate is longitude in degrees west of Greenwich
87 * y-coordinate is latitude in degrees above equator
90 * distance between the points in miles on earth's surface
92 * If float8 is passed-by-value, the oldstyle version-0 calling convention
93 * is unportable, so we use version-1. However, if it's passed-by-reference,
94 * continue to use oldstyle. This is just because we'd like earthdistance
95 * to serve as a canary for any unintentional breakage of version-0 functions
96 * with float8 results.
97 ******************************************************/
99 #ifdef USE_FLOAT8_BYVAL
101 PG_FUNCTION_INFO_V1(geo_distance);
104 geo_distance(PG_FUNCTION_ARGS)
106 Point *pt1 = PG_GETARG_POINT_P(0);
107 Point *pt2 = PG_GETARG_POINT_P(1);
110 result = geo_distance_internal(pt1, pt2);
111 PG_RETURN_FLOAT8(result);
113 #else /* !USE_FLOAT8_BYVAL */
115 double *geo_distance(Point *pt1, Point *pt2);
118 geo_distance(Point *pt1, Point *pt2)
120 double *resultp = palloc(sizeof(double));
122 *resultp = geo_distance_internal(pt1, pt2);
126 #endif /* USE_FLOAT8_BYVAL */