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

Definition at line 467 of file geography_measurement.c.

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

468 {
469  LWGEOM *lwgeom = NULL;
470  GSERIALIZED *g = NULL;
471  double length;
472  bool use_spheroid = LW_TRUE;
473  SPHEROID s;
474  int type;
475 
476  /* Get our geometry object loaded into memory. */
477  g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
478 
479  /* Only return for area features. */
480  type = gserialized_get_type(g);
481  if ( ! (type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE) )
482  {
483  PG_RETURN_FLOAT8(0.0);
484  }
485 
486  lwgeom = lwgeom_from_gserialized(g);
487 
488  /* EMPTY things have no perimeter */
489  if ( lwgeom_is_empty(lwgeom) )
490  {
491  lwgeom_free(lwgeom);
492  PG_RETURN_FLOAT8(0.0);
493  }
494 
495  /* Read our calculation type */
496  use_spheroid = PG_GETARG_BOOL(1);
497 
498  /* Initialize spheroid */
499  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
500 
501  /* User requests spherical calculation, turn our spheroid into a sphere */
502  if ( ! use_spheroid )
503  s.a = s.b = s.radius;
504 
505  /* Calculate the length */
506  length = lwgeom_length_spheroid(lwgeom, &s);
507 
508  /* Something went wrong... */
509  if ( length < 0.0 )
510  {
511  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
512  PG_RETURN_NULL();
513  }
514 
515  /* Clean up, but not all the way to the point arrays */
516  lwgeom_free(lwgeom);
517 
518  PG_FREE_IF_COPY(g, 0);
519  PG_RETURN_FLOAT8(length);
520 }
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
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
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
#define COLLECTIONTYPE
Definition: liblwgeom.h:66

Here is the call graph for this function: