PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum geography_distance_uncached ( PG_FUNCTION_ARGS  )

Definition at line 51 of file geography_measurement.c.

References SPHEROID::a, SPHEROID::b, distance(), FP_TOLERANCE, gserialized_get_srid(), lwgeom_add_bbox_deep(), lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), SPHEROID::radius, and s.

52 {
53  LWGEOM *lwgeom1 = NULL;
54  LWGEOM *lwgeom2 = NULL;
55  GSERIALIZED *g1 = NULL;
56  GSERIALIZED *g2 = NULL;
57  double distance;
58  /* double tolerance; */
59  bool use_spheroid;
60  SPHEROID s;
61 
62  /* Get our geometry objects loaded into memory. */
63  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
64  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
65 
66  /* Read our tolerance value. */
67  /* tolerance = PG_GETARG_FLOAT8(2); */
68 
69  /* Read our calculation type. */
70  use_spheroid = PG_GETARG_BOOL(3);
71 
72  /* Initialize spheroid */
73  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
74 
75  /* Set to sphere if requested */
76  if ( ! use_spheroid )
77  s.a = s.b = s.radius;
78 
79  lwgeom1 = lwgeom_from_gserialized(g1);
80  lwgeom2 = lwgeom_from_gserialized(g2);
81 
82  /* Return NULL on empty arguments. */
83  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
84  {
85  PG_FREE_IF_COPY(g1, 0);
86  PG_FREE_IF_COPY(g2, 1);
87  PG_RETURN_NULL();
88  }
89 
90  /* Make sure we have boxes attached */
91  lwgeom_add_bbox_deep(lwgeom1, NULL);
92  lwgeom_add_bbox_deep(lwgeom2, NULL);
93 
94  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, FP_TOLERANCE);
95 
96  /* Clean up */
97  lwgeom_free(lwgeom1);
98  lwgeom_free(lwgeom2);
99  PG_FREE_IF_COPY(g1, 0);
100  PG_FREE_IF_COPY(g2, 1);
101 
102  /* Something went wrong, negative return... should already be eloged, return NULL */
103  if ( distance < 0.0 )
104  {
105  PG_RETURN_NULL();
106  }
107 
108  PG_RETURN_FLOAT8(distance);
109 }
void lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox)
Compute a box for geom and all sub-geometries, if not already computed.
Definition: lwgeom.c:567
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double b
Definition: liblwgeom.h:270
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
double radius
Definition: liblwgeom.h:274
#define FP_TOLERANCE
Floating point comparitors.
char * s
Definition: cu_in_wkt.c:24
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:269
double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
Calculate the geodetic distance from lwgeom1 to lwgeom2 on the spheroid.
Definition: lwgeodetic.c:2078
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1229
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:70

Here is the call graph for this function: