PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ geography_area()

Datum geography_area ( PG_FUNCTION_ARGS  )

Definition at line 502 of file geography_measurement.c.

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

Referenced by geography_expand().

503 {
504  LWGEOM *lwgeom = NULL;
505  GSERIALIZED *g = NULL;
506  GBOX gbox;
507  double area;
508  bool use_spheroid = LW_TRUE;
509  SPHEROID s;
510 
511  /* Get our geometry object loaded into memory. */
512  g = PG_GETARG_GSERIALIZED_P(0);
513 
514  /* Read our calculation type */
515  use_spheroid = PG_GETARG_BOOL(1);
516 
517  /* Initialize spheroid */
518  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
519 
520  lwgeom = lwgeom_from_gserialized(g);
521 
522  /* EMPTY things have no area */
523  if ( lwgeom_is_empty(lwgeom) )
524  {
525  lwgeom_free(lwgeom);
526  PG_RETURN_FLOAT8(0.0);
527  }
528 
529  if ( lwgeom->bbox )
530  gbox = *(lwgeom->bbox);
531  else
532  lwgeom_calculate_gbox_geodetic(lwgeom, &gbox);
533 
534 #if ! PROJ_GEODESIC
535  /* Test for cases that are currently not handled by spheroid code */
536  if ( use_spheroid )
537  {
538  /* We can't circle the poles right now */
539  if ( FP_GTEQ(gbox.zmax,1.0) || FP_LTEQ(gbox.zmin,-1.0) )
540  use_spheroid = LW_FALSE;
541  /* We can't cross the equator right now */
542  if ( gbox.zmax > 0.0 && gbox.zmin < 0.0 )
543  use_spheroid = LW_FALSE;
544  }
545 #endif /* if ! PROJ_GEODESIC */
546 
547  /* User requests spherical calculation, turn our spheroid into a sphere */
548  if ( ! use_spheroid )
549  s.a = s.b = s.radius;
550 
551  /* Calculate the area */
552  if ( use_spheroid )
553  area = lwgeom_area_spheroid(lwgeom, &s);
554  else
555  area = lwgeom_area_sphere(lwgeom, &s);
556 
557  /* Clean up */
558  lwgeom_free(lwgeom);
559  PG_FREE_IF_COPY(g, 0);
560 
561  /* Something went wrong... */
562  if ( area < 0.0 )
563  {
564  elog(ERROR, "lwgeom_area_spher(oid) returned area < 0.0");
565  PG_RETURN_NULL();
566  }
567 
568  PG_RETURN_FLOAT8(area);
569 }
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:2940
GBOX * bbox
Definition: liblwgeom.h:400
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Datum area(PG_FUNCTION_ARGS)
double b
Definition: liblwgeom.h:316
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1128
double radius
Definition: liblwgeom.h:320
#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:172
double zmax
Definition: liblwgeom.h:299
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
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:1944
double a
Definition: liblwgeom.h:315
#define FP_GTEQ(A, B)
double zmin
Definition: liblwgeom.h:298
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the spheroid.
Definition: lwspheroid.c:642
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:99
Here is the call graph for this function:
Here is the caller graph for this function: