postgresql/contrib/earthdistance/earthdistance.c

130 lines
3.0 KiB
C
Raw Normal View History

/* $PostgreSQL: pgsql/contrib/earthdistance/earthdistance.c,v 1.15 2008/04/21 01:11:43 tgl Exp $ */
#include "postgres.h"
1998-06-16 11:55:15 +08:00
#include <math.h>
#include "utils/geo_decls.h" /* for Point */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
1998-06-16 11:55:15 +08:00
PG_MODULE_MAGIC;
1998-06-16 11:55:15 +08:00
/* Earth's radius is in statute miles. */
static const double EARTH_RADIUS = 3958.747716;
static const double TWO_PI = 2.0 * M_PI;
2000-06-16 02:55:34 +08:00
1998-06-16 11:55:15 +08:00
/******************************************************
*
* degtorad - convert degrees to radians
*
1999-05-26 00:15:34 +08:00
* arg: double, angle in degrees
1998-06-16 11:55:15 +08:00
*
1999-05-26 00:15:34 +08:00
* returns: double, same angle in radians
1998-06-16 11:55:15 +08:00
******************************************************/
static double
1999-05-26 00:15:34 +08:00
degtorad(double degrees)
{
1998-06-16 11:55:15 +08:00
return (degrees / 360.0) * TWO_PI;
}
/******************************************************
*
* geo_distance_internal - distance between points
1998-06-16 11:55:15 +08:00
*
* args:
1999-05-26 00:15:34 +08:00
* a pair of points - for each point,
* x-coordinate is longitude in degrees west of Greenwich
* y-coordinate is latitude in degrees above equator
1998-06-16 11:55:15 +08:00
*
* returns: double
1999-05-26 00:15:34 +08:00
* distance between the points in miles on earth's surface
1998-06-16 11:55:15 +08:00
******************************************************/
static double
geo_distance_internal(Point *pt1, Point *pt2)
1999-05-26 00:15:34 +08:00
{
double long1,
lat1,
long2,
lat2;
double longdiff;
double sino;
1998-06-16 11:55:15 +08:00
/* convert degrees to radians */
1999-05-26 00:15:34 +08:00
long1 = degtorad(pt1->x);
lat1 = degtorad(pt1->y);
1998-06-16 11:55:15 +08:00
1999-05-26 00:15:34 +08:00
long2 = degtorad(pt2->x);
lat2 = degtorad(pt2->y);
1998-06-16 11:55:15 +08:00
/* compute difference in longitudes - want < 180 degrees */
1999-05-26 00:15:34 +08:00
longdiff = fabs(long1 - long2);
1998-06-16 11:55:15 +08:00
if (longdiff > M_PI)
longdiff = TWO_PI - longdiff;
2003-08-04 08:43:34 +08:00
sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
2005-10-15 10:49:52 +08:00
cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
2003-08-04 08:43:34 +08:00
if (sino > 1.)
sino = 1.;
1998-06-16 11:55:15 +08:00
return 2. * EARTH_RADIUS * asin(sino);
}
/******************************************************
*
* geo_distance - distance between points
*
* args:
* a pair of points - for each point,
* x-coordinate is longitude in degrees west of Greenwich
* y-coordinate is latitude in degrees above equator
*
* returns: float8
* distance between the points in miles on earth's surface
*
* If float8 is passed-by-value, the oldstyle version-0 calling convention
* is unportable, so we use version-1. However, if it's passed-by-reference,
* continue to use oldstyle. This is just because we'd like earthdistance
* to serve as a canary for any unintentional breakage of version-0 functions
* with float8 results.
******************************************************/
#ifdef USE_FLOAT8_BYVAL
Datum geo_distance(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(geo_distance);
Datum
geo_distance(PG_FUNCTION_ARGS)
{
Point *pt1 = PG_GETARG_POINT_P(0);
Point *pt2 = PG_GETARG_POINT_P(1);
float8 result;
result = geo_distance_internal(pt1, pt2);
PG_RETURN_FLOAT8(result);
1998-06-16 11:55:15 +08:00
}
#else /* !USE_FLOAT8_BYVAL */
double *geo_distance(Point *pt1, Point *pt2);
double *
geo_distance(Point *pt1, Point *pt2)
{
double *resultp = palloc(sizeof(double));
*resultp = geo_distance_internal(pt1, pt2);
return resultp;
}
#endif /* USE_FLOAT8_BYVAL */