PostGIS  3.2.2dev-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 2033 of file lwgeodetic.c.

2034 {
2035  int type;
2036  double radius2 = spheroid->radius * spheroid->radius;
2037 
2038  assert(lwgeom);
2039 
2040  /* No area in nothing */
2041  if ( lwgeom_is_empty(lwgeom) )
2042  return 0.0;
2043 
2044  /* Read the geometry type number */
2045  type = lwgeom->type;
2046 
2047  /* Anything but polygons and collections returns zero */
2048  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
2049  return 0.0;
2050 
2051  /* Actually calculate area */
2052  if ( type == POLYGONTYPE )
2053  {
2054  LWPOLY *poly = (LWPOLY*)lwgeom;
2055  uint32_t i;
2056  double area = 0.0;
2057 
2058  /* Just in case there's no rings */
2059  if ( poly->nrings < 1 )
2060  return 0.0;
2061 
2062  /* First, the area of the outer ring */
2063  area += radius2 * ptarray_area_sphere(poly->rings[0]);
2064 
2065  /* Subtract areas of inner rings */
2066  for ( i = 1; i < poly->nrings; i++ )
2067  {
2068  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
2069  }
2070  return area;
2071  }
2072 
2073  /* Recurse into sub-geometries to get area */
2074  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
2075  {
2076  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
2077  uint32_t i;
2078  double area = 0.0;
2079 
2080  for ( i = 0; i < col->ngeoms; i++ )
2081  {
2082  area += lwgeom_area_sphere(col->geoms[i], spheroid);
2083  }
2084  return area;
2085  }
2086 
2087  /* Shouldn't get here. */
2088  return 0.0;
2089 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
#define POLYGONTYPE
Definition: liblwgeom.h:118
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:1809
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwgeodetic.c:2033
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
type
Definition: ovdump.py:42
uint32_t ngeoms
Definition: liblwgeom.h:594
LWGEOM ** geoms
Definition: liblwgeom.h:589
uint8_t type
Definition: liblwgeom.h:476
POINTARRAY ** rings
Definition: liblwgeom.h:533
uint32_t nrings
Definition: liblwgeom.h:538
double radius
Definition: liblwgeom.h:394

References 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: