PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ geography_dwithin()

Datum geography_dwithin ( PG_FUNCTION_ARGS  )

Definition at line 268 of file geography_measurement.c.

269 {
270  SHARED_GSERIALIZED *shared_geom1 = ToastCacheGetGeometry(fcinfo, 0);
271  SHARED_GSERIALIZED *shared_geom2 = ToastCacheGetGeometry(fcinfo, 1);
272  const GSERIALIZED *g1 = shared_gserialized_get(shared_geom1);
273  const GSERIALIZED *g2 = shared_gserialized_get(shared_geom2);
274  SPHEROID s;
275  double tolerance = 0.0;
276  bool use_spheroid = true;
277  double distance;
278  int dwithin = LW_FALSE;
279 
280  gserialized_error_if_srid_mismatch(g1, g2, __func__);
281 
282  /* Read our tolerance value. */
283  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
284  tolerance = PG_GETARG_FLOAT8(2);
285 
286  /* Read our calculation type. */
287  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
288  use_spheroid = PG_GETARG_BOOL(3);
289 
290  /* Initialize spheroid */
291  spheroid_init_from_srid(gserialized_get_srid(g1), &s);
292 
293  /* Set to sphere if requested */
294  if ( ! use_spheroid )
295  s.a = s.b = s.radius;
296 
297  /* Return FALSE on empty arguments. */
299  PG_RETURN_BOOL(false);
300 
301  /* Do the brute force calculation if the cached calculation doesn't tick over */
302  if (LW_FAILURE == geography_dwithin_cache(fcinfo, shared_geom1, shared_geom2, &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  PG_RETURN_BOOL(dwithin);
316 }
char * s
Definition: cu_in_wkt.c:23
int geography_dwithin_cache(FunctionCallInfo fcinfo, SHARED_GSERIALIZED *g1, SHARED_GSERIALIZED *g2, const SPHEROID *s, double tolerance, int *dwithin)
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
Definition: gserialized.c:404
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
#define LW_FALSE
Definition: liblwgeom.h:108
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:2190
#define LW_FAILURE
Definition: liblwgeom.h:110
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032

References distance(), geography_dwithin_cache(), gserialized_error_if_srid_mismatch(), gserialized_get_srid(), gserialized_is_empty(), LW_FAILURE, LW_FALSE, lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), and s.

Referenced by geography_intersects().

Here is the call graph for this function:
Here is the caller graph for this function: