PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum geometry_distance_spheroid ( PG_FUNCTION_ARGS  )

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

471 {
472  GSERIALIZED *geom1 = PG_GETARG_GSERIALIZED_P(0);
473  GSERIALIZED *geom2 = PG_GETARG_GSERIALIZED_P(1);
474  SPHEROID *sphere = (SPHEROID *)PG_GETARG_POINTER(2);
475  int type1 = gserialized_get_type(geom1);
476  int type2 = gserialized_get_type(geom2);
477  bool use_spheroid = PG_GETARG_BOOL(3);
478  LWGEOM *lwgeom1, *lwgeom2;
479  double distance;
480 
481  /* Calculate some other parameters on the spheroid */
482  spheroid_init(sphere, sphere->a, sphere->b);
483 
485 
486  /* Catch sphere special case and re-jig spheroid appropriately */
487  if ( ! use_spheroid )
488  {
489  sphere->a = sphere->b = sphere->radius;
490  }
491 
492  if ( ! ( type1 == POINTTYPE || type1 == LINETYPE || type1 == POLYGONTYPE ||
493  type1 == MULTIPOINTTYPE || type1 == MULTILINETYPE || type1 == MULTIPOLYGONTYPE ))
494  {
495  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
496  PG_RETURN_NULL();
497  }
498 
499  if ( ! ( type2 == POINTTYPE || type2 == LINETYPE || type2 == POLYGONTYPE ||
500  type2 == MULTIPOINTTYPE || type2 == MULTILINETYPE || type2 == MULTIPOLYGONTYPE ))
501  {
502  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
503  PG_RETURN_NULL();
504  }
505 
506  /* Get #LWGEOM structures */
507  lwgeom1 = lwgeom_from_gserialized(geom1);
508  lwgeom2 = lwgeom_from_gserialized(geom2);
509 
510  /* We are going to be calculating geodetic distances */
511  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
512  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
513 
514  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, sphere, 0.0);
515 
516  PG_RETURN_FLOAT8(distance);
517 
518 }
#define LINETYPE
Definition: liblwgeom.h:71
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:55
void spheroid_init(SPHEROID *s, double a, double b)
Initialize a spheroid object for use in geodetic functions.
Definition: lwspheroid.c:25
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:72
double b
Definition: liblwgeom.h:298
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
double radius
Definition: liblwgeom.h:302
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
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:858
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
double a
Definition: liblwgeom.h:297
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
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
#define MULTILINETYPE
Definition: liblwgeom.h:74
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: