PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geometry_distance_spheroid()

Datum geometry_distance_spheroid ( PG_FUNCTION_ARGS  )

Definition at line 483 of file lwgeom_spheroid.c.

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

Referenced by distance_sphere_method(), LWGEOM_distance_ellipsoid(), and LWGEOM_distance_sphere().

484 {
485  GSERIALIZED *geom1 = PG_GETARG_GSERIALIZED_P(0);
486  GSERIALIZED *geom2 = PG_GETARG_GSERIALIZED_P(1);
487  SPHEROID *sphere = (SPHEROID *)PG_GETARG_POINTER(2);
488  int type1 = gserialized_get_type(geom1);
489  int type2 = gserialized_get_type(geom2);
490  bool use_spheroid = PG_GETARG_BOOL(3);
491  LWGEOM *lwgeom1, *lwgeom2;
492  double distance;
493 
494  /* Calculate some other parameters on the spheroid */
495  spheroid_init(sphere, sphere->a, sphere->b);
496 
498 
499  /* Catch sphere special case and re-jig spheroid appropriately */
500  if ( ! use_spheroid )
501  {
502  sphere->a = sphere->b = sphere->radius;
503  }
504 
505  if ( ! ( type1 == POINTTYPE || type1 == LINETYPE || type1 == POLYGONTYPE ||
506  type1 == MULTIPOINTTYPE || type1 == MULTILINETYPE || type1 == MULTIPOLYGONTYPE ))
507  {
508  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
509  PG_RETURN_NULL();
510  }
511 
512  if ( ! ( type2 == POINTTYPE || type2 == LINETYPE || type2 == POLYGONTYPE ||
513  type2 == MULTIPOINTTYPE || type2 == MULTILINETYPE || type2 == MULTIPOLYGONTYPE ))
514  {
515  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
516  PG_RETURN_NULL();
517  }
518 
519  /* Get #LWGEOM structures */
520  lwgeom1 = lwgeom_from_gserialized(geom1);
521  lwgeom2 = lwgeom_from_gserialized(geom2);
522 
523  /* We are going to be calculating geodetic distances */
524  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
525  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
526 
527  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, sphere, 0.0);
528 
529  PG_RETURN_FLOAT8(distance);
530 
531 }
#define LINETYPE
Definition: liblwgeom.h:86
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
void spheroid_init(SPHEROID *s, double a, double b)
Initialize a spheroid object for use in geodetic functions.
Definition: lwspheroid.c:39
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:87
double b
Definition: liblwgeom.h:314
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
double radius
Definition: liblwgeom.h:318
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:371
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
Datum distance(PG_FUNCTION_ARGS)
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:907
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
double a
Definition: liblwgeom.h:313
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
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
#define MULTILINETYPE
Definition: liblwgeom.h:89
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: