andoyer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2016 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2016 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2016 Mateusz Loskot, London, UK.
  6. // This file was modified by Oracle on 2014-2017.
  7. // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #include <geometry_test_common.hpp>
  16. #include <boost/concept_check.hpp>
  17. #include <boost/geometry/algorithms/assign.hpp>
  18. #include <boost/geometry/algorithms/distance.hpp>
  19. #include <boost/geometry/geometries/point.hpp>
  20. #include <boost/geometry/srs/spheroid.hpp>
  21. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  22. #include <boost/geometry/strategies/geographic/distance_andoyer.hpp>
  23. #include <boost/geometry/strategies/geographic/side_andoyer.hpp>
  24. #include <test_common/test_point.hpp>
  25. #ifdef HAVE_TTMATH
  26. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  27. #endif
  28. double make_deg(double deg, double min, double sec)
  29. {
  30. return deg + min / 60.0 + sec / 3600.0;
  31. }
  32. double to_rad(double deg)
  33. {
  34. return bg::math::pi<double>() * deg / 180.0;
  35. }
  36. double to_deg(double rad)
  37. {
  38. return 180.0 * rad / bg::math::pi<double>();
  39. }
  40. double normlized_deg(double deg)
  41. {
  42. if (deg > 180)
  43. return deg - 360;
  44. else if (deg < -180)
  45. return deg + 360;
  46. else
  47. return deg;
  48. }
  49. template <typename P1, typename P2>
  50. void test_distance(double lon1, double lat1, double lon2, double lat2, double expected_km)
  51. {
  52. // Set radius type, but for integer coordinates we want to have floating point radius type
  53. typedef typename bg::promote_floating_point
  54. <
  55. typename bg::coordinate_type<P1>::type
  56. >::type rtype;
  57. typedef bg::srs::spheroid<rtype> stype;
  58. typedef bg::strategy::distance::andoyer<stype> andoyer_type;
  59. typedef bg::strategy::distance::geographic<bg::strategy::andoyer, stype> geographic_type;
  60. typedef bg::formula::andoyer_inverse<rtype, true, false> andoyer_inverse_type;
  61. BOOST_CONCEPT_ASSERT
  62. (
  63. (bg::concepts::PointDistanceStrategy<andoyer_type, P1, P2>)
  64. );
  65. andoyer_type andoyer;
  66. geographic_type geographic;
  67. typedef typename bg::strategy::distance
  68. ::services::return_type<andoyer_type, P1, P2>::type return_type;
  69. P1 p1;
  70. P2 p2;
  71. bg::assign_values(p1, lon1, lat1);
  72. bg::assign_values(p2, lon2, lat2);
  73. return_type d_strategy = andoyer.apply(p1, p2);
  74. return_type d_strategy2 = geographic.apply(p1, p2);
  75. return_type d_function = bg::distance(p1, p2, andoyer);
  76. double diff = bg::math::longitude_distance_signed<bg::degree>(lon1, lon2);
  77. return_type d_formula;
  78. // if the points lay on a meridian, distance strategy calls the special formula
  79. // for meridian distance that returns different result than andoyer formula
  80. // for nearly antipodal points
  81. if (bg::math::equals(diff, 0.0)
  82. || bg::math::equals(bg::math::abs(diff), 180.0))
  83. {
  84. d_formula = d_strategy;
  85. }
  86. else
  87. {
  88. d_formula = andoyer_inverse_type::apply(to_rad(lon1), to_rad(lat1),
  89. to_rad(lon2), to_rad(lat2),
  90. stype()).distance;
  91. }
  92. BOOST_CHECK_CLOSE(d_strategy / 1000.0, expected_km, 0.001);
  93. BOOST_CHECK_CLOSE(d_strategy2 / 1000.0, expected_km, 0.001);
  94. BOOST_CHECK_CLOSE(d_function / 1000.0, expected_km, 0.001);
  95. BOOST_CHECK_CLOSE(d_formula / 1000.0, expected_km, 0.001);
  96. }
  97. template <typename PS, typename P>
  98. void test_azimuth(double lon1, double lat1,
  99. double lon2, double lat2,
  100. double expected_azimuth_deg)
  101. {
  102. // Set radius type, but for integer coordinates we want to have floating point radius type
  103. typedef typename bg::promote_floating_point
  104. <
  105. typename bg::coordinate_type<PS>::type
  106. >::type rtype;
  107. typedef bg::srs::spheroid<rtype> stype;
  108. typedef bg::formula::andoyer_inverse<rtype, false, true> andoyer_inverse_type;
  109. rtype a_formula = andoyer_inverse_type::apply(to_rad(lon1), to_rad(lat1), to_rad(lon2), to_rad(lat2), stype()).azimuth;
  110. rtype azimuth_deg = to_deg(a_formula);
  111. if (bg::math::equals(azimuth_deg, -180.0))
  112. azimuth_deg = 180.0;
  113. if (bg::math::equals(expected_azimuth_deg, -180.0))
  114. expected_azimuth_deg = 180.0;
  115. if (bg::math::equals(expected_azimuth_deg, 0.0))
  116. {
  117. BOOST_CHECK(bg::math::equals(azimuth_deg, expected_azimuth_deg));
  118. }
  119. else
  120. {
  121. BOOST_CHECK_CLOSE(azimuth_deg, expected_azimuth_deg, 0.001);
  122. }
  123. }
  124. template <typename P1, typename P2>
  125. void test_distazi(double lon1, double lat1, double lon2, double lat2,
  126. double expected_km, double expected_azimuth_deg)
  127. {
  128. test_distance<P1, P2>(lon1, lat1, lon2, lat2, expected_km);
  129. test_azimuth<P1, P2>(lon1, lat1, lon2, lat2, expected_azimuth_deg);
  130. }
  131. // requires SW->NE
  132. template <typename P1, typename P2>
  133. void test_distazi_symm(double lon1, double lat1, double lon2, double lat2,
  134. double expected_km, double expected_azimuth_deg,
  135. bool is_antipodal = false)
  136. {
  137. double d180 = is_antipodal ? 0 : 180;
  138. test_distazi<P1, P2>(lon1, lat1, lon2, lat2, expected_km, expected_azimuth_deg);
  139. test_distazi<P1, P2>(-lon1, lat1, -lon2, lat2, expected_km, -expected_azimuth_deg);
  140. test_distazi<P1, P2>(lon1, -lat1, lon2, -lat2, expected_km, d180 - expected_azimuth_deg);
  141. test_distazi<P1, P2>(-lon1, -lat1, -lon2, -lat2, expected_km, -d180 + expected_azimuth_deg);
  142. }
  143. template <typename P1, typename P2>
  144. void test_distazi_symmNS(double lon1, double lat1, double lon2, double lat2,
  145. double expected_km, double expected_azimuth_deg)
  146. {
  147. test_distazi<P1, P2>(lon1, lat1, lon2, lat2, expected_km, expected_azimuth_deg);
  148. test_distazi<P1, P2>(lon1, -lat1, lon2, -lat2, expected_km, 180 - expected_azimuth_deg);
  149. }
  150. template <typename PS, typename P>
  151. void test_side(double lon1, double lat1,
  152. double lon2, double lat2,
  153. double lon, double lat,
  154. int expected_side)
  155. {
  156. // Set radius type, but for integer coordinates we want to have floating point radius type
  157. typedef typename bg::promote_floating_point
  158. <
  159. typename bg::coordinate_type<PS>::type
  160. >::type rtype;
  161. typedef bg::srs::spheroid<rtype> stype;
  162. typedef bg::strategy::side::andoyer<stype> strategy_type;
  163. typedef bg::strategy::side::geographic<bg::strategy::andoyer, stype> strategy2_type;
  164. strategy_type strategy;
  165. strategy2_type strategy2;
  166. PS p1, p2;
  167. P p;
  168. bg::assign_values(p1, lon1, lat1);
  169. bg::assign_values(p2, lon2, lat2);
  170. bg::assign_values(p, lon, lat);
  171. int side = strategy.apply(p1, p2, p);
  172. int side2 = strategy2.apply(p1, p2, p);
  173. BOOST_CHECK_EQUAL(side, expected_side);
  174. BOOST_CHECK_EQUAL(side2, expected_side);
  175. }
  176. template <typename P1, typename P2>
  177. void test_all()
  178. {
  179. // polar
  180. test_distazi<P1, P2>(0, 90, 1, 80,
  181. 1116.814237, 179);
  182. // no point difference
  183. test_distazi<P1, P2>(4, 52, 4, 52,
  184. 0.0, 0.0);
  185. // normal cases
  186. test_distazi<P1, P2>(4, 52, 3, 40,
  187. 1336.039890, -176.3086);
  188. test_distazi<P1, P2>(3, 52, 4, 40,
  189. 1336.039890, 176.3086);
  190. test_distazi<P1, P2>(make_deg(17, 19, 43.28),
  191. make_deg(40, 30, 31.151),
  192. 18, 40,
  193. 80.323245,
  194. make_deg(134, 27, 50.05));
  195. // antipodal
  196. // ok? in those cases shorter path would pass through a pole
  197. // but 90 or -90 would be consistent with distance?
  198. test_distazi<P1, P2>(0, 0, 180, 0, 20003.9, 0.0);
  199. test_distazi<P1, P2>(0, 0, -180, 0, 20003.9, 0.0);
  200. test_distazi<P1, P2>(-90, 0, 90, 0, 20003.9, 0.0);
  201. test_distazi<P1, P2>(90, 0, -90, 0, 20003.9, 0.0);
  202. // 0, 45, 90 ...
  203. for (int i = 0 ; i < 360 ; i += 45)
  204. {
  205. // 0 45 90 ...
  206. double l = normlized_deg(i);
  207. // -1 44 89 ...
  208. double l1 = normlized_deg(i - 1);
  209. // 1 46 91 ...
  210. double l2 = normlized_deg(i + 1);
  211. // near equator
  212. test_distazi_symm<P1, P2>(l1, -1, l2, 1, 313.7956, 45.1964);
  213. // near poles
  214. test_distazi_symmNS<P1, P2>(l, -89.5, l, 89.5, 19892.2, 0.0);
  215. test_distazi_symmNS<P1, P2>(l, -89.6, l, 89.6, 19914.6, 0.0);
  216. test_distazi_symmNS<P1, P2>(l, -89.7, l, 89.7, 19936.9, 0.0);
  217. test_distazi_symmNS<P1, P2>(l, -89.8, l, 89.8, 19959.2, 0.0);
  218. test_distazi_symmNS<P1, P2>(l, -89.9, l, 89.9, 19981.6, 0.0);
  219. test_distazi_symmNS<P1, P2>(l, -89.99, l, 89.99, 20001.7, 0.0);
  220. test_distazi_symmNS<P1, P2>(l, -89.999, l, 89.999, 20003.7, 0.0);
  221. // antipodal
  222. test_distazi_symmNS<P1, P2>(l, -90, l, 90, 20003.9, 0.0);
  223. test_distazi_symm<P1, P2>(normlized_deg(l-10.0), -10.0, normlized_deg(l+135), 45, 14892.1, 34.1802);
  224. test_distazi_symm<P1, P2>(normlized_deg(l-30.0), -30.0, normlized_deg(l+135), 45, 17890.7, 33.7002);
  225. test_distazi_symm<P1, P2>(normlized_deg(l-40.0), -40.0, normlized_deg(l+135), 45, 19319.7, 33.4801);
  226. test_distazi_symm<P1, P2>(normlized_deg(l-41.0), -41.0, normlized_deg(l+135), 45, 19459.1, 33.2408);
  227. test_distazi_symm<P1, P2>(normlized_deg(l-42.0), -42.0, normlized_deg(l+135), 45, 19597.8, 32.7844);
  228. test_distazi_symm<P1, P2>(normlized_deg(l-43.0), -43.0, normlized_deg(l+135), 45, 19735.8, 31.7784);
  229. test_distazi_symm<P1, P2>(normlized_deg(l-44.0), -44.0, normlized_deg(l+135), 45, 19873.0, 28.5588);
  230. test_distazi_symm<P1, P2>(normlized_deg(l-44.1), -44.1, normlized_deg(l+135), 45, 19886.7, 27.8304);
  231. test_distazi_symm<P1, P2>(normlized_deg(l-44.2), -44.2, normlized_deg(l+135), 45, 19900.4, 26.9173);
  232. test_distazi_symm<P1, P2>(normlized_deg(l-44.3), -44.3, normlized_deg(l+135), 45, 19914.1, 25.7401);
  233. test_distazi_symm<P1, P2>(normlized_deg(l-44.4), -44.4, normlized_deg(l+135), 45, 19927.7, 24.1668);
  234. test_distazi_symm<P1, P2>(normlized_deg(l-44.5), -44.5, normlized_deg(l+135), 45, 19941.4, 21.9599);
  235. test_distazi_symm<P1, P2>(normlized_deg(l-44.6), -44.6, normlized_deg(l+135), 45, 19955.0, 18.6438);
  236. test_distazi_symm<P1, P2>(normlized_deg(l-44.7), -44.7, normlized_deg(l+135), 45, 19968.6, 13.1096);
  237. test_distazi_symm<P1, P2>(normlized_deg(l-44.8), -44.8, normlized_deg(l+135), 45, 19982.3, 2.0300);
  238. // nearly antipodal
  239. test_distazi_symm<P1, P2>(normlized_deg(l-44.9), -44.9, normlized_deg(l+135), 45, 19995.9, 0.0);
  240. test_distazi_symm<P1, P2>(normlized_deg(l-44.95), -44.95, normlized_deg(l+135), 45, 20002.7, 0.0);
  241. test_distazi_symm<P1, P2>(normlized_deg(l-44.99), -44.99, normlized_deg(l+135), 45, 20008.1, 0.0);
  242. test_distazi_symm<P1, P2>(normlized_deg(l-44.999), -44.999, normlized_deg(l+135), 45, 20009.4, 0.0);
  243. // antipodal
  244. test_distazi_symm<P1, P2>(normlized_deg(l-45), -45, normlized_deg(l+135), 45, 20003.92, 0.0, true);
  245. }
  246. /* SQL Server gives:
  247. 1116.82586908528, 0, 1336.02721932545
  248. with:
  249. SELECT 0.001 * geography::STGeomFromText('POINT(0 90)', 4326).STDistance(geography::STGeomFromText('POINT(1 80)', 4326))
  250. union SELECT 0.001 * geography::STGeomFromText('POINT(4 52)', 4326).STDistance(geography::STGeomFromText('POINT(4 52)', 4326))
  251. union SELECT 0.001 * geography::STGeomFromText('POINT(4 52)', 4326).STDistance(geography::STGeomFromText('POINT(3 40)', 4326))
  252. */
  253. test_side<P1, P2>(0, 0, 0, 1, 0, 2, 0);
  254. test_side<P1, P2>(0, 0, 0, 1, 0, -2, 0);
  255. test_side<P1, P2>(10, 0, 10, 1, 10, 2, 0);
  256. test_side<P1, P2>(10, 0, 10, -1, 10, 2, 0);
  257. test_side<P1, P2>(10, 0, 10, 1, 0, 2, 1); // left
  258. test_side<P1, P2>(10, 0, 10, -1, 0, 2, -1); // right
  259. test_side<P1, P2>(-10, -10, 10, 10, 10, 0, -1); // right
  260. test_side<P1, P2>(-10, -10, 10, 10, -10, 0, 1); // left
  261. test_side<P1, P2>(170, -10, -170, 10, -170, 0, -1); // right
  262. test_side<P1, P2>(170, -10, -170, 10, 170, 0, 1); // left
  263. }
  264. template <typename P>
  265. void test_all()
  266. {
  267. test_all<P, P>();
  268. }
  269. int test_main(int, char* [])
  270. {
  271. //test_all<float[2]>();
  272. //test_all<double[2]>();
  273. //test_all<bg::model::point<int, 2, bg::cs::geographic<bg::degree> > >();
  274. //test_all<bg::model::point<float, 2, bg::cs::geographic<bg::degree> > >();
  275. test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
  276. #if defined(HAVE_TTMATH)
  277. test_all<bg::model::point<ttmath::Big<1,4>, 2, bg::cs::geographic<bg::degree> > >();
  278. test_all<bg::model::point<ttmath_big, 2, bg::cs::geographic<bg::degree> > >();
  279. #endif
  280. return 0;
  281. }