vincenty_direct.hpp 6.7 KB

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