PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lwgeom_area_sphere()

double lwgeom_area_sphere ( const LWGEOM lwgeom,
const SPHEROID spheroid 
)

Calculate the area of an LWGEOM.

Calculate the geodetic area of a lwgeom on the sphere.

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 calculate an outside point.

Definition at line 2031 of file lwgeodetic.c.

2032 {
2033  int type;
2034  double radius2 = spheroid->radius * spheroid->radius;
2035 
2036  assert(lwgeom);
2037 
2038  /* No area in nothing */
2039  if ( lwgeom_is_empty(lwgeom) )
2040  return 0.0;
2041 
2042  /* Read the geometry type number */
2043  type = lwgeom->type;
2044 
2045  /* Anything but polygons and collections returns zero */
2046  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
2047  return 0.0;
2048 
2049  /* Actually calculate area */
2050  if ( type == POLYGONTYPE )
2051  {
2052  LWPOLY *poly = (LWPOLY*)lwgeom;
2053  uint32_t i;
2054  double area = 0.0;
2055 
2056  /* Just in case there's no rings */
2057  if ( poly->nrings < 1 )
2058  return 0.0;
2059 
2060  /* First, the area of the outer ring */
2061  area += radius2 * ptarray_area_sphere(poly->rings[0]);
2062 
2063  /* Subtract areas of inner rings */
2064  for ( i = 1; i < poly->nrings; i++ )
2065  {
2066  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
2067  }
2068  return area;
2069  }
2070 
2071  /* Recurse into sub-geometries to get area */
2072  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
2073  {
2074  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
2075  uint32_t i;
2076  double area = 0.0;
2077 
2078  for ( i = 0; i < col->ngeoms; i++ )
2079  {
2080  area += lwgeom_area_sphere(col->geoms[i], spheroid);
2081  }
2082  return area;
2083  }
2084 
2085  /* Shouldn't get here. */
2086  return 0.0;
2087 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
#define POLYGONTYPE
Definition: liblwgeom.h:87
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
double ptarray_area_sphere(const POINTARRAY *pa)
Returns the area of the ring (ring must be closed) in square radians (surface of the sphere is 4*PI).
Definition: lwgeodetic.c:1807
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwgeodetic.c:2031
Datum area(PG_FUNCTION_ARGS)
type
Definition: ovdump.py:41
uint32_t ngeoms
Definition: liblwgeom.h:510
LWGEOM ** geoms
Definition: liblwgeom.h:512
uint8_t type
Definition: liblwgeom.h:399
POINTARRAY ** rings
Definition: liblwgeom.h:460
uint32_t nrings
Definition: liblwgeom.h:458
double radius
Definition: liblwgeom.h:321
unsigned int uint32_t
Definition: uthash.h:78

References area(), COLLECTIONTYPE, LWCOLLECTION::geoms, lwgeom_area_sphere(), lwgeom_is_empty(), MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, LWPOLY::nrings, POLYGONTYPE, ptarray_area_sphere(), SPHEROID::radius, LWPOLY::rings, LWGEOM::type, and ovdump::type.

Referenced by geography_area(), geography_centroid_from_mpoly(), lwgeom_area_sphere(), test_lwgeom_area_sphere(), and test_spheroid_area().

Here is the call graph for this function:
Here is the caller graph for this function: