PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ geography_length()

Datum geography_length ( PG_FUNCTION_ARGS  )

Definition at line 633 of file geography_measurement.c.

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

Referenced by geography_perimeter().

634 {
635  LWGEOM *lwgeom = NULL;
636  GSERIALIZED *g = NULL;
637  double length;
638  bool use_spheroid = LW_TRUE;
639  SPHEROID s;
640 
641  /* Get our geometry object loaded into memory. */
642  g = PG_GETARG_GSERIALIZED_P(0);
643  lwgeom = lwgeom_from_gserialized(g);
644 
645  /* EMPTY things have no length */
646  if ( lwgeom_is_empty(lwgeom) || lwgeom->type == POLYGONTYPE || lwgeom->type == MULTIPOLYGONTYPE )
647  {
648  lwgeom_free(lwgeom);
649  PG_RETURN_FLOAT8(0.0);
650  }
651 
652  /* Read our calculation type */
653  use_spheroid = PG_GETARG_BOOL(1);
654 
655  /* Initialize spheroid */
656  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
657 
658  /* User requests spherical calculation, turn our spheroid into a sphere */
659  if ( ! use_spheroid )
660  s.a = s.b = s.radius;
661 
662  /* Calculate the length */
663  length = lwgeom_length_spheroid(lwgeom, &s);
664 
665  /* Something went wrong... */
666  if ( length < 0.0 )
667  {
668  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
669  PG_RETURN_NULL();
670  }
671 
672  /* Clean up */
673  lwgeom_free(lwgeom);
674 
675  PG_FREE_IF_COPY(g, 0);
676  PG_RETURN_FLOAT8(length);
677 }
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
uint8_t type
Definition: liblwgeom.h:395
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
Here is the call graph for this function:
Here is the caller graph for this function: