PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lwgeom_area_spheroid()

double lwgeom_area_spheroid ( const LWGEOM lwgeom,
const SPHEROID spheroid 
)

Calculate the area of an LWGEOM.

Calculate the geodetic area of a lwgeom on the spheroid.

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 check relationship to equator an outside point. WARNING: Does NOT WORK for polygons over equator or pole.

Definition at line 647 of file lwspheroid.c.

648 {
649  int type;
650 
651  assert(lwgeom);
652 
653  /* No area in nothing */
654  if ( lwgeom_is_empty(lwgeom) )
655  return 0.0;
656 
657  /* Read the geometry type number */
658  type = lwgeom->type;
659 
660  /* Anything but polygons and collections returns zero */
661  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
662  return 0.0;
663 
664  /* Actually calculate area */
665  if ( type == POLYGONTYPE )
666  {
667  LWPOLY *poly = (LWPOLY*)lwgeom;
668  uint32_t i;
669  double area = 0.0;
670 
671  /* Just in case there's no rings */
672  if ( poly->nrings < 1 )
673  return 0.0;
674 
675  /* First, the area of the outer ring */
676  area += ptarray_area_spheroid(poly->rings[0], spheroid);
677 
678  /* Subtract areas of inner rings */
679  for ( i = 1; i < poly->nrings; i++ )
680  {
681  area -= ptarray_area_spheroid(poly->rings[i], spheroid);
682  }
683  return area;
684  }
685 
686  /* Recurse into sub-geometries to get area */
687  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
688  {
689  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
690  uint32_t i;
691  double area = 0.0;
692 
693  for ( i = 0; i < col->ngeoms; i++ )
694  {
695  area += lwgeom_area_spheroid(col->geoms[i], spheroid);
696  }
697  return area;
698  }
699 
700  /* Shouldn't get here. */
701  return 0.0;
702 }
#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
Datum area(PG_FUNCTION_ARGS)
static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
Definition: lwspheroid.c:499
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwspheroid.c:647
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
unsigned int uint32_t
Definition: uthash.h:78

References area(), COLLECTIONTYPE, LWCOLLECTION::geoms, lwgeom_area_spheroid(), lwgeom_is_empty(), MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, LWPOLY::nrings, POLYGONTYPE, ptarray_area_spheroid(), LWPOLY::rings, LWGEOM::type, and ovdump::type.

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

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