3 // Copyright (c) 2021, Oracle and/or its affiliates.
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7 // Licensed under the Boost Software License version 1.0.
8 // http://www.boost.org/users/license.html
10 #ifndef BOOST_GEOMETRY_STRATEGY_GEOGRAPHIC_AREA_BOX_HPP
11 #define BOOST_GEOMETRY_STRATEGY_GEOGRAPHIC_AREA_BOX_HPP
14 #include <boost/geometry/core/radian_access.hpp>
15 #include <boost/geometry/srs/spheroid.hpp>
16 #include <boost/geometry/strategies/spherical/get_radius.hpp>
17 #include <boost/geometry/strategy/area.hpp>
18 #include <boost/geometry/util/normalize_spheroidal_box_coordinates.hpp>
21 namespace boost { namespace geometry
24 namespace strategy { namespace area
27 // Based on the approach for spherical coordinate system:
28 // https://math.stackexchange.com/questions/131735/surface-element-in-spherical-coordinates
29 // http://www.cs.cmu.edu/afs/cs/academic/class/16823-s16/www/pdfs/appearance-modeling-3.pdf
30 // https://www.astronomyclub.xyz/celestial-sphere-2/solid-angle-on-the-celestial-sphere.html
31 // https://mathworld.wolfram.com/SolidAngle.html
32 // https://en.wikipedia.org/wiki/Spherical_coordinate_system
33 // and equations for spheroid:
34 // https://en.wikipedia.org/wiki/Geographic_coordinate_conversion
35 // https://en.wikipedia.org/wiki/Meridian_arc
36 // Note that the equations use geodetic latitudes so we do not have to convert them.
37 // assume(y_max > y_min);
38 // assume(x_max > x_min);
39 // M: a*(1-e^2) / (1-e^2*sin(y)^2)^(3/2);
40 // N: a / sqrt(1-e^2*sin(y)^2);
42 // tellsimp(log(abs(e*sin(y_min)+1)), p_min);
43 // tellsimp(log(abs(e*sin(y_min)-1)), m_min);
44 // tellsimp(log(abs(e*sin(y_max)+1)), p_max);
45 // tellsimp(log(abs(e*sin(y_max)-1)), m_max);
46 // S: integrate(integrate(O, y, y_min, y_max), x, x_min, x_max);
49 // An alternative solution to the above formula was suggested by Charles Karney
50 // https://github.com/boostorg/geometry/pull/832
51 // The following are formulas for area of a box defined by the equator and some latitude,
54 // dlambda*b^2*sin(phi)/2*(1/(1-e^2*sin(phi)^2) + atanh(e*sin(phi))/(e*sin(phi)))
56 // dlambda*b^2*sin(phi)/2*(1/(1-e^2*sin(phi)^2) + atan(ea*sin(phi))/(ea*sin(phi)))
57 // where ea = sqrt(-e^2)
60 typename Spheroid = srs::spheroid<double>,
61 typename CalculationType = void
66 template <typename Box>
68 : strategy::area::detail::result_type
75 geographic_box() = default;
77 explicit geographic_box(Spheroid const& spheroid)
78 : m_spheroid(spheroid)
81 template <typename Box>
82 inline auto apply(Box const& box) const
84 typedef typename result_type<Box>::type return_type;
86 return_type const c0 = 0;
88 return_type x_min = get_as_radian<min_corner, 0>(box); // lon
89 return_type y_min = get_as_radian<min_corner, 1>(box); // lat
90 return_type x_max = get_as_radian<max_corner, 0>(box);
91 return_type y_max = get_as_radian<max_corner, 1>(box);
93 math::normalize_spheroidal_box_coordinates<radian>(x_min, y_min, x_max, y_max);
95 if (x_min == x_max || y_max == y_min)
100 return_type const e2 = formula::eccentricity_sqr<return_type>(m_spheroid);
102 return_type const x_diff = x_max - x_min;
103 return_type const sin_y_min = sin(y_min);
104 return_type const sin_y_max = sin(y_max);
106 if (math::equals(e2, c0))
109 return_type const a = get_radius<0>(m_spheroid);
110 return x_diff * (sin_y_max - sin_y_min) * a * a;
113 return_type const c1 = 1;
114 return_type const c2 = 2;
115 return_type const b = get_radius<2>(m_spheroid);
118 return_type const c4 = 4;
119 return_type const e = math::sqrt(e2);
121 return_type const p_min = log(math::abs(e * sin_y_min + c1));
122 return_type const p_max = log(math::abs(e * sin_y_max + c1));
123 return_type const m_min = log(math::abs(e * sin_y_min - c1));
124 return_type const m_max = log(math::abs(e * sin_y_max - c1));
125 return_type const n_min = e * sin_y_min * sin_y_min;
126 return_type const n_max = e * sin_y_max * sin_y_max;
127 return_type const d_min = e * n_min - c1;
128 return_type const d_max = e * n_max - c1;
130 // NOTE: For equal latitudes the original formula generated by maxima may give negative
131 // result. It's caused by the order of operations, so here they're rearranged for
133 return_type const comp0 = (p_min - m_min) / (c4 * e * d_min);
134 return_type const comp1 = sin_y_min / (c2 * d_min);
135 return_type const comp2 = n_min * (m_min - p_min) / (c4 * d_min);
136 return_type const comp3 = (p_max - m_max) / (c4 * e * d_max);
137 return_type const comp4 = sin_y_max / (c2 * d_max);
138 return_type const comp5 = n_max * (m_max - p_max) / (c4 * d_max);
139 return_type const comp02 = comp0 + comp1 + comp2;
140 return_type const comp35 = comp3 + comp4 + comp5;
142 return b * b * x_diff * (comp02 - comp35);
145 return_type const comp0_min = c1 / (c1 - e2 * sin_y_min * sin_y_min);
146 return_type const comp0_max = c1 / (c1 - e2 * sin_y_max * sin_y_max);
148 // NOTE: For latitudes equal to 0 the original formula returns NAN
149 return_type comp1_min = 0, comp1_max = 0;
152 return_type const e = math::sqrt(e2);
153 return_type const e_sin_y_min = e * sin_y_min;
154 return_type const e_sin_y_max = e * sin_y_max;
156 comp1_min = e_sin_y_min == c0 ? c1 : atanh(e_sin_y_min) / e_sin_y_min;
157 comp1_max = e_sin_y_max == c0 ? c1 : atanh(e_sin_y_max) / e_sin_y_max;
161 return_type const ea = math::sqrt(-e2);
162 return_type const ea_sin_y_min = ea * sin_y_min;
163 return_type const ea_sin_y_max = ea * sin_y_max;
165 comp1_min = ea_sin_y_min == c0 ? c1 : atan(ea_sin_y_min) / ea_sin_y_min;
166 comp1_max = ea_sin_y_max == c0 ? c1 : atan(ea_sin_y_max) / ea_sin_y_max;
169 return_type const comp01_min = sin_y_min * (comp0_min + comp1_min);
170 return_type const comp01_max = sin_y_max * (comp0_max + comp1_max);
172 return b * b * x_diff * (comp01_max - comp01_min) / c2;
175 Spheroid model() const
185 }} // namespace strategy::area
188 }} // namespace boost::geometry
191 #endif // BOOST_GEOMETRY_STRATEGY_GEOGRAPHIC_AREA_BOX_HPP