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

Definition at line 296 of file geography_measurement.c.

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

297 {
298  LWGEOM *lwgeom1 = NULL;
299  LWGEOM *lwgeom2 = NULL;
300  GSERIALIZED *g1 = NULL;
301  GSERIALIZED *g2 = NULL;
302  double tolerance;
303  double distance;
304  bool use_spheroid;
305  SPHEROID s;
306 
307  /* Get our geometry objects loaded into memory. */
308  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
309  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
310 
311  /* Read our tolerance value. */
312  tolerance = PG_GETARG_FLOAT8(2);
313 
314  /* Read our calculation type. */
315  use_spheroid = PG_GETARG_BOOL(3);
316 
317  /* Initialize spheroid */
318  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
319 
320  /* Set to sphere if requested */
321  if ( ! use_spheroid )
322  s.a = s.b = s.radius;
323 
324  lwgeom1 = lwgeom_from_gserialized(g1);
325  lwgeom2 = lwgeom_from_gserialized(g2);
326 
327  /* Return FALSE on empty arguments. */
328  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
329  {
330  PG_RETURN_BOOL(FALSE);
331  }
332 
333  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
334 
335  /* Clean up */
336  lwgeom_free(lwgeom1);
337  lwgeom_free(lwgeom2);
338  PG_FREE_IF_COPY(g1, 0);
339  PG_FREE_IF_COPY(g2, 1);
340 
341  /* Something went wrong... should already be eloged, return FALSE */
342  if ( distance < 0.0 )
343  {
344  elog(ERROR, "lwgeom_distance_spheroid returned negative!");
345  PG_RETURN_BOOL(FALSE);
346  }
347 
348  PG_RETURN_BOOL(distance <= tolerance);
349 }
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
char * s
Definition: cu_in_wkt.c:24
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:269
#define FALSE
Definition: dbfopen.c:169
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: