PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geography_area()

Datum geography_area ( PG_FUNCTION_ARGS  )

Definition at line 492 of file geography_measurement.c.

493 {
494  LWGEOM *lwgeom = NULL;
495  GSERIALIZED *g = NULL;
496  GBOX gbox;
497  double area;
498  bool use_spheroid = LW_TRUE;
499  SPHEROID s;
500 
501  /* Get our geometry object loaded into memory. */
502  g = PG_GETARG_GSERIALIZED_P(0);
503 
504  /* Read our calculation type */
505  use_spheroid = PG_GETARG_BOOL(1);
506 
507  /* Initialize spheroid */
508  spheroid_init_from_srid(gserialized_get_srid(g), &s);
509 
510  lwgeom = lwgeom_from_gserialized(g);
511 
512  /* EMPTY things have no area */
513  if ( lwgeom_is_empty(lwgeom) )
514  {
515  lwgeom_free(lwgeom);
516  PG_RETURN_FLOAT8(0.0);
517  }
518 
519  if ( lwgeom->bbox )
520  gbox = *(lwgeom->bbox);
521  else
522  lwgeom_calculate_gbox_geodetic(lwgeom, &gbox);
523 
524 #ifndef PROJ_GEODESIC
525  /* Test for cases that are currently not handled by spheroid code */
526  if ( use_spheroid )
527  {
528  /* We can't circle the poles right now */
529  if ( FP_GTEQ(gbox.zmax,1.0) || FP_LTEQ(gbox.zmin,-1.0) )
530  use_spheroid = LW_FALSE;
531  /* We can't cross the equator right now */
532  if ( gbox.zmax > 0.0 && gbox.zmin < 0.0 )
533  use_spheroid = LW_FALSE;
534  }
535 #endif /* ifndef PROJ_GEODESIC */
536 
537  /* User requests spherical calculation, turn our spheroid into a sphere */
538  if ( ! use_spheroid )
539  s.a = s.b = s.radius;
540 
541  /* Calculate the area */
542  if ( use_spheroid )
543  area = lwgeom_area_spheroid(lwgeom, &s);
544  else
545  area = lwgeom_area_sphere(lwgeom, &s);
546 
547  /* Clean up */
548  lwgeom_free(lwgeom);
549  PG_FREE_IF_COPY(g, 0);
550 
551  /* Something went wrong... */
552  if ( area < 0.0 )
553  {
554  elog(ERROR, "lwgeom_area_spher(oid) returned area < 0.0");
555  PG_RETURN_NULL();
556  }
557 
558  PG_RETURN_FLOAT8(area);
559 }
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
#define LW_FALSE
Definition: liblwgeom.h:94
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:3027
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the spheroid.
Definition: lwspheroid.c:647
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the sphere.
Definition: lwgeodetic.c:2037
#define FP_GTEQ(A, B)
#define FP_LTEQ(A, B)
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
double zmax
Definition: liblwgeom.h:359
double zmin
Definition: liblwgeom.h:358
GBOX * bbox
Definition: liblwgeom.h:458

References LWGEOM::bbox, FP_GTEQ, FP_LTEQ, gserialized_get_srid(), LW_FALSE, LW_TRUE, lwgeom_area_sphere(), lwgeom_area_spheroid(), lwgeom_calculate_gbox_geodetic(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), s, GBOX::zmax, and GBOX::zmin.

Here is the call graph for this function: