PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum geography_length ( PG_FUNCTION_ARGS  )

Definition at line 527 of file geography_measurement.c.

References SPHEROID::a, SPHEROID::b, gserialized_get_srid(), LW_TRUE, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), lwgeom_length_spheroid(), MULTIPOLYGONTYPE, POLYGONTYPE, SPHEROID::radius, s, and LWGEOM::type.

528 {
529  LWGEOM *lwgeom = NULL;
530  GSERIALIZED *g = NULL;
531  double length;
532  bool use_spheroid = LW_TRUE;
533  SPHEROID s;
534 
535  /* Get our geometry object loaded into memory. */
536  g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
537  lwgeom = lwgeom_from_gserialized(g);
538 
539  /* EMPTY things have no length */
540  if ( lwgeom_is_empty(lwgeom) || lwgeom->type == POLYGONTYPE || lwgeom->type == MULTIPOLYGONTYPE )
541  {
542  lwgeom_free(lwgeom);
543  PG_RETURN_FLOAT8(0.0);
544  }
545 
546  /* Read our calculation type */
547  use_spheroid = PG_GETARG_BOOL(1);
548 
549  /* Initialize spheroid */
550  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
551 
552  /* User requests spherical calculation, turn our spheroid into a sphere */
553  if ( ! use_spheroid )
554  s.a = s.b = s.radius;
555 
556  /* Calculate the length */
557  length = lwgeom_length_spheroid(lwgeom, &s);
558 
559  /* Something went wrong... */
560  if ( length < 0.0 )
561  {
562  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
563  PG_RETURN_NULL();
564  }
565 
566  /* Clean up */
567  lwgeom_free(lwgeom);
568 
569  PG_FREE_IF_COPY(g, 0);
570  PG_RETURN_FLOAT8(length);
571 }
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
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
double radius
Definition: liblwgeom.h:274
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:2881
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
char * s
Definition: cu_in_wkt.c:24
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
double a
Definition: liblwgeom.h:269
uint8_t type
Definition: liblwgeom.h:352
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1229
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: