PostGIS  2.4.9dev-r@@SVN_REVISION@@

## ◆ lwgeom_area_spheroid()

 double lwgeom_area_spheroid ( const LWGEOM * lwgeom, const SPHEROID * spheroid )

Calculate the geodetic area of a lwgeom on the spheroid.

The result will have the squared units of the spheroid axes.

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.

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  int 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, 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  int 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:87
Datum area(PG_FUNCTION_ARGS)
LWGEOM ** geoms
Definition: liblwgeom.h:509
POINTARRAY ** rings
Definition: liblwgeom.h:457
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwspheroid.c:642
int nrings
Definition: liblwgeom.h:455
static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
Definition: lwspheroid.c:494
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
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: 