PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ geography_area()

Datum geography_area ( PG_FUNCTION_ARGS  )

Definition at line 493 of file geography_measurement.c.

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

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: