distance_se_pl_pl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2014-2018, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Licensed under the Boost Software License version 1.0.
  8. // http://www.boost.org/users/license.html
  9. #include <iostream>
  10. #ifndef BOOST_TEST_MODULE
  11. #define BOOST_TEST_MODULE test_distance_spherical_equatorial_pointlike_pointlike
  12. #endif
  13. #include <boost/range.hpp>
  14. #include <boost/test/included/unit_test.hpp>
  15. #include <boost/geometry/strategies/strategies.hpp>
  16. #include "test_distance_se_common.hpp"
  17. #include "test_empty_geometry.hpp"
  18. typedef bg::cs::spherical_equatorial<bg::degree> cs_type;
  19. typedef bg::model::point<double, 2, cs_type> point_type;
  20. typedef bg::model::multi_point<point_type> multi_point_type;
  21. namespace distance = bg::strategy::distance;
  22. namespace services = distance::services;
  23. typedef bg::default_distance_result<point_type>::type return_type;
  24. typedef distance::haversine<double> point_point_strategy;
  25. typedef distance::comparable::haversine<double> comparable_point_point_strategy;
  26. //===========================================================================
  27. template <typename Strategy>
  28. inline typename bg::distance_result<point_type, point_type, Strategy>::type
  29. distance_from_wkt(std::string const& wkt1,
  30. std::string const& wkt2,
  31. Strategy const& strategy)
  32. {
  33. point_type p1, p2;
  34. bg::read_wkt(wkt1, p1);
  35. bg::read_wkt(wkt2, p2);
  36. return bg::distance(p1, p2, strategy);
  37. }
  38. inline bg::default_comparable_distance_result<point_type>::type
  39. comparable_distance_from_wkt(std::string const& wkt1,
  40. std::string const& wkt2)
  41. {
  42. point_type p1, p2;
  43. bg::read_wkt(wkt1, p1);
  44. bg::read_wkt(wkt2, p2);
  45. return bg::comparable_distance(p1, p2);
  46. }
  47. //===========================================================================
  48. template <typename Strategy>
  49. void test_distance_point_point(Strategy const& strategy,
  50. bool is_comparable_strategy = false)
  51. {
  52. double pi = bg::math::pi<double>();
  53. double r = strategy.radius();
  54. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  55. std::cout << std::endl;
  56. std::cout << "point/point distance tests" << std::endl;
  57. #endif
  58. typedef test_distance_of_geometries<point_type, point_type> tester;
  59. tester::apply("p-p-01",
  60. "POINT(10 10)",
  61. "POINT(0 0)",
  62. (is_comparable_strategy
  63. ? 0.0150768448035229
  64. : (0.24619691677893202 * r)),
  65. 0.0150768448035229,
  66. strategy);
  67. tester::apply("p-p-02",
  68. "POINT(10 10)",
  69. "POINT(10 10)",
  70. 0,
  71. strategy);
  72. // antipodal points
  73. tester::apply("p-p-03",
  74. "POINT(0 10)",
  75. "POINT(180 -10)",
  76. (is_comparable_strategy
  77. ? 1.0
  78. : (pi * r)),
  79. 1.0,
  80. strategy);
  81. tester::apply("p-p-04",
  82. "POINT(0 0)",
  83. "POINT(180 0)",
  84. (is_comparable_strategy
  85. ? 1.0
  86. : (pi * r)),
  87. 1.0,
  88. strategy);
  89. // https://svn.boost.org/trac/boost/ticket/11982
  90. tester::apply("p-p-05",
  91. "POINT(10.521812 59.887214)",
  92. "POINT(10.4 63.43)",
  93. (is_comparable_strategy
  94. ? 0.00095578716185788
  95. : (0.06184146915711819 * r)),
  96. 0.00095578716185788,
  97. strategy);
  98. tester::apply("p-p-06",
  99. "POINT(10.733557 59.911923)",
  100. "POINT(10.4 63.43)",
  101. (is_comparable_strategy
  102. ? 0.0009441561071329
  103. : (0.0614639773975828 * r)),
  104. 0.000944156107132969,
  105. strategy);
  106. }
  107. //===========================================================================
  108. template <typename Strategy>
  109. void test_distance_point_multipoint(Strategy const& strategy)
  110. {
  111. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  112. std::cout << std::endl;
  113. std::cout << "point/multipoint distance tests" << std::endl;
  114. #endif
  115. typedef test_distance_of_geometries<point_type, multi_point_type> tester;
  116. tester::apply("p-mp-01",
  117. "POINT(10 10)",
  118. "MULTIPOINT(10 10,20 10,20 20,10 20)",
  119. 0,
  120. strategy);
  121. tester::apply("p-mp-02",
  122. "POINT(10 10)",
  123. "MULTIPOINT(20 20,20 30,30 20,30 30)",
  124. distance_from_wkt("POINT(10 10)", "POINT(20 20)", strategy),
  125. comparable_distance_from_wkt("POINT(10 10)", "POINT(20 20)"),
  126. strategy);
  127. tester::apply("p-mp-03",
  128. "POINT(3 0)",
  129. "MULTIPOINT(20 20,20 40,40 20,40 40)",
  130. distance_from_wkt("POINT(3 0)", "POINT(20 20)", strategy),
  131. comparable_distance_from_wkt("POINT(3 0)", "POINT(20 20)"),
  132. strategy);
  133. // almost antipodal points
  134. tester::apply("p-mp-04",
  135. "POINT(179 2)",
  136. "MULTIPOINT(3 3,4 3,4 4,3 4)",
  137. distance_from_wkt("POINT(179 2)", "POINT(4 4)", strategy),
  138. comparable_distance_from_wkt("POINT(179 2)", "POINT(4 4)"),
  139. strategy);
  140. // minimum distance across the dateline
  141. tester::apply("p-mp-05",
  142. "POINT(355 5)",
  143. "MULTIPOINT(10 10,20 10,20 20,10 20)",
  144. distance_from_wkt("POINT(355 5)", "POINT(10 10)", strategy),
  145. comparable_distance_from_wkt("POINT(355 5)", "POINT(10 10)"),
  146. strategy);
  147. tester::apply("p-mp-06",
  148. "POINT(-5 5)",
  149. "MULTIPOINT(10 10,20 10,20 20,10 20)",
  150. distance_from_wkt("POINT(-5 5)", "POINT(10 10)", strategy),
  151. comparable_distance_from_wkt("POINT(-5 5)", "POINT(10 10)"),
  152. strategy);
  153. }
  154. //===========================================================================
  155. template <typename Strategy>
  156. void test_distance_multipoint_multipoint(Strategy const& strategy)
  157. {
  158. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  159. std::cout << std::endl;
  160. std::cout << "multipoint/multipoint distance tests" << std::endl;
  161. #endif
  162. typedef test_distance_of_geometries
  163. <
  164. multi_point_type, multi_point_type
  165. > tester;
  166. tester::apply("mp-mp-01",
  167. "MULTIPOINT(10 10,11 10,10 11,11 11)",
  168. "MULTIPOINT(11 11,12 11,12 12,11 12)",
  169. 0,
  170. strategy);
  171. tester::apply("mp-mp-02",
  172. "MULTIPOINT(10 10,11 10,10 11,11 11)",
  173. "MULTIPOINT(12 12,12 13,13 12,13 13)",
  174. distance_from_wkt("POINT(11 11)", "POINT(12 12)", strategy),
  175. comparable_distance_from_wkt("POINT(11 11)", "POINT(12 12)"),
  176. strategy);
  177. // example with many points in each multi-point so that the r-tree
  178. // does some splitting.
  179. tester::apply("mp-mp-03",
  180. "MULTIPOINT(1 1,1 2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1 10,\
  181. 2 1,2 2,2 3,2 4,2 5,2 6,2 7,2 8,2 9,2 10,\
  182. 3 1,3 2,3 3,3 4,3 5,3 6,3 7,3 8,3 9,3 10,\
  183. 10 1,10 10)",
  184. "MULTIPOINT(11 11,11 12,11 13,11 14,11 15,\
  185. 11 16,11 17,11 18,11 19,11 20,\
  186. 12 11,12 12,12 13,12 24,12 15,\
  187. 12 16,12 17,12 18,12 29,12 20,\
  188. 13 11,13 12,13 13,13 24,13 15,\
  189. 13 16,13 17,13 18,13 29,13 20,\
  190. 20 11,20 20)",
  191. distance_from_wkt("POINT(10 10)", "POINT(11 11)", strategy),
  192. comparable_distance_from_wkt("POINT(10 10)", "POINT(11 11)"),
  193. strategy);
  194. }
  195. //===========================================================================
  196. BOOST_AUTO_TEST_CASE( test_all_pointlike_pointlike )
  197. {
  198. test_distance_point_point(point_point_strategy());
  199. test_distance_point_point(point_point_strategy(earth_radius_km));
  200. test_distance_point_point(point_point_strategy(earth_radius_miles));
  201. test_distance_point_point(comparable_point_point_strategy(), true);
  202. test_distance_point_point
  203. (comparable_point_point_strategy(earth_radius_km), true);
  204. test_distance_point_point
  205. (comparable_point_point_strategy(earth_radius_miles), true);
  206. }
  207. BOOST_AUTO_TEST_CASE( test_all_point_multipoint )
  208. {
  209. test_distance_point_multipoint(point_point_strategy());
  210. test_distance_point_multipoint(point_point_strategy(earth_radius_km));
  211. test_distance_point_multipoint(point_point_strategy(earth_radius_miles));
  212. test_distance_point_multipoint(comparable_point_point_strategy());
  213. test_distance_point_multipoint
  214. (comparable_point_point_strategy(earth_radius_km));
  215. test_distance_point_multipoint
  216. (comparable_point_point_strategy(earth_radius_miles));
  217. }
  218. BOOST_AUTO_TEST_CASE( test_all_multipoint_multipoint )
  219. {
  220. test_distance_multipoint_multipoint(point_point_strategy());
  221. test_distance_multipoint_multipoint(point_point_strategy(earth_radius_km));
  222. test_distance_multipoint_multipoint
  223. (point_point_strategy(earth_radius_miles));
  224. test_distance_multipoint_multipoint(comparable_point_point_strategy());
  225. test_distance_multipoint_multipoint
  226. (comparable_point_point_strategy(earth_radius_km));
  227. test_distance_multipoint_multipoint
  228. (comparable_point_point_strategy(earth_radius_miles));
  229. }
  230. BOOST_AUTO_TEST_CASE( test_all_empty_input_pointlike_pointlike )
  231. {
  232. test_more_empty_input_pointlike_pointlike
  233. <
  234. point_type
  235. >(point_point_strategy());
  236. test_more_empty_input_pointlike_pointlike
  237. <
  238. point_type
  239. >(point_point_strategy(earth_radius_km));
  240. test_more_empty_input_pointlike_pointlike
  241. <
  242. point_type
  243. >(point_point_strategy(earth_radius_miles));
  244. test_more_empty_input_pointlike_pointlike
  245. <
  246. point_type
  247. >(comparable_point_point_strategy());
  248. test_more_empty_input_pointlike_pointlike
  249. <
  250. point_type
  251. >(comparable_point_point_strategy(earth_radius_km));
  252. test_more_empty_input_pointlike_pointlike
  253. <
  254. point_type
  255. >(comparable_point_point_strategy(earth_radius_miles));
  256. }