PostGIS  2.5.1dev-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 642 of file lwspheroid.c.

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

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

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