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

Definition at line 395 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.

396 {
397  LWGEOM *lwgeom = NULL;
398  GSERIALIZED *g = NULL;
399  GBOX gbox;
400  double area;
401  bool use_spheroid = LW_TRUE;
402  SPHEROID s;
403 
404  /* Get our geometry object loaded into memory. */
405  g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
406 
407  /* Read our calculation type */
408  use_spheroid = PG_GETARG_BOOL(1);
409 
410  /* Initialize spheroid */
411  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
412 
413  lwgeom = lwgeom_from_gserialized(g);
414 
415  /* EMPTY things have no area */
416  if ( lwgeom_is_empty(lwgeom) )
417  {
418  lwgeom_free(lwgeom);
419  PG_RETURN_FLOAT8(0.0);
420  }
421 
422  if ( lwgeom->bbox )
423  gbox = *(lwgeom->bbox);
424  else
425  lwgeom_calculate_gbox_geodetic(lwgeom, &gbox);
426 
427  /* Test for cases that are currently not handled by spheroid code */
428  if ( use_spheroid )
429  {
430  /* We can't circle the poles right now */
431  if ( FP_GTEQ(gbox.zmax,1.0) || FP_LTEQ(gbox.zmin,-1.0) )
432  use_spheroid = LW_FALSE;
433  /* We can't cross the equator right now */
434  if ( gbox.zmax > 0.0 && gbox.zmin < 0.0 )
435  use_spheroid = LW_FALSE;
436  }
437 
438  /* User requests spherical calculation, turn our spheroid into a sphere */
439  if ( ! use_spheroid )
440  s.a = s.b = s.radius;
441 
442  /* Calculate the area */
443  if ( use_spheroid )
444  area = lwgeom_area_spheroid(lwgeom, &s);
445  else
446  area = lwgeom_area_sphere(lwgeom, &s);
447 
448  /* Clean up */
449  lwgeom_free(lwgeom);
450  PG_FREE_IF_COPY(g, 0);
451 
452  /* Something went wrong... */
453  if ( area < 0.0 )
454  {
455  elog(ERROR, "lwgeom_area_spher(oid) returned area < 0.0");
456  PG_RETURN_NULL();
457  }
458 
459  PG_RETURN_FLOAT8(area);
460 }
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:2612
GBOX * bbox
Definition: liblwgeom.h:354
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Datum area(PG_FUNCTION_ARGS)
double b
Definition: liblwgeom.h:270
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
double radius
Definition: liblwgeom.h:274
#define FP_LTEQ(A, B)
double zmax
Definition: liblwgeom.h:253
#define LW_FALSE
Definition: liblwgeom.h:52
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
char * s
Definition: cu_in_wkt.c:24
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the sphere.
Definition: lwgeodetic.c:1924
double a
Definition: liblwgeom.h:269
#define FP_GTEQ(A, B)
double zmin
Definition: liblwgeom.h:252
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the spheroid.
Definition: lwspheroid.c:513
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:1229
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:70

Here is the call graph for this function: