PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum geography_length ( PG_FUNCTION_ARGS  )

Definition at line 619 of file geography_measurement.c.

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

620 {
621  LWGEOM *lwgeom = NULL;
622  GSERIALIZED *g = NULL;
623  double length;
624  bool use_spheroid = LW_TRUE;
625  SPHEROID s;
626 
627  /* Get our geometry object loaded into memory. */
628  g = PG_GETARG_GSERIALIZED_P(0);
629  lwgeom = lwgeom_from_gserialized(g);
630 
631  /* EMPTY things have no length */
632  if ( lwgeom_is_empty(lwgeom) || lwgeom->type == POLYGONTYPE || lwgeom->type == MULTIPOLYGONTYPE )
633  {
634  lwgeom_free(lwgeom);
635  PG_RETURN_FLOAT8(0.0);
636  }
637 
638  /* Read our calculation type */
639  use_spheroid = PG_GETARG_BOOL(1);
640 
641  /* Initialize spheroid */
642  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
643 
644  /* User requests spherical calculation, turn our spheroid into a sphere */
645  if ( ! use_spheroid )
646  s.a = s.b = s.radius;
647 
648  /* Calculate the length */
649  length = lwgeom_length_spheroid(lwgeom, &s);
650 
651  /* Something went wrong... */
652  if ( length < 0.0 )
653  {
654  elog(ERROR, "lwgeom_length_spheroid returned length < 0.0");
655  PG_RETURN_NULL();
656  }
657 
658  /* Clean up */
659  lwgeom_free(lwgeom);
660 
661  PG_FREE_IF_COPY(g, 0);
662  PG_RETURN_FLOAT8(length);
663 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:72
double b
Definition: liblwgeom.h:298
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
double radius
Definition: liblwgeom.h:302
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:2884
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
char * s
Definition: cu_in_wkt.c:23
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
double a
Definition: liblwgeom.h:297
uint8_t type
Definition: liblwgeom.h:380
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:1297
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:69

Here is the call graph for this function: