Only skip pages marked as clean in the visibility map, if the last 32
[PostgreSQL.git] / contrib / earthdistance / earthdistance.c
blob5230d97541c73eb5883ebc660464665073334e10
1 /* $PostgreSQL$ */
3 #include "postgres.h"
5 #include <math.h>
7 #include "utils/geo_decls.h" /* for Point */
9 #ifndef M_PI
10 #define M_PI 3.14159265358979323846
11 #endif
14 PG_MODULE_MAGIC;
16 /* Earth's radius is in statute miles. */
17 static const double EARTH_RADIUS = 3958.747716;
18 static const double TWO_PI = 2.0 * M_PI;
21 /******************************************************
23 * degtorad - convert degrees to radians
25 * arg: double, angle in degrees
27 * returns: double, same angle in radians
28 ******************************************************/
30 static double
31 degtorad(double degrees)
33 return (degrees / 360.0) * TWO_PI;
36 /******************************************************
38 * geo_distance_internal - distance between points
40 * args:
41 * a pair of points - for each point,
42 * x-coordinate is longitude in degrees west of Greenwich
43 * y-coordinate is latitude in degrees above equator
45 * returns: double
46 * distance between the points in miles on earth's surface
47 ******************************************************/
49 static double
50 geo_distance_internal(Point *pt1, Point *pt2)
52 double long1,
53 lat1,
54 long2,
55 lat2;
56 double longdiff;
57 double sino;
59 /* convert degrees to radians */
61 long1 = degtorad(pt1->x);
62 lat1 = degtorad(pt1->y);
64 long2 = degtorad(pt2->x);
65 lat2 = degtorad(pt2->y);
67 /* compute difference in longitudes - want < 180 degrees */
68 longdiff = fabs(long1 - long2);
69 if (longdiff > M_PI)
70 longdiff = TWO_PI - longdiff;
72 sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
73 cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
74 if (sino > 1.)
75 sino = 1.;
77 return 2. * EARTH_RADIUS * asin(sino);
81 /******************************************************
83 * geo_distance - distance between points
85 * args:
86 * a pair of points - for each point,
87 * x-coordinate is longitude in degrees west of Greenwich
88 * y-coordinate is latitude in degrees above equator
90 * returns: float8
91 * distance between the points in miles on earth's surface
93 * If float8 is passed-by-value, the oldstyle version-0 calling convention
94 * is unportable, so we use version-1. However, if it's passed-by-reference,
95 * continue to use oldstyle. This is just because we'd like earthdistance
96 * to serve as a canary for any unintentional breakage of version-0 functions
97 * with float8 results.
98 ******************************************************/
100 #ifdef USE_FLOAT8_BYVAL
102 Datum geo_distance(PG_FUNCTION_ARGS);
103 PG_FUNCTION_INFO_V1(geo_distance);
105 Datum
106 geo_distance(PG_FUNCTION_ARGS)
108 Point *pt1 = PG_GETARG_POINT_P(0);
109 Point *pt2 = PG_GETARG_POINT_P(1);
110 float8 result;
112 result = geo_distance_internal(pt1, pt2);
113 PG_RETURN_FLOAT8(result);
116 #else /* !USE_FLOAT8_BYVAL */
118 double *geo_distance(Point *pt1, Point *pt2);
120 double *
121 geo_distance(Point *pt1, Point *pt2)
123 double *resultp = palloc(sizeof(double));
125 *resultp = geo_distance_internal(pt1, pt2);
126 return resultp;
129 #endif /* USE_FLOAT8_BYVAL */