PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ geometry_distance_spheroid()

Datum geometry_distance_spheroid ( PG_FUNCTION_ARGS  )

Definition at line 482 of file lwgeom_spheroid.c.

483 {
484  GSERIALIZED *geom1 = PG_GETARG_GSERIALIZED_P(0);
485  GSERIALIZED *geom2 = PG_GETARG_GSERIALIZED_P(1);
486  SPHEROID *sphere = (SPHEROID *)PG_GETARG_POINTER(2);
487  int type1 = gserialized_get_type(geom1);
488  int type2 = gserialized_get_type(geom2);
489  bool use_spheroid = PG_GETARG_BOOL(3);
490  LWGEOM *lwgeom1, *lwgeom2;
491  double distance;
492  gserialized_error_if_srid_mismatch(geom1, geom2, __func__);
493 
494  /* Calculate some other parameters on the spheroid */
495  spheroid_init(sphere, sphere->a, sphere->b);
496 
497  /* Catch sphere special case and re-jig spheroid appropriately */
498  if ( ! use_spheroid )
499  {
500  sphere->a = sphere->b = sphere->radius;
501  }
502 
503  if ( ! ( type1 == POINTTYPE || type1 == LINETYPE || type1 == POLYGONTYPE ||
504  type1 == MULTIPOINTTYPE || type1 == MULTILINETYPE || type1 == MULTIPOLYGONTYPE ))
505  {
506  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
507  PG_RETURN_NULL();
508  }
509 
510  if ( ! ( type2 == POINTTYPE || type2 == LINETYPE || type2 == POLYGONTYPE ||
511  type2 == MULTIPOINTTYPE || type2 == MULTILINETYPE || type2 == MULTIPOLYGONTYPE ))
512  {
513  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
514  PG_RETURN_NULL();
515  }
516 
517  /* Get #LWGEOM structures */
518  lwgeom1 = lwgeom_from_gserialized(geom1);
519  lwgeom2 = lwgeom_from_gserialized(geom2);
520 
521  /* We are going to be calculating geodetic distances */
522  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
523  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
524 
525  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, sphere, 0.0);
526 
527  PG_RETURN_FLOAT8(distance);
528 
529 }
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
Definition: gserialized.c:404
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:946
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:2187
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define LINETYPE
Definition: liblwgeom.h:117
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
#define POLYGONTYPE
Definition: liblwgeom.h:118
void spheroid_init(SPHEROID *s, double a, double b)
Initialize a spheroid object for use in geodetic functions.
Definition: lwspheroid.c:39
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032
double radius
Definition: liblwgeom.h:366
double a
Definition: liblwgeom.h:361
double b
Definition: liblwgeom.h:362

References SPHEROID::a, SPHEROID::b, distance(), gserialized_error_if_srid_mismatch(), gserialized_get_type(), LINETYPE, LW_TRUE, lwgeom_distance_spheroid(), lwgeom_from_gserialized(), lwgeom_set_geodetic(), MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, POINTTYPE, POLYGONTYPE, SPHEROID::radius, and spheroid_init().

Referenced by LWGEOM_distance_ellipsoid(), and LWGEOM_distance_sphere().

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