PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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.

Macros

#define M_PI   3.14159265358979323846
 

Functions

 PG_MODULE_MAGIC_EXT (.name="earthdistance",.version=PG_VERSION)
 
static double degtorad (double degrees)
 
static double geo_distance_internal (Point *pt1, Point *pt2)
 
 PG_FUNCTION_INFO_V1 (geo_distance)
 
Datum geo_distance (PG_FUNCTION_ARGS)
 

Variables

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

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 11 of file earthdistance.c.

Function Documentation

◆ degtorad()

static double degtorad ( double  degrees)
static

Definition at line 34 of file earthdistance.c.

35{
36 return (degrees / 360.0) * TWO_PI;
37}
static const double TWO_PI
Definition: earthdistance.c:21
Datum degrees(PG_FUNCTION_ARGS)
Definition: float.c:2561

References degrees(), and TWO_PI.

Referenced by geo_distance_internal().

◆ geo_distance()

Datum geo_distance ( PG_FUNCTION_ARGS  )

Definition at line 100 of file earthdistance.c.

101{
102 Point *pt1 = PG_GETARG_POINT_P(0);
103 Point *pt2 = PG_GETARG_POINT_P(1);
104 float8 result;
105
106 result = geo_distance_internal(pt1, pt2);
107 PG_RETURN_FLOAT8(result);
108}
double float8
Definition: c.h:601
static double geo_distance_internal(Point *pt1, Point *pt2)
Definition: earthdistance.c:53
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:185

References geo_distance_internal(), PG_GETARG_POINT_P, and PG_RETURN_FLOAT8.

◆ geo_distance_internal()

static double geo_distance_internal ( Point pt1,
Point pt2 
)
static

Definition at line 53 of file earthdistance.c.

54{
55 double long1,
56 lat1,
57 long2,
58 lat2;
59 double longdiff;
60 double sino;
61
62 /* convert degrees to radians */
63
64 long1 = degtorad(pt1->x);
65 lat1 = degtorad(pt1->y);
66
67 long2 = degtorad(pt2->x);
68 lat2 = degtorad(pt2->y);
69
70 /* compute difference in longitudes - want < 180 degrees */
71 longdiff = fabs(long1 - long2);
72 if (longdiff > M_PI)
73 longdiff = TWO_PI - longdiff;
74
75 sino = sqrt(sin(fabs(lat1 - lat2) / 2.) * sin(fabs(lat1 - lat2) / 2.) +
76 cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
77 if (sino > 1.)
78 sino = 1.;
79
80 return 2. * EARTH_RADIUS * asin(sino);
81}
static double degtorad(double degrees)
Definition: earthdistance.c:34
static const double EARTH_RADIUS
Definition: earthdistance.c:20
#define M_PI
Definition: earthdistance.c:11
float8 y
Definition: geo_decls.h:99
float8 x
Definition: geo_decls.h:98

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

Referenced by geo_distance().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( geo_distance  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "earthdistance",
version = PG_VERSION 
)

Variable Documentation

◆ EARTH_RADIUS

const double EARTH_RADIUS = 3958.747716
static

Definition at line 20 of file earthdistance.c.

Referenced by geo_distance_internal().

◆ TWO_PI

const double TWO_PI = 2.0 * M_PI
static

Definition at line 21 of file earthdistance.c.

Referenced by degtorad(), and geo_distance_internal().