PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ geography_length()

Datum geography_length ( PG_FUNCTION_ARGS  )

Definition at line 637 of file geography_measurement.c.

638 {
639  LWGEOM *lwgeom = NULL;
640  GSERIALIZED *g = NULL;
641  double length;
642  bool use_spheroid = LW_TRUE;
643  SPHEROID s;
644 
645  /* Get our geometry object loaded into memory. */
646  g = PG_GETARG_GSERIALIZED_P(0);
647  lwgeom = lwgeom_from_gserialized(g);
648 
649  /* EMPTY things have no length */
650  if ( lwgeom_is_empty(lwgeom) || lwgeom->type == POLYGONTYPE || lwgeom->type == MULTIPOLYGONTYPE )
651  {
652  lwgeom_free(lwgeom);
653  PG_RETURN_FLOAT8(0.0);
654  }
655 
656  /* Read our calculation type */
657  use_spheroid = PG_GETARG_BOOL(1);
658 
659  /* Initialize spheroid */
660  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
661 
662  /* User requests spherical calculation, turn our spheroid into a sphere */
663  if ( ! use_spheroid )
664  s.a = s.b = s.radius;
665 
666  /* Calculate the length */
667  length = lwgeom_length_spheroid(lwgeom, &s);
668 
669  /* Something went wrong... */
670  if ( length < 0.0 )
671  {
672  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
673  PG_RETURN_NULL();
674  }
675 
676  /* Clean up */
677  lwgeom_free(lwgeom);
678 
679  PG_FREE_IF_COPY(g, 0);
680  PG_RETURN_FLOAT8(length);
681 }
char * s
Definition: cu_in_wkt.c:23
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
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
#define POLYGONTYPE
Definition: liblwgeom.h:87
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:1393
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:3297
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
uint8_t type
Definition: liblwgeom.h:399

References gserialized_get_srid(), LW_TRUE, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), lwgeom_length_spheroid(), MULTIPOLYGONTYPE, POLYGONTYPE, s, and LWGEOM::type.

Here is the call graph for this function: