PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geography_length()

Datum geography_length ( PG_FUNCTION_ARGS  )

Definition at line 626 of file geography_measurement.c.

627 {
628  LWGEOM *lwgeom = NULL;
629  GSERIALIZED *g = NULL;
630  double length;
631  bool use_spheroid = LW_TRUE;
632  SPHEROID s;
633 
634  /* Get our geometry object loaded into memory. */
635  g = PG_GETARG_GSERIALIZED_P(0);
636  lwgeom = lwgeom_from_gserialized(g);
637 
638  /* EMPTY things have no length */
639  if ( lwgeom_is_empty(lwgeom) || lwgeom->type == POLYGONTYPE || lwgeom->type == MULTIPOLYGONTYPE )
640  {
641  lwgeom_free(lwgeom);
642  PG_RETURN_FLOAT8(0.0);
643  }
644 
645  /* Read our calculation type */
646  use_spheroid = PG_GETARG_BOOL(1);
647 
648  /* Initialize spheroid */
649  spheroid_init_from_srid(gserialized_get_srid(g), &s);
650 
651  /* User requests spherical calculation, turn our spheroid into a sphere */
652  if ( ! use_spheroid )
653  s.a = s.b = s.radius;
654 
655  /* Calculate the length */
656  length = lwgeom_length_spheroid(lwgeom, &s);
657 
658  /* Something went wrong... */
659  if ( length < 0.0 )
660  {
661  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
662  PG_RETURN_NULL();
663  }
664 
665  /* Clean up */
666  lwgeom_free(lwgeom);
667 
668  PG_FREE_IF_COPY(g, 0);
669  PG_RETURN_FLOAT8(length);
670 }
char * s
Definition: cu_in_wkt.c:23
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
#define POLYGONTYPE
Definition: liblwgeom.h:104
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:3296
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
uint8_t type
Definition: liblwgeom.h:462

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: