PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geography_dwithin()

Datum geography_dwithin ( PG_FUNCTION_ARGS  )

Definition at line 262 of file geography_measurement.c.

References SPHEROID::a, SPHEROID::b, distance(), error_if_srid_mismatch(), FALSE, geography_distance_tree(), geography_dwithin_cache(), gserialized_get_srid(), gserialized_is_empty(), LW_FAILURE, LW_FALSE, lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), PG_FUNCTION_INFO_V1(), SPHEROID::radius, and s.

Referenced by geography_distance().

263 {
264  GSERIALIZED *g1 = NULL;
265  GSERIALIZED *g2 = NULL;
266  double tolerance = 0.0;
267  double distance;
268  bool use_spheroid = true;
269  SPHEROID s;
270  int dwithin = LW_FALSE;
271 
272  /* Get our geometry objects loaded into memory. */
273  g1 = PG_GETARG_GSERIALIZED_P(0);
274  g2 = PG_GETARG_GSERIALIZED_P(1);
275 
276  /* Read our tolerance value. */
277  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
278  tolerance = PG_GETARG_FLOAT8(2);
279 
280  /* Read our calculation type. */
281  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
282  use_spheroid = PG_GETARG_BOOL(3);
283 
285 
286  /* Initialize spheroid */
287  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
288 
289  /* Set to sphere if requested */
290  if ( ! use_spheroid )
291  s.a = s.b = s.radius;
292 
293  /* Return FALSE on empty arguments. */
295  {
296  PG_FREE_IF_COPY(g1, 0);
297  PG_FREE_IF_COPY(g2, 1);
298  PG_RETURN_BOOL(FALSE);
299  }
300 
301  /* Do the brute force calculation if the cached calculation doesn't tick over */
302  if ( LW_FAILURE == geography_dwithin_cache(fcinfo, g1, g2, &s, tolerance, &dwithin) )
303  {
304  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
305  LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
306  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
307  /* Something went wrong... */
308  if ( distance < 0.0 )
309  elog(ERROR, "lwgeom_distance_spheroid returned negative!");
310  dwithin = (distance <= tolerance);
311  lwgeom_free(lwgeom1);
312  lwgeom_free(lwgeom2);
313  }
314 
315  /* Clean up */
316  PG_FREE_IF_COPY(g1, 0);
317  PG_FREE_IF_COPY(g2, 1);
318 
319  PG_RETURN_BOOL(dwithin);
320 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double b
Definition: liblwgeom.h:298
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
double radius
Definition: liblwgeom.h:302
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:139
#define LW_FAILURE
Definition: liblwgeom.h:64
#define LW_FALSE
Definition: liblwgeom.h:62
char * s
Definition: cu_in_wkt.c:23
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:297
int geography_dwithin_cache(FunctionCallInfoData *fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, int *dwithin)
#define FALSE
Definition: dbfopen.c:168
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:2081
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:69
Here is the call graph for this function:
Here is the caller graph for this function: