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

Definition at line 117 of file geography_measurement.c.

References SPHEROID::a, SPHEROID::b, distance(), geography_distance_cache(), gserialized_get_srid(), gserialized_is_empty(), LW_FAILURE, lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), SPHEROID::radius, and s.

118 {
119  GSERIALIZED* g1 = NULL;
120  GSERIALIZED* g2 = NULL;
121  double distance;
122  double tolerance;
123  bool use_spheroid;
124  SPHEROID s;
125 
126  /* Get our geometry objects loaded into memory. */
127  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
128  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
129 
130  /* Read our tolerance value. */
131  tolerance = PG_GETARG_FLOAT8(2);
132 
133  /* Read our calculation type. */
134  use_spheroid = PG_GETARG_BOOL(3);
135 
136  /* Initialize spheroid */
137  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
138 
139  /* Set to sphere if requested */
140  if ( ! use_spheroid )
141  s.a = s.b = s.radius;
142 
143  /* Return NULL on empty arguments. */
145  {
146  PG_FREE_IF_COPY(g1, 0);
147  PG_FREE_IF_COPY(g2, 1);
148  PG_RETURN_NULL();
149  }
150 
151  /* Do the brute force calculation if the cached calculation doesn't tick over */
152  if ( LW_FAILURE == geography_distance_cache(fcinfo, g1, g2, &s, &distance) )
153  {
154  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
155  LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
156  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
157  lwgeom_free(lwgeom1);
158  lwgeom_free(lwgeom2);
159  }
160 
161  /* Clean up */
162  PG_FREE_IF_COPY(g1, 0);
163  PG_FREE_IF_COPY(g2, 1);
164 
165  /* Something went wrong, negative return... should already be eloged, return NULL */
166  if ( distance < 0.0 )
167  {
168  elog(ERROR, "distance returned negative!");
169  PG_RETURN_NULL();
170  }
171 
172  /* Knock off any funny business at the micrometer level, ticket #2168 */
173  distance = round(distance * 10e8) / 10e8;
174 
175  PG_RETURN_FLOAT8(distance);
176 }
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
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:140
#define LW_FAILURE
Definition: liblwgeom.h:54
char * s
Definition: cu_in_wkt.c:24
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:269
int geography_distance_cache(FunctionCallInfoData *fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
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
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: