PostGIS  2.1.10dev-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(), 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 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
473  GSERIALIZED *geom2 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(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 
484  /* Catch sphere special case and re-jig spheroid appropriately */
485  if ( ! use_spheroid )
486  {
487  sphere->a = sphere->b = sphere->radius;
488  }
489 
490  if ( ! ( type1 == POINTTYPE || type1 == LINETYPE || type1 == POLYGONTYPE ||
491  type1 == MULTIPOINTTYPE || type1 == MULTILINETYPE || type1 == MULTIPOLYGONTYPE ))
492  {
493  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
494  PG_RETURN_NULL();
495  }
496 
497  if ( ! ( type2 == POINTTYPE || type2 == LINETYPE || type2 == POLYGONTYPE ||
498  type2 == MULTIPOINTTYPE || type2 == MULTILINETYPE || type2 == MULTIPOLYGONTYPE ))
499  {
500  elog(ERROR, "geometry_distance_spheroid: Only point/line/polygon supported.\n");
501  PG_RETURN_NULL();
502  }
503 
504 
505  if (gserialized_get_srid(geom1) != gserialized_get_srid(geom2))
506  {
507  elog(ERROR, "geometry_distance_spheroid: Operation on two GEOMETRIES with different SRIDs\n");
508  PG_RETURN_NULL();
509  }
510 
511  /* Get #LWGEOM structures */
512  lwgeom1 = lwgeom_from_gserialized(geom1);
513  lwgeom2 = lwgeom_from_gserialized(geom2);
514 
515  /* We are going to be calculating geodetic distances */
516  lwgeom_set_geodetic(lwgeom1, LW_TRUE);
517  lwgeom_set_geodetic(lwgeom2, LW_TRUE);
518 
519  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, sphere, 0.0);
520 
521  PG_RETURN_FLOAT8(distance);
522 
523 }
#define LINETYPE
Definition: liblwgeom.h:61
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:56
void spheroid_init(SPHEROID *s, double a, double b)
Initialize a spheroid object for use in geodetic functions.
Definition: lwspheroid.c:20
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:62
double b
Definition: liblwgeom.h:270
#define MULTIPOINTTYPE
Definition: liblwgeom.h:63
double radius
Definition: liblwgeom.h:274
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
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:814
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
double a
Definition: liblwgeom.h:269
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
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:2078
#define MULTILINETYPE
Definition: liblwgeom.h:64
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:70

Here is the call graph for this function:

Here is the caller graph for this function: