PostGIS  2.4.9dev-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 2027 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, ovdump::type, and LWGEOM::type.

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

2028 {
2029  int type;
2030  double radius2 = spheroid->radius * spheroid->radius;
2031 
2032  assert(lwgeom);
2033 
2034  /* No area in nothing */
2035  if ( lwgeom_is_empty(lwgeom) )
2036  return 0.0;
2037 
2038  /* Read the geometry type number */
2039  type = lwgeom->type;
2040 
2041  /* Anything but polygons and collections returns zero */
2042  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
2043  return 0.0;
2044 
2045  /* Actually calculate area */
2046  if ( type == POLYGONTYPE )
2047  {
2048  LWPOLY *poly = (LWPOLY*)lwgeom;
2049  int i;
2050  double area = 0.0;
2051 
2052  /* Just in case there's no rings */
2053  if ( poly->nrings < 1 )
2054  return 0.0;
2055 
2056  /* First, the area of the outer ring */
2057  area += radius2 * ptarray_area_sphere(poly->rings[0]);
2058 
2059  /* Subtract areas of inner rings */
2060  for ( i = 1; i < poly->nrings; i++ )
2061  {
2062  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
2063  }
2064  return area;
2065  }
2066 
2067  /* Recurse into sub-geometries to get area */
2068  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
2069  {
2070  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
2071  int i;
2072  double area = 0.0;
2073 
2074  for ( i = 0; i < col->ngeoms; i++ )
2075  {
2076  area += lwgeom_area_sphere(col->geoms[i], spheroid);
2077  }
2078  return area;
2079  }
2080 
2081  /* Shouldn't get here. */
2082  return 0.0;
2083 }
#define POLYGONTYPE
Definition: liblwgeom.h:87
Datum area(PG_FUNCTION_ARGS)
double radius
Definition: liblwgeom.h:318
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwgeodetic.c:2027
LWGEOM ** geoms
Definition: liblwgeom.h:509
POINTARRAY ** rings
Definition: liblwgeom.h:457
int nrings
Definition: liblwgeom.h:455
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
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:1803
uint8_t type
Definition: liblwgeom.h:396
type
Definition: ovdump.py:41
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:1346
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
Here is the call graph for this function:
Here is the caller graph for this function: