PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ geography_length()

Datum geography_length ( PG_FUNCTION_ARGS  )

Definition at line 635 of file geography_measurement.c.

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

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: