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

Definition at line 184 of file geography_measurement.c.

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

185 {
186  GSERIALIZED *g1 = NULL;
187  GSERIALIZED *g2 = NULL;
188  double tolerance;
189  double distance;
190  bool use_spheroid;
191  SPHEROID s;
192  int dwithin = LW_FALSE;
193 
194  /* Get our geometry objects loaded into memory. */
195  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
196  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
197 
198  /* Read our tolerance value. */
199  tolerance = PG_GETARG_FLOAT8(2);
200 
201  /* Read our calculation type. */
202  use_spheroid = PG_GETARG_BOOL(3);
203 
204  /* Initialize spheroid */
205  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
206 
207  /* Set to sphere if requested */
208  if ( ! use_spheroid )
209  s.a = s.b = s.radius;
210 
211  /* Return FALSE on empty arguments. */
213  {
214  PG_FREE_IF_COPY(g1, 0);
215  PG_FREE_IF_COPY(g2, 1);
216  PG_RETURN_BOOL(FALSE);
217  }
218 
219  /* Do the brute force calculation if the cached calculation doesn't tick over */
220  if ( LW_FAILURE == geography_dwithin_cache(fcinfo, g1, g2, &s, tolerance, &dwithin) )
221  {
222  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
223  LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
224  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
225  /* Something went wrong... */
226  if ( distance < 0.0 )
227  elog(ERROR, "lwgeom_distance_spheroid returned negative!");
228  dwithin = (distance <= tolerance);
229  lwgeom_free(lwgeom1);
230  lwgeom_free(lwgeom2);
231  }
232 
233  /* Clean up */
234  PG_FREE_IF_COPY(g1, 0);
235  PG_FREE_IF_COPY(g2, 1);
236 
237  PG_RETURN_BOOL(dwithin);
238 }
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
#define LW_FALSE
Definition: liblwgeom.h:52
char * s
Definition: cu_in_wkt.c:24
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:269
int geography_dwithin_cache(FunctionCallInfoData *fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, int *dwithin)
#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
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: