PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ geography_perimeter()

Datum geography_perimeter ( PG_FUNCTION_ARGS  )

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

574 {
575  LWGEOM *lwgeom = NULL;
576  GSERIALIZED *g = NULL;
577  double length;
578  bool use_spheroid = LW_TRUE;
579  SPHEROID s;
580  int type;
581 
582  /* Get our geometry object loaded into memory. */
583  g = PG_GETARG_GSERIALIZED_P(0);
584 
585  /* Only return for area features. */
586  type = gserialized_get_type(g);
587  if ( ! (type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE) )
588  {
589  PG_RETURN_FLOAT8(0.0);
590  }
591 
592  lwgeom = lwgeom_from_gserialized(g);
593 
594  /* EMPTY things have no perimeter */
595  if ( lwgeom_is_empty(lwgeom) )
596  {
597  lwgeom_free(lwgeom);
598  PG_RETURN_FLOAT8(0.0);
599  }
600 
601  /* Read our calculation type */
602  use_spheroid = PG_GETARG_BOOL(1);
603 
604  /* Initialize spheroid */
605  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
606 
607  /* User requests spherical calculation, turn our spheroid into a sphere */
608  if ( ! use_spheroid )
609  s.a = s.b = s.radius;
610 
611  /* Calculate the length */
612  length = lwgeom_length_spheroid(lwgeom, &s);
613 
614  /* Something went wrong... */
615  if ( length < 0.0 )
616  {
617  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
618  PG_RETURN_NULL();
619  }
620 
621  /* Clean up, but not all the way to the point arrays */
622  lwgeom_free(lwgeom);
623 
624  PG_FREE_IF_COPY(g, 0);
625  PG_RETURN_FLOAT8(length);
626 }
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:69
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:86
double b
Definition: liblwgeom.h:313
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
double radius
Definition: liblwgeom.h:317
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:2895
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
char * s
Definition: cu_in_wkt.c:23
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
double a
Definition: liblwgeom.h:312
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:1310
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:83
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
Here is the call graph for this function:
Here is the caller graph for this function: