PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_perimeter()

Datum geography_perimeter ( PG_FUNCTION_ARGS  )

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

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