PostGIS  2.5.2dev-r@@SVN_REVISION@@

◆ geography_area()

Datum geography_area ( PG_FUNCTION_ARGS  )

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

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