PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geography_perimeter()

Datum geography_perimeter ( PG_FUNCTION_ARGS  )

Definition at line 559 of file geography_measurement.c.

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

Referenced by geography_area().

560 {
561  LWGEOM *lwgeom = NULL;
562  GSERIALIZED *g = NULL;
563  double length;
564  bool use_spheroid = LW_TRUE;
565  SPHEROID s;
566  int type;
567 
568  /* Get our geometry object loaded into memory. */
569  g = PG_GETARG_GSERIALIZED_P(0);
570 
571  /* Only return for area features. */
572  type = gserialized_get_type(g);
573  if ( ! (type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE) )
574  {
575  PG_RETURN_FLOAT8(0.0);
576  }
577 
578  lwgeom = lwgeom_from_gserialized(g);
579 
580  /* EMPTY things have no perimeter */
581  if ( lwgeom_is_empty(lwgeom) )
582  {
583  lwgeom_free(lwgeom);
584  PG_RETURN_FLOAT8(0.0);
585  }
586 
587  /* Read our calculation type */
588  use_spheroid = PG_GETARG_BOOL(1);
589 
590  /* Initialize spheroid */
591  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
592 
593  /* User requests spherical calculation, turn our spheroid into a sphere */
594  if ( ! use_spheroid )
595  s.a = s.b = s.radius;
596 
597  /* Calculate the length */
598  length = lwgeom_length_spheroid(lwgeom, &s);
599 
600  /* Something went wrong... */
601  if ( length < 0.0 )
602  {
603  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
604  PG_RETURN_NULL();
605  }
606 
607  /* Clean up, but not all the way to the point arrays */
608  lwgeom_free(lwgeom);
609 
610  PG_FREE_IF_COPY(g, 0);
611  PG_RETURN_FLOAT8(length);
612 }
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
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
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
double radius
Definition: liblwgeom.h:302
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:2884
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
char * s
Definition: cu_in_wkt.c:23
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
double a
Definition: liblwgeom.h:297
type
Definition: ovdump.py:41
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:1297
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
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
Here is the call graph for this function:
Here is the caller graph for this function: