PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_distance()

Datum geography_distance ( PG_FUNCTION_ARGS  )

Definition at line 205 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().

206 {
207  GSERIALIZED* g1 = NULL;
208  GSERIALIZED* g2 = NULL;
209  double distance;
210  double tolerance = 0.0;
211  bool use_spheroid = true;
212  SPHEROID s;
213 
214  /* Get our geometry objects loaded into memory. */
215  g1 = PG_GETARG_GSERIALIZED_P(0);
216  g2 = PG_GETARG_GSERIALIZED_P(1);
217 
218  /* Read our tolerance value. */
219  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
220  tolerance = PG_GETARG_FLOAT8(2);
221 
222  /* Read our calculation type. */
223  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
224  use_spheroid = PG_GETARG_BOOL(3);
225 
227 
228  /* Initialize spheroid */
229  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
230 
231  /* Set to sphere if requested */
232  if ( ! use_spheroid )
233  s.a = s.b = s.radius;
234 
235  /* Return NULL on empty arguments. */
237  {
238  PG_FREE_IF_COPY(g1, 0);
239  PG_FREE_IF_COPY(g2, 1);
240  PG_RETURN_NULL();
241  }
242 
243  /* Do the brute force calculation if the cached calculation doesn't tick over */
244  if ( LW_FAILURE == geography_distance_cache(fcinfo, g1, g2, &s, &distance) )
245  {
246  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
247  LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
248  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
249  lwgeom_free(lwgeom1);
250  lwgeom_free(lwgeom2);
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 }
int geography_distance_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double b
Definition: liblwgeom.h:314
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
#define INVMINDIST
double radius
Definition: liblwgeom.h:318
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:371
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:179
#define LW_FAILURE
Definition: liblwgeom.h:79
char * s
Definition: cu_in_wkt.c:23
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:313
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:2183
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
Here is the call graph for this function:
Here is the caller graph for this function: