area_box.hpp 6.6 KB

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