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

Definition at line 485 of file geography_measurement.c.

References SPHEROID::a, area(), SPHEROID::b, 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(), SPHEROID::radius, s, GBOX::zmax, and GBOX::zmin.

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