PostGIS  2.1.10dev-r@@SVN_REVISION@@
double lwgeom_area_spheroid ( const LWGEOM lwgeom,
const SPHEROID spheroid 
)

Calculate the geodetic area of a lwgeom on the spheroid.

The result will have the squared units of the spheroid axes.

Calculate the geodetic area of a lwgeom on the spheroid.

Anything except POLYGON, MULTIPOLYGON and GEOMETRYCOLLECTION return zero immediately. Multi's recurse, polygons calculate external ring area and subtract internal ring area. A GBOX is required to check relationship to equator an outside point. WARNING: Does NOT WORK for polygons over equator or pole.

Definition at line 513 of file lwspheroid.c.

References area(), COLLECTIONTYPE, LWCOLLECTION::geoms, lwgeom_is_empty(), MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, LWPOLY::nrings, POLYGONTYPE, ptarray_area_spheroid(), LWPOLY::rings, and LWGEOM::type.

Referenced by geography_area(), and test_spheroid_area().

514 {
515  int type;
516 
517  assert(lwgeom);
518 
519  /* No area in nothing */
520  if ( lwgeom_is_empty(lwgeom) )
521  return 0.0;
522 
523  /* Read the geometry type number */
524  type = lwgeom->type;
525 
526  /* Anything but polygons and collections returns zero */
527  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
528  return 0.0;
529 
530  /* Actually calculate area */
531  if ( type == POLYGONTYPE )
532  {
533  LWPOLY *poly = (LWPOLY*)lwgeom;
534  int i;
535  double area = 0.0;
536 
537  /* Just in case there's no rings */
538  if ( poly->nrings < 1 )
539  return 0.0;
540 
541  /* First, the area of the outer ring */
542  area += ptarray_area_spheroid(poly->rings[0], spheroid);
543 
544  /* Subtract areas of inner rings */
545  for ( i = 1; i < poly->nrings; i++ )
546  {
547  area -= ptarray_area_spheroid(poly->rings[i], spheroid);
548  }
549  return area;
550  }
551 
552  /* Recurse into sub-geometries to get area */
553  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
554  {
555  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
556  int i;
557  double area = 0.0;
558 
559  for ( i = 0; i < col->ngeoms; i++ )
560  {
561  area += lwgeom_area_spheroid(col->geoms[i], spheroid);
562  }
563  return area;
564  }
565 
566  /* Shouldn't get here. */
567  return 0.0;
568 }
#define POLYGONTYPE
Definition: liblwgeom.h:62
Datum area(PG_FUNCTION_ARGS)
LWGEOM ** geoms
Definition: liblwgeom.h:465
POINTARRAY ** rings
Definition: liblwgeom.h:413
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwspheroid.c:513
int nrings
Definition: liblwgeom.h:411
static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
Definition: lwspheroid.c:367
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
uint8_t type
Definition: liblwgeom.h:352
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
#define COLLECTIONTYPE
Definition: liblwgeom.h:66

Here is the call graph for this function:

Here is the caller graph for this function: