PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ 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:108
#define MULTIPOLYGONTYPE
Definition liblwgeom.h:107
#define POLYGONTYPE
Definition liblwgeom.h:104
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:199
static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
Definition lwspheroid.c:142
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition lwspheroid.c:647
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

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

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

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