distance.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2016 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014-2018.
  4. // Modifications copyright (c) 2014-2018 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_DISTANCE_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_DISTANCE_HPP
  12. #include <boost/geometry/core/coordinate_type.hpp>
  13. #include <boost/geometry/core/radian_access.hpp>
  14. #include <boost/geometry/core/radius.hpp>
  15. #include <boost/geometry/formulas/andoyer_inverse.hpp>
  16. #include <boost/geometry/formulas/meridian_inverse.hpp>
  17. #include <boost/geometry/formulas/flattening.hpp>
  18. #include <boost/geometry/srs/spheroid.hpp>
  19. #include <boost/geometry/strategies/distance.hpp>
  20. #include <boost/geometry/strategies/geographic/parameters.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  23. #include <boost/geometry/util/promote_floating_point.hpp>
  24. #include <boost/geometry/util/select_calculation_type.hpp>
  25. #include <boost/geometry/geometries/point_xy.hpp>
  26. namespace boost { namespace geometry
  27. {
  28. namespace strategy { namespace distance
  29. {
  30. /*!
  31. \brief Distance calculation for geographic coordinates on a spheroid
  32. \ingroup strategies
  33. \tparam FormulaPolicy Formula used to calculate azimuths
  34. \tparam Spheroid The spheroid model
  35. \tparam CalculationType \tparam_calculation
  36. \qbk{
  37. [heading See also]
  38. \* [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  39. \* [link geometry.reference.srs.srs_spheroid srs::spheroid]
  40. }
  41. */
  42. template
  43. <
  44. typename FormulaPolicy = strategy::andoyer,
  45. typename Spheroid = srs::spheroid<double>,
  46. typename CalculationType = void
  47. >
  48. class geographic
  49. {
  50. public :
  51. template <typename Point1, typename Point2>
  52. struct calculation_type
  53. : promote_floating_point
  54. <
  55. typename select_calculation_type
  56. <
  57. Point1,
  58. Point2,
  59. CalculationType
  60. >::type
  61. >
  62. {};
  63. typedef Spheroid model_type;
  64. inline geographic()
  65. : m_spheroid()
  66. {}
  67. explicit inline geographic(Spheroid const& spheroid)
  68. : m_spheroid(spheroid)
  69. {}
  70. template <typename CT>
  71. static inline CT apply(CT lon1, CT lat1, CT lon2, CT lat2,
  72. Spheroid const& spheroid)
  73. {
  74. typedef typename formula::meridian_inverse
  75. <
  76. CT, strategy::default_order<FormulaPolicy>::value
  77. > meridian_inverse;
  78. typename meridian_inverse::result res =
  79. meridian_inverse::apply(lon1, lat1, lon2, lat2, spheroid);
  80. if (res.meridian)
  81. {
  82. return res.distance;
  83. }
  84. return FormulaPolicy::template inverse
  85. <
  86. CT, true, false, false, false, false
  87. >::apply(lon1, lat1, lon2, lat2, spheroid).distance;
  88. }
  89. template <typename Point1, typename Point2>
  90. inline typename calculation_type<Point1, Point2>::type
  91. apply(Point1 const& point1, Point2 const& point2) const
  92. {
  93. typedef typename calculation_type<Point1, Point2>::type CT;
  94. CT lon1 = get_as_radian<0>(point1);
  95. CT lat1 = get_as_radian<1>(point1);
  96. CT lon2 = get_as_radian<0>(point2);
  97. CT lat2 = get_as_radian<1>(point2);
  98. return apply(lon1, lat1, lon2, lat2, m_spheroid);
  99. }
  100. inline Spheroid const& model() const
  101. {
  102. return m_spheroid;
  103. }
  104. private :
  105. Spheroid m_spheroid;
  106. };
  107. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  108. namespace services
  109. {
  110. template
  111. <
  112. typename FormulaPolicy,
  113. typename Spheroid,
  114. typename CalculationType
  115. >
  116. struct tag<geographic<FormulaPolicy, Spheroid, CalculationType> >
  117. {
  118. typedef strategy_tag_distance_point_point type;
  119. };
  120. template
  121. <
  122. typename FormulaPolicy,
  123. typename Spheroid,
  124. typename CalculationType,
  125. typename P1,
  126. typename P2
  127. >
  128. struct return_type<geographic<FormulaPolicy, Spheroid, CalculationType>, P1, P2>
  129. : geographic<FormulaPolicy, Spheroid, CalculationType>::template calculation_type<P1, P2>
  130. {};
  131. template
  132. <
  133. typename FormulaPolicy,
  134. typename Spheroid,
  135. typename CalculationType
  136. >
  137. struct comparable_type<geographic<FormulaPolicy, Spheroid, CalculationType> >
  138. {
  139. typedef geographic<FormulaPolicy, Spheroid, CalculationType> type;
  140. };
  141. template
  142. <
  143. typename FormulaPolicy,
  144. typename Spheroid,
  145. typename CalculationType
  146. >
  147. struct get_comparable<geographic<FormulaPolicy, Spheroid, CalculationType> >
  148. {
  149. static inline geographic<FormulaPolicy, Spheroid, CalculationType>
  150. apply(geographic<FormulaPolicy, Spheroid, CalculationType> const& input)
  151. {
  152. return input;
  153. }
  154. };
  155. template
  156. <
  157. typename FormulaPolicy,
  158. typename Spheroid,
  159. typename CalculationType,
  160. typename P1,
  161. typename P2
  162. >
  163. struct result_from_distance<geographic<FormulaPolicy, Spheroid, CalculationType>, P1, P2>
  164. {
  165. template <typename T>
  166. static inline typename return_type<geographic<FormulaPolicy, Spheroid, CalculationType>, P1, P2>::type
  167. apply(geographic<FormulaPolicy, Spheroid, CalculationType> const& , T const& value)
  168. {
  169. return value;
  170. }
  171. };
  172. template <typename Point1, typename Point2>
  173. struct default_strategy<point_tag, point_tag, Point1, Point2, geographic_tag, geographic_tag>
  174. {
  175. typedef strategy::distance::geographic
  176. <
  177. strategy::andoyer,
  178. srs::spheroid
  179. <
  180. typename select_coordinate_type<Point1, Point2>::type
  181. >
  182. > type;
  183. };
  184. } // namespace services
  185. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  186. }} // namespace strategy::distance
  187. }} // namespace boost::geometry
  188. #endif // BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_DISTANCE_HPP