Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions | Variables

earthdistance.c File Reference

#include "postgres.h"
#include <math.h>
#include "utils/geo_decls.h"
Include dependency graph for earthdistance.c:

Go to the source code of this file.

Defines

#define M_PI   3.14159265358979323846

Functions

static double degtorad (double degrees)
static double geo_distance_internal (Point *pt1, Point *pt2)
double * geo_distance (Point *pt1, Point *pt2)

Variables

 PG_MODULE_MAGIC
static const double EARTH_RADIUS = 3958.747716
static const double TWO_PI = 2.0 * M_PI

Define Documentation

#define M_PI   3.14159265358979323846

Definition at line 10 of file earthdistance.c.

Referenced by geo_distance_internal().


Function Documentation

static double degtorad ( double  degrees  )  [static]

Definition at line 31 of file earthdistance.c.

References TWO_PI.

Referenced by geo_distance_internal().

{
    return (degrees / 360.0) * TWO_PI;
}

double * geo_distance ( Point pt1,
Point pt2 
)

Definition at line 121 of file earthdistance.c.

References geo_distance_internal(), and palloc().

{
    double     *resultp = palloc(sizeof(double));

    *resultp = geo_distance_internal(pt1, pt2);
    return resultp;
}

static double geo_distance_internal ( Point pt1,
Point pt2 
) [static]

Definition at line 50 of file earthdistance.c.

References degtorad(), EARTH_RADIUS, M_PI, TWO_PI, Point::x, and Point::y.

Referenced by geo_distance().

{
    double      long1,
                lat1,
                long2,
                lat2;
    double      longdiff;
    double      sino;

    /* convert degrees to radians */

    long1 = degtorad(pt1->x);
    lat1 = degtorad(pt1->y);

    long2 = degtorad(pt2->x);
    lat2 = degtorad(pt2->y);

    /* compute difference in longitudes - want < 180 degrees */
    longdiff = fabs(long1 - long2);
    if (longdiff > M_PI)
        longdiff = TWO_PI - longdiff;

    sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
            cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
    if (sino > 1.)
        sino = 1.;

    return 2. * EARTH_RADIUS * asin(sino);
}


Variable Documentation

const double EARTH_RADIUS = 3958.747716 [static]

Definition at line 17 of file earthdistance.c.

Referenced by geo_distance_internal().

Definition at line 14 of file earthdistance.c.

const double TWO_PI = 2.0 * M_PI [static]

Definition at line 18 of file earthdistance.c.

Referenced by degtorad(), and geo_distance_internal().