PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ geography_distance()

Datum geography_distance ( PG_FUNCTION_ARGS  )

Definition at line 205 of file geography_measurement.c.

206 {
207  GSERIALIZED* g1 = NULL;
208  GSERIALIZED* g2 = NULL;
209  double distance;
210  bool use_spheroid = true;
211  SPHEROID s;
212 
213  /* Get our geometry objects loaded into memory. */
214  g1 = PG_GETARG_GSERIALIZED_P(0);
215  g2 = PG_GETARG_GSERIALIZED_P(1);
216 
217  /* Read our calculation type. */
218  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
219  use_spheroid = PG_GETARG_BOOL(3);
220 
222 
223  /* Initialize spheroid */
224  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
225 
226  /* Set to sphere if requested */
227  if ( ! use_spheroid )
228  s.a = s.b = s.radius;
229 
230  /* Return NULL on empty arguments. */
232  {
233  PG_FREE_IF_COPY(g1, 0);
234  PG_FREE_IF_COPY(g2, 1);
235  PG_RETURN_NULL();
236  }
237 
238  /* Do the brute force calculation if the cached calculation doesn't tick over */
239  if ( LW_FAILURE == geography_distance_cache(fcinfo, g1, g2, &s, &distance) )
240  {
241  /* default to using tree-based distance calculation at all times */
242  /* in standard distance call. */
244  /*
245  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
246  LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
247  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
248  lwgeom_free(lwgeom1);
249  lwgeom_free(lwgeom2);
250  */
251  }
252 
253  /* Clean up */
254  PG_FREE_IF_COPY(g1, 0);
255  PG_FREE_IF_COPY(g2, 1);
256 
257  /* Knock off any funny business at the nanometer level, ticket #2168 */
258  distance = round(distance * INVMINDIST) / INVMINDIST;
259 
260  /* Something went wrong, negative return... should already be eloged, return NULL */
261  if ( distance < 0.0 )
262  {
263  elog(ERROR, "distance returned negative!");
264  PG_RETURN_NULL();
265  }
266 
267  PG_RETURN_FLOAT8(distance);
268 }
char * s
Definition: cu_in_wkt.c:23
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:100
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:179
#define INVMINDIST
int geography_tree_distance(const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, double *distance)
int geography_distance_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
#define LW_FAILURE
Definition: liblwgeom.h:79
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:338
#define FP_TOLERANCE
Floating point comparators.
Datum distance(PG_FUNCTION_ARGS)

References distance(), error_if_srid_mismatch(), FP_TOLERANCE, geography_distance_cache(), geography_tree_distance(), gserialized_get_srid(), gserialized_is_empty(), INVMINDIST, LW_FAILURE, and s.

Here is the call graph for this function: