...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Calculates the area of a geometry using the specified strategy.
The free function area calculates the area of a geometry using the specified strategy. Reasons to specify a strategy include: use another coordinate system for calculations; construct the strategy beforehand (e.g. with the radius of the Earth); select a strategy when there are more than one available for a calculation.
template<typename Geometry, typename Strategy> Strategy::return_type area(Geometry const & geometry, Strategy const & strategy)
Type |
Concept |
Name |
Description |
---|---|---|---|
Geometry const & |
Any type fulfilling a Geometry Concept |
geometry |
A model of the specified concept |
Strategy const & |
Any type fulfilling a Area Strategy Concept |
strategy |
The strategy which will be used for area calculations |
The calculated area
Either
#include <boost/geometry.hpp>
Or
#include <boost/geometry/algorithms/area.hpp>
The function area implements function Area from the OGC Simple Feature Specification.
Case |
Behavior |
---|---|
pointlike (e.g. point) |
Returns 0 |
linear (e.g. linestring) |
Returns 0 |
areal (e.g. polygon) |
Returns the area |
Cartesian |
Returns the area in the same units as the input coordinates |
Spherical |
Returns the area on a unit sphere (or another sphere, if specified as such in the constructor of the strategy) |
Reversed polygon (coordinates not according their orientiation) |
Returns the negative area |
Geometry |
Status |
---|---|
Point |
|
Segment |
|
Box |
|
Linestring |
|
Ring |
|
Polygon |
|
MultiPoint |
|
MultiLinestring |
|
MultiPolygon |
|
Variant |
|
Linear
Calculate the area of a polygon
#include <iostream> #include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/geometry/geometries/polygon.hpp> namespace bg = boost::geometry; int main() { // Calculate the area of a cartesian polygon bg::model::polygon<bg::model::d2::point_xy<double> > poly; bg::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly); double area = bg::area(poly); std::cout << "Area: " << area << std::endl; // Calculate the area of a spherical polygon (for latitude: 0 at equator) bg::model::polygon<bg::model::point<float, 2, bg::cs::spherical_equatorial<bg::degree> > > sph_poly; bg::read_wkt("POLYGON((0 0,0 45,45 0,0 0))", sph_poly); area = bg::area(sph_poly); std::cout << "Area: " << area << std::endl; return 0; }
Output:
Area: 16 Area: 0.339837