PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geography_distance()

Datum geography_distance ( PG_FUNCTION_ARGS  )

Definition at line 191 of file geography_measurement.c.

References SPHEROID::a, SPHEROID::b, distance(), error_if_srid_mismatch(), geography_distance_cache(), geography_dwithin(), gserialized_get_srid(), gserialized_is_empty(), INVMINDIST, LW_FAILURE, lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), PG_FUNCTION_INFO_V1(), SPHEROID::radius, and s.

Referenced by geography_distance_uncached().

192 {
193  GSERIALIZED* g1 = NULL;
194  GSERIALIZED* g2 = NULL;
195  double distance;
196  double tolerance = 0.0;
197  bool use_spheroid = true;
198  SPHEROID s;
199 
200  /* Get our geometry objects loaded into memory. */
201  g1 = PG_GETARG_GSERIALIZED_P(0);
202  g2 = PG_GETARG_GSERIALIZED_P(1);
203 
204  /* Read our tolerance value. */
205  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
206  tolerance = PG_GETARG_FLOAT8(2);
207 
208  /* Read our calculation type. */
209  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
210  use_spheroid = PG_GETARG_BOOL(3);
211 
213 
214  /* Initialize spheroid */
215  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
216 
217  /* Set to sphere if requested */
218  if ( ! use_spheroid )
219  s.a = s.b = s.radius;
220 
221  /* Return NULL on empty arguments. */
223  {
224  PG_FREE_IF_COPY(g1, 0);
225  PG_FREE_IF_COPY(g2, 1);
226  PG_RETURN_NULL();
227  }
228 
229  /* Do the brute force calculation if the cached calculation doesn't tick over */
230  if ( LW_FAILURE == geography_distance_cache(fcinfo, g1, g2, &s, &distance) )
231  {
232  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
233  LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
234  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
235  lwgeom_free(lwgeom1);
236  lwgeom_free(lwgeom2);
237  }
238 
239  /* Clean up */
240  PG_FREE_IF_COPY(g1, 0);
241  PG_FREE_IF_COPY(g2, 1);
242 
243  /* Knock off any funny business at the nanometer level, ticket #2168 */
244  distance = round(distance * INVMINDIST) / INVMINDIST;
245 
246  /* Something went wrong, negative return... should already be eloged, return NULL */
247  if ( distance < 0.0 )
248  {
249  elog(ERROR, "distance returned negative!");
250  PG_RETURN_NULL();
251  }
252 
253  PG_RETURN_FLOAT8(distance);
254 }
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
#define INVMINDIST
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
char * s
Definition: cu_in_wkt.c:23
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:297
int geography_distance_cache(FunctionCallInfoData *fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
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: