PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ geography_area()

Datum geography_area ( PG_FUNCTION_ARGS  )

Definition at line 499 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().

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