PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwgeom_area_sphere()

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

2038 {
2039  int type;
2040  double radius2 = spheroid->radius * spheroid->radius;
2041 
2042  assert(lwgeom);
2043 
2044  /* No area in nothing */
2045  if ( lwgeom_is_empty(lwgeom) )
2046  return 0.0;
2047 
2048  /* Read the geometry type number */
2049  type = lwgeom->type;
2050 
2051  /* Anything but polygons and collections returns zero */
2052  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
2053  return 0.0;
2054 
2055  /* Actually calculate area */
2056  if ( type == POLYGONTYPE )
2057  {
2058  LWPOLY *poly = (LWPOLY*)lwgeom;
2059  uint32_t i;
2060  double area = 0.0;
2061 
2062  /* Just in case there's no rings */
2063  if ( poly->nrings < 1 )
2064  return 0.0;
2065 
2066  /* First, the area of the outer ring */
2067  area += radius2 * ptarray_area_sphere(poly->rings[0]);
2068 
2069  /* Subtract areas of inner rings */
2070  for ( i = 1; i < poly->nrings; i++ )
2071  {
2072  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
2073  }
2074  return area;
2075  }
2076 
2077  /* Recurse into sub-geometries to get area */
2078  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
2079  {
2080  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
2081  uint32_t i;
2082  double area = 0.0;
2083 
2084  for ( i = 0; i < col->ngeoms; i++ )
2085  {
2086  area += lwgeom_area_sphere(col->geoms[i], spheroid);
2087  }
2088  return area;
2089  }
2090 
2091  /* Shouldn't get here. */
2092  return 0.0;
2093 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
#define POLYGONTYPE
Definition: liblwgeom.h:104
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:1813
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwgeodetic.c:2037
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:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
uint8_t type
Definition: liblwgeom.h:462
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
double radius
Definition: liblwgeom.h:380

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: