PostGIS  2.5.0dev-r@@SVN_REVISION@@
double lwgeom_area_sphere ( const LWGEOM lwgeom,
const SPHEROID spheroid 
)

Calculate the geodetic area of a lwgeom on the sphere.

The result will be multiplied by the average radius of the supplied spheroid.

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 1940 of file lwgeodetic.c.

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

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

1941 {
1942  int type;
1943  double radius2 = spheroid->radius * spheroid->radius;
1944 
1945  assert(lwgeom);
1946 
1947  /* No area in nothing */
1948  if ( lwgeom_is_empty(lwgeom) )
1949  return 0.0;
1950 
1951  /* Read the geometry type number */
1952  type = lwgeom->type;
1953 
1954  /* Anything but polygons and collections returns zero */
1955  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
1956  return 0.0;
1957 
1958  /* Actually calculate area */
1959  if ( type == POLYGONTYPE )
1960  {
1961  LWPOLY *poly = (LWPOLY*)lwgeom;
1962  uint32_t i;
1963  double area = 0.0;
1964 
1965  /* Just in case there's no rings */
1966  if ( poly->nrings < 1 )
1967  return 0.0;
1968 
1969  /* First, the area of the outer ring */
1970  area += radius2 * ptarray_area_sphere(poly->rings[0]);
1971 
1972  /* Subtract areas of inner rings */
1973  for ( i = 1; i < poly->nrings; i++ )
1974  {
1975  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
1976  }
1977  return area;
1978  }
1979 
1980  /* Recurse into sub-geometries to get area */
1981  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
1982  {
1983  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
1984  uint32_t i;
1985  double area = 0.0;
1986 
1987  for ( i = 0; i < col->ngeoms; i++ )
1988  {
1989  area += lwgeom_area_sphere(col->geoms[i], spheroid);
1990  }
1991  return area;
1992  }
1993 
1994  /* Shouldn't get here. */
1995  return 0.0;
1996 }
#define POLYGONTYPE
Definition: liblwgeom.h:86
Datum area(PG_FUNCTION_ARGS)
double radius
Definition: liblwgeom.h:317
uint32_t ngeoms
Definition: liblwgeom.h:506
uint32_t nrings
Definition: liblwgeom.h:454
unsigned int uint32_t
Definition: uthash.h:78
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwgeodetic.c:1940
LWGEOM ** geoms
Definition: liblwgeom.h:508
POINTARRAY ** rings
Definition: liblwgeom.h:456
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
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:1716
uint8_t type
Definition: liblwgeom.h:395
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:1386
#define COLLECTIONTYPE
Definition: liblwgeom.h:90

Here is the call graph for this function:

Here is the caller graph for this function: