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

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

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

Macro Definition 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 30 of file earthdistance.c.

References TWO_PI.

Referenced by geo_distance_internal().

31 {
32  return (degrees / 360.0) * TWO_PI;
33 }
Datum degrees(PG_FUNCTION_ARGS)
Definition: float.c:2374
static const double TWO_PI
Definition: earthdistance.c:17
Datum geo_distance ( PG_FUNCTION_ARGS  )

Definition at line 96 of file earthdistance.c.

References geo_distance_internal(), PG_GETARG_POINT_P, PG_RETURN_FLOAT8, and result.

97 {
98  Point *pt1 = PG_GETARG_POINT_P(0);
99  Point *pt2 = PG_GETARG_POINT_P(1);
100  float8 result;
101 
102  result = geo_distance_internal(pt1, pt2);
103  PG_RETURN_FLOAT8(result);
104 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
return result
Definition: formatting.c:1633
double float8
Definition: c.h:375
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:139
static double geo_distance_internal(Point *pt1, Point *pt2)
Definition: earthdistance.c:49
static double geo_distance_internal ( Point pt1,
Point pt2 
)
static

Definition at line 49 of file earthdistance.c.

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

Referenced by geo_distance().

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

Variable Documentation

const double EARTH_RADIUS = 3958.747716
static

Definition at line 16 of file earthdistance.c.

Referenced by geo_distance_internal().

PG_MODULE_MAGIC

Definition at line 13 of file earthdistance.c.

const double TWO_PI = 2.0 * M_PI
static

Definition at line 17 of file earthdistance.c.

Referenced by degtorad(), and geo_distance_internal().