vincenty_direct.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Boost.Geometry
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014-2020.
  4. // Modifications copyright (c) 2014-2020 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_FORMULAS_VINCENTY_DIRECT_HPP
  10. #define BOOST_GEOMETRY_FORMULAS_VINCENTY_DIRECT_HPP
  11. #include <boost/math/constants/constants.hpp>
  12. #include <boost/geometry/core/radius.hpp>
  13. #include <boost/geometry/util/condition.hpp>
  14. #include <boost/geometry/util/math.hpp>
  15. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  16. #include <boost/geometry/formulas/differential_quantities.hpp>
  17. #include <boost/geometry/formulas/flattening.hpp>
  18. #include <boost/geometry/formulas/result_direct.hpp>
  19. #ifndef BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS
  20. #define BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS 1000
  21. #endif
  22. namespace boost { namespace geometry { namespace formula
  23. {
  24. /*!
  25. \brief The solution of the direct problem of geodesics on latlong coordinates, after Vincenty, 1975
  26. \author See
  27. - http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
  28. - http://www.icsm.gov.au/gda/gdav2.3.pdf
  29. \author Adapted from various implementations to get it close to the original document
  30. - http://www.movable-type.co.uk/scripts/LatLongVincenty.html
  31. - http://exogen.case.edu/projects/geopy/source/geopy.distance.html
  32. - http://futureboy.homeip.net/fsp/colorize.fsp?fileName=navigation.frink
  33. */
  34. template <
  35. typename CT,
  36. bool EnableCoordinates = true,
  37. bool EnableReverseAzimuth = false,
  38. bool EnableReducedLength = false,
  39. bool EnableGeodesicScale = false
  40. >
  41. class vincenty_direct
  42. {
  43. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  44. static const bool CalcCoordinates = EnableCoordinates || CalcQuantities;
  45. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
  46. public:
  47. typedef result_direct<CT> result_type;
  48. template <typename T, typename Dist, typename Azi, typename Spheroid>
  49. static inline result_type apply(T const& lo1,
  50. T const& la1,
  51. Dist const& distance,
  52. Azi const& azimuth12,
  53. Spheroid const& spheroid)
  54. {
  55. result_type result;
  56. CT const lon1 = lo1;
  57. CT const lat1 = la1;
  58. CT const radius_a = CT(get_radius<0>(spheroid));
  59. CT const radius_b = CT(get_radius<2>(spheroid));
  60. CT const flattening = formula::flattening<CT>(spheroid);
  61. CT const sin_azimuth12 = sin(azimuth12);
  62. CT const cos_azimuth12 = cos(azimuth12);
  63. // U: reduced latitude, defined by tan U = (1-f) tan phi
  64. CT const one_min_f = CT(1) - flattening;
  65. CT const tan_U1 = one_min_f * tan(lat1);
  66. CT const sigma1 = atan2(tan_U1, cos_azimuth12); // (1)
  67. // may be calculated from tan using 1 sqrt()
  68. CT const U1 = atan(tan_U1);
  69. CT const sin_U1 = sin(U1);
  70. CT const cos_U1 = cos(U1);
  71. CT const sin_alpha = cos_U1 * sin_azimuth12; // (2)
  72. CT const sin_alpha_sqr = math::sqr(sin_alpha);
  73. CT const cos_alpha_sqr = CT(1) - sin_alpha_sqr;
  74. CT const b_sqr = radius_b * radius_b;
  75. CT const u_sqr = cos_alpha_sqr * (radius_a * radius_a - b_sqr) / b_sqr;
  76. CT const A = CT(1) + (u_sqr/CT(16384)) * (CT(4096) + u_sqr*(CT(-768) + u_sqr*(CT(320) - u_sqr*CT(175)))); // (3)
  77. CT const B = (u_sqr/CT(1024))*(CT(256) + u_sqr*(CT(-128) + u_sqr*(CT(74) - u_sqr*CT(47)))); // (4)
  78. CT s_div_bA = distance / (radius_b * A);
  79. CT sigma = s_div_bA; // (7)
  80. CT previous_sigma;
  81. CT sin_sigma;
  82. CT cos_sigma;
  83. CT cos_2sigma_m;
  84. CT cos_2sigma_m_sqr;
  85. int counter = 0; // robustness
  86. do
  87. {
  88. previous_sigma = sigma;
  89. CT const two_sigma_m = CT(2) * sigma1 + sigma; // (5)
  90. sin_sigma = sin(sigma);
  91. cos_sigma = cos(sigma);
  92. CT const sin_sigma_sqr = math::sqr(sin_sigma);
  93. cos_2sigma_m = cos(two_sigma_m);
  94. cos_2sigma_m_sqr = math::sqr(cos_2sigma_m);
  95. CT const delta_sigma = B * sin_sigma * (cos_2sigma_m
  96. + (B/CT(4)) * ( cos_sigma * (CT(-1) + CT(2)*cos_2sigma_m_sqr)
  97. - (B/CT(6) * cos_2sigma_m * (CT(-3)+CT(4)*sin_sigma_sqr) * (CT(-3)+CT(4)*cos_2sigma_m_sqr)) )); // (6)
  98. sigma = s_div_bA + delta_sigma; // (7)
  99. ++counter; // robustness
  100. } while ( geometry::math::abs(previous_sigma - sigma) > CT(1e-12)
  101. //&& geometry::math::abs(sigma) < pi
  102. && counter < BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS ); // robustness
  103. if (BOOST_GEOMETRY_CONDITION(CalcCoordinates))
  104. {
  105. result.lat2
  106. = atan2( sin_U1 * cos_sigma + cos_U1 * sin_sigma * cos_azimuth12,
  107. one_min_f * math::sqrt(sin_alpha_sqr + math::sqr(sin_U1 * sin_sigma - cos_U1 * cos_sigma * cos_azimuth12))); // (8)
  108. CT const lambda = atan2( sin_sigma * sin_azimuth12,
  109. cos_U1 * cos_sigma - sin_U1 * sin_sigma * cos_azimuth12); // (9)
  110. CT const C = (flattening/CT(16)) * cos_alpha_sqr * ( CT(4) + flattening * ( CT(4) - CT(3) * cos_alpha_sqr ) ); // (10)
  111. CT const L = lambda - (CT(1) - C) * flattening * sin_alpha
  112. * ( sigma + C * sin_sigma * ( cos_2sigma_m + C * cos_sigma * ( CT(-1) + CT(2) * cos_2sigma_m_sqr ) ) ); // (11)
  113. result.lon2 = lon1 + L;
  114. }
  115. if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
  116. {
  117. result.reverse_azimuth
  118. = atan2(sin_alpha, -sin_U1 * sin_sigma + cos_U1 * cos_sigma * cos_azimuth12); // (12)
  119. }
  120. if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
  121. {
  122. typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
  123. quantities::apply(lon1, lat1, result.lon2, result.lat2,
  124. azimuth12, result.reverse_azimuth,
  125. radius_b, flattening,
  126. result.reduced_length, result.geodesic_scale);
  127. }
  128. if (BOOST_GEOMETRY_CONDITION(CalcCoordinates))
  129. {
  130. // For longitudes close to the antimeridian the result can be out
  131. // of range. Therefore normalize.
  132. // It has to be done at the end because otherwise differential
  133. // quantities are calculated incorrectly.
  134. math::detail::normalize_angle_cond<radian>(result.lon2);
  135. }
  136. return result;
  137. }
  138. };
  139. }}} // namespace boost::geometry::formula
  140. #endif // BOOST_GEOMETRY_FORMULAS_VINCENTY_DIRECT_HPP