2001-02-10 05:31:31 +03:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1998-06-16 07:55:15 +04:00
|
|
|
#include <math.h>
|
|
|
|
|
2001-02-10 05:31:31 +03:00
|
|
|
#include "utils/geo_decls.h" /* for Pt */
|
|
|
|
|
1998-06-16 07:55:15 +04:00
|
|
|
|
|
|
|
/* Earth's radius is in statute miles. */
|
2003-08-04 04:43:34 +04:00
|
|
|
const double EARTH_RADIUS = 3958.747716;
|
|
|
|
const double TWO_PI = 2.0 * M_PI;
|
2000-06-15 22:55:34 +04:00
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
double *geo_distance(Point *pt1, Point *pt2);
|
2000-06-15 22:55:34 +04:00
|
|
|
|
1998-06-16 07:55:15 +04:00
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
*
|
|
|
|
* degtorad - convert degrees to radians
|
|
|
|
*
|
1999-05-25 20:15:34 +04:00
|
|
|
* arg: double, angle in degrees
|
1998-06-16 07:55:15 +04:00
|
|
|
*
|
1999-05-25 20:15:34 +04:00
|
|
|
* returns: double, same angle in radians
|
1998-06-16 07:55:15 +04:00
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static double
|
1999-05-25 20:15:34 +04:00
|
|
|
degtorad(double degrees)
|
|
|
|
{
|
1998-06-16 07:55:15 +04:00
|
|
|
return (degrees / 360.0) * TWO_PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
*
|
|
|
|
* geo_distance - distance between points
|
|
|
|
*
|
|
|
|
* args:
|
1999-05-25 20:15:34 +04: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 07:55:15 +04:00
|
|
|
*
|
1999-05-25 20:15:34 +04:00
|
|
|
* returns: double
|
|
|
|
* distance between the points in miles on earth's surface
|
1998-06-16 07:55:15 +04:00
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
double *
|
1999-05-25 20:15:34 +04:00
|
|
|
geo_distance(Point *pt1, Point *pt2)
|
|
|
|
{
|
1998-06-16 07:55:15 +04:00
|
|
|
|
1999-05-25 20:15:34 +04:00
|
|
|
double long1,
|
|
|
|
lat1,
|
|
|
|
long2,
|
|
|
|
lat2;
|
|
|
|
double longdiff;
|
2002-11-08 23:20:22 +03:00
|
|
|
double sino;
|
1999-05-25 20:15:34 +04:00
|
|
|
double *resultp = palloc(sizeof(double));
|
1998-06-16 07:55:15 +04:00
|
|
|
|
|
|
|
/* convert degrees to radians */
|
|
|
|
|
1999-05-25 20:15:34 +04:00
|
|
|
long1 = degtorad(pt1->x);
|
|
|
|
lat1 = degtorad(pt1->y);
|
1998-06-16 07:55:15 +04:00
|
|
|
|
1999-05-25 20:15:34 +04:00
|
|
|
long2 = degtorad(pt2->x);
|
|
|
|
lat2 = degtorad(pt2->y);
|
1998-06-16 07:55:15 +04:00
|
|
|
|
|
|
|
/* compute difference in longitudes - want < 180 degrees */
|
1999-05-25 20:15:34 +04:00
|
|
|
longdiff = fabs(long1 - long2);
|
1998-06-16 07:55:15 +04:00
|
|
|
if (longdiff > M_PI)
|
|
|
|
longdiff = TWO_PI - longdiff;
|
|
|
|
|
2003-08-04 04:43:34 +04:00
|
|
|
sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
|
2005-10-15 06:49:52 +04:00
|
|
|
cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
|
2003-08-04 04:43:34 +04:00
|
|
|
if (sino > 1.)
|
|
|
|
sino = 1.;
|
|
|
|
*resultp = 2. * EARTH_RADIUS * asin(sino);
|
1998-06-16 07:55:15 +04:00
|
|
|
|
|
|
|
return resultp;
|
|
|
|
}
|