simplify.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <iterator>
  12. #include <algorithms/test_simplify.hpp>
  13. #include <boost/geometry/geometries/geometries.hpp>
  14. #include <boost/geometry/geometries/point_xy.hpp>
  15. #include <test_geometries/wrapped_boost_array.hpp>
  16. #include <test_common/test_point.hpp>
  17. // #define TEST_PULL89
  18. #ifdef TEST_PULL89
  19. #include <boost/geometry/strategies/cartesian/distance_projected_point_ax.hpp>
  20. #endif
  21. #ifdef TEST_PULL89
  22. template <typename Geometry, typename T>
  23. void test_with_ax(std::string const& wkt,
  24. std::string const& expected,
  25. T const& adt,
  26. T const& xdt)
  27. {
  28. typedef typename bg::point_type<Geometry>::type point_type;
  29. typedef bg::strategy::distance::detail::projected_point_ax<> ax_type;
  30. typedef typename bg::strategy::distance::services::return_type
  31. <
  32. bg::strategy::distance::detail::projected_point_ax<>,
  33. point_type,
  34. point_type
  35. >::type return_type;
  36. typedef bg::strategy::distance::detail::projected_point_ax_less
  37. <
  38. return_type
  39. > comparator_type;
  40. typedef bg::strategy::simplify::detail::douglas_peucker
  41. <
  42. point_type,
  43. bg::strategy::distance::detail::projected_point_ax<>,
  44. comparator_type
  45. > dp_ax;
  46. return_type max_distance(adt, xdt);
  47. comparator_type comparator(max_distance);
  48. dp_ax strategy(comparator);
  49. test_geometry<Geometry>(wkt, expected, max_distance, strategy);
  50. }
  51. #endif
  52. template <typename P>
  53. void test_all()
  54. {
  55. test_geometry<bg::model::linestring<P> >(
  56. "LINESTRING(0 0,5 5,10 10)",
  57. "LINESTRING(0 0,10 10)", 1.0);
  58. test_geometry<bg::model::linestring<P> >(
  59. "LINESTRING(0 0, 5 5, 6 5, 10 10)",
  60. "LINESTRING(0 0,10 10)", 1.0);
  61. test_geometry<bg::model::linestring<P> >(
  62. "LINESTRING(0 0,5 5,7 5,10 10)",
  63. "LINESTRING(0 0,5 5,7 5,10 10)", 1.0);
  64. // Lightning-form which fails for Douglas-Peucker
  65. test_geometry<bg::model::linestring<P> >(
  66. "LINESTRING(0 0,120 6,80 10,200 0)",
  67. "LINESTRING(0 0,120 6,80 10,200 0)", 7);
  68. // Same which reordered coordinates
  69. test_geometry<bg::model::linestring<P> >(
  70. "LINESTRING(0 0,80 10,120 6,200 0)",
  71. "LINESTRING(0 0,80 10,200 0)", 7);
  72. // Duplicate point is removed
  73. test_geometry<bg::model::linestring<P> >(
  74. "LINESTRING(0 0,0 0)",
  75. "LINESTRING(0 0)", 1.0);
  76. // Mail 2013-10-07, real-life test, piece of River Leine
  77. // PostGIS returns exactly the same result
  78. test_geometry<bg::model::linestring<P> >(
  79. "LINESTRING(4293586 3290439,4293568 3290340,4293566 3290332,4293570 3290244,4293576 3290192"
  80. ",4293785 3289660,4293832 3289597,4293879 3289564,4293937 3289545,4294130 3289558"
  81. ",4294204 3289553,4294240 3289539,4294301 3289479,4294317 3289420,4294311 3289353"
  82. ",4294276 3289302,4293870 3289045,4293795 3288978,4293713 3288879,4293669 3288767"
  83. ",4293654 3288652,4293657 3288563,4293690 3288452,4293761 3288360,4293914 3288215"
  84. ",4293953 3288142,4293960 3288044,4293951 3287961,4293913 3287875,4293708 3287628"
  85. ",4293658 3287542,4293633 3287459,4293630 3287383,4293651 3287323,4293697 3287271"
  86. ",4293880 3287128,4293930 3287045,4293938 3286977,4293931 3286901,4293785 3286525"
  87. ",4293775 3286426,4293786 3286358,4293821 3286294,4294072 3286076,4294134 3285986)",
  88. "LINESTRING(4293586 3290439,4293785 3289660,4294317 3289420,4293654 3288652,4293960 3288044"
  89. ",4293633 3287459,4293786 3286358,4294134 3285986)", 250);
  90. /* TODO fix this
  91. test_geometry<test::wrapped_boost_array<P, 10> >(
  92. "LINESTRING(0 0,5 5,7 5,10 10)",
  93. "LINESTRING(0 0,5 5,7 5,10 10)", 1.0);
  94. */
  95. /*
  96. Above can be checked in PostGIS by:
  97. select astext(ST_Simplify(geomfromtext('LINESTRING(0 0, 5 5, 10 10)'),1.0)) as simplified
  98. union all select astext(ST_Simplify(geomfromtext('LINESTRING(0 0, 5 5, 6 5, 10 10)'),1.0))
  99. etc
  100. */
  101. test_geometry<bg::model::polygon<P> >(
  102. "POLYGON((4 0,8 2,8 7,4 9,0 7,0 2,2 1,4 0))",
  103. "POLYGON((4 0,8 2,8 7,4 9,0 7,0 2,4 0))", 1.0);
  104. test_geometry<bg::model::polygon<P> >(
  105. "POLYGON((4 0,8 2,8 7,4 9,0 7,0 2,2 1,4 0),(7 3,7 6,1 6,1 3,4 3,7 3))",
  106. "POLYGON((4 0,8 2,8 7,4 9,0 7,0 2,4 0),(7 3,7 6,1 6,1 3,7 3))", 1.0);
  107. // Closing point should be simplified away
  108. test_geometry<bg::model::polygon<P> >(
  109. "POLYGON((1 0,0 0,0 4,4 4,4 0,1 0))",
  110. "POLYGON((0 0,0 4,4 4,4 0,0 0))", 0.1);
  111. // Section around closing point should be simplified away
  112. test_geometry<bg::model::polygon<P> >(
  113. "POLYGON((5 0,4 0,3 0,2 0,1 0,0 0,0 5,5 5,5 0))",
  114. "POLYGON((5 0,0 0,0 5,5 5,5 0))", 0.1);
  115. // Manually rotate this WKT over all the 5 redundant points at closing area
  116. test_geometry<bg::model::polygon<P> >(
  117. "POLYGON((4 0,3 0,2 0,1 0,0 0,0 5,5 5,5 0,4 0))",
  118. "POLYGON((0 0,0 5,5 5,5 0,0 0))", 0.1);
  119. test_geometry<bg::model::polygon<P> >(
  120. "POLYGON((3 0,2 0,1 0,0 0,0 5,5 5,5 0,4 0,3 0))",
  121. "POLYGON((0 0,0 5,5 5,5 0,0 0))", 0.1);
  122. test_geometry<bg::model::polygon<P> >(
  123. "POLYGON((2 0,1 0,0 0,0 5,5 5,5 0,4 0,3 0,2 0))",
  124. "POLYGON((0 0,0 5,5 5,5 0,0 0))", 0.1);
  125. test_geometry<bg::model::polygon<P> >(
  126. "POLYGON((1 0,0 0,0 5,5 5,5 0,4 0,3 0,2 0,1 0))",
  127. "POLYGON((0 0,0 5,5 5,5 0,0 0))", 0.1);
  128. test_geometry<bg::model::polygon<P> >(
  129. "POLYGON((0 0,0 5,5 5,5 0,4 0,3 0,2 0,1 0,0 0))",
  130. "POLYGON((0 0,0 5,5 5,5 0,0 0))", 0.1);
  131. // Test wither all collinear points in between are simplified away
  132. // First approach did select one of them because it was the end of the
  133. // "closing area". Now the opposite is taken, which is farthest and never
  134. // collinear
  135. test_geometry<bg::model::polygon<P> >(
  136. "POLYGON((2 0,1 0,0 0,0 1,0 2,0 3,0 4,1 4,2 4,3 4,4 4,4 3,4 2,4 1,4 0,3 0,2 0))",
  137. "POLYGON((0 0,0 4,4 4,4 0,0 0))", 1.0);
  138. // Test simplifying away one of the sides (collinear), then closing point
  139. // and finally the whole polygon
  140. std::string const near_triangle = "POLYGON((0.55 0.55,1 0,0.5 0,0 0,0 1,0.55 0.55))";
  141. test_geometry<bg::model::polygon<P> >(near_triangle,
  142. "POLYGON((0.55 0.55,1 0,0 0,0 1,0.55 0.55))", 0.01);
  143. test_geometry<bg::model::polygon<P> >(near_triangle,
  144. "POLYGON((1 0,0 0,0 1,1 0))", 0.1);
  145. // 0.9 should still result in a simplified polygon
  146. test_geometry<bg::model::polygon<P> >(near_triangle,
  147. "POLYGON((1 0,0 0,0 1,1 0))", 0.9);
  148. test_geometry<bg::model::polygon<P> >(near_triangle,
  149. "POLYGON(())", 1.1);
  150. // Test simplifying away the closing point, and closing it explicitly
  151. std::string const salamina = "POLYGON((2616131.59828 4579307.29099,2616687.86177 4579151.05325,2618172.52982 4578718.79836,2618728.79332 4578522.73574,2620336.91468 4577424.54293,2620522.48427 4576992.50129,2621264.76264 4569815.3917,2621140.75272 4569502.07546,2620491.53745 4568208.96982,2620151.34509 4567855.90928,2612606.55528 4562800.36094,2611833.3301 4562291.50023,2611369.5731 4562174.16117,2610225.54269 4562408.69959,2605896.21638 4564367.29512,2605494.13038 4564641.6634,2605277.94792 4566288.44857,2606019.89233 4569423.46562,2609050.12019 4577424.54293,2614337.90732 4579347.12775,2615296.7021 4579543.34723,2616131.59828 4579307.29099))";
  152. test_geometry<bg::model::polygon<P> >(salamina, 196318951.5097456, 100);
  153. test_geometry<bg::model::polygon<P> >(salamina, 194471472.35804176, 200);
  154. test_geometry<bg::model::polygon<P> >(salamina, 191735337.33374023, 500);
  155. test_geometry<bg::model::polygon<P> >(salamina, 186593693.18401337, 1000);
  156. test_geometry<bg::model::polygon<P> >(salamina, 181448561.04094696, 2000);
  157. test_geometry<bg::model::polygon<P> >(salamina, 141965392.92240524, 5000);
  158. // Interior ring (sized ~ 1) should be simplified away (distance 5)
  159. test_geometry<bg::model::polygon<P> >(
  160. "POLYGON((0 0,0 10,10 10,10 0,0 0),(5 5,6 6,5 6,5 5))",
  161. "POLYGON((0 0,0 10,10 10,10 0,0 0))", 5.0);
  162. // // Non-closed version
  163. // test_geometry<bg::model::polygon<P, true, false> >(
  164. // "POLYGON((1 0,0 0,0 4,4 4,4 0))",
  165. // "POLYGON((0 0,0 4,4 4,4 0))", 0.1);
  166. {
  167. // Test with explicit strategy
  168. typedef bg::strategy::simplify::douglas_peucker
  169. <
  170. P,
  171. bg::strategy::distance::projected_point<double>
  172. > dp;
  173. test_geometry<bg::model::linestring<P> >(
  174. "LINESTRING(0 0,5 5,10 10)",
  175. "LINESTRING(0 0,10 10)", 1.0, dp());
  176. }
  177. // POINT: check compilation
  178. test_geometry<P>(
  179. "POINT(0 0)",
  180. "POINT(0 0)", 1.0);
  181. // RING: check compilation and behaviour
  182. test_geometry<bg::model::ring<P> >(
  183. "POLYGON((4 0,8 2,8 7,4 9,0 7,0 2,2 1,4 0))",
  184. "POLYGON((4 0,8 2,8 7,4 9,0 7,0 2,4 0))", 1.0);
  185. #ifdef TEST_PULL89
  186. test_with_ax<bg::model::linestring<P> >(
  187. "LINESTRING(0 0,120 6,80 10,200 0)",
  188. "LINESTRING(0 0,80 10,200 0)", 10, 7);
  189. #endif
  190. }
  191. template <typename P>
  192. void test_zigzag()
  193. {
  194. static const std::string zigzag = "LINESTRING(0 10,1 7,1 9,2 6,2 7,3 4,3 5,5 3,4 5,6 2,6 3,9 1,7 3,10 1,9 2,12 1,10 2,13 1,11 2,14 1,12 2,16 1,14 2,17 3,15 3,18 4,16 4,19 5,17 5,20 6,18 6,21 8,19 7,21 9,19 8,21 10,19 9,21 11,19 10,20 13,19 11)";
  195. static const std::string expected100 = "LINESTRING(0 10,3 4,5 3,4 5,6 2,9 1,7 3,10 1,9 2,16 1,14 2,17 3,15 3,18 4,16 4,19 5,17 5,21 8,19 7,21 9,19 8,21 10,19 9,21 11,19 10,20 13,19 11)";
  196. static const std::string expected150 = "LINESTRING(0 10,6 2,16 1,14 2,21 8,19 7,21 9,19 8,21 10,19 9,20 13,19 11)";
  197. static const std::string expected200 = "LINESTRING(0 10,6 2,16 1,14 2,21 8,19 7,20 13,19 11)";
  198. static const std::string expected225 = "LINESTRING(0 10,6 2,16 1,21 8,19 11)";
  199. test_geometry<bg::model::linestring<P> >(zigzag, expected100, 1.0001);
  200. test_geometry<bg::model::linestring<P> >(zigzag, expected150, 1.5001);
  201. test_geometry<bg::model::linestring<P> >(zigzag, expected200, 2.0001);
  202. test_geometry<bg::model::linestring<P> >(zigzag, expected225, 2.25); // should be larger than sqrt(5)=2.236
  203. #ifdef TEST_PULL89
  204. // This should work (results might vary but should have LESS points then expected above
  205. // Small xtd, larger adt,
  206. test_with_ax<bg::model::linestring<P> >(zigzag, expected100, 1.0001, 1.0001);
  207. test_with_ax<bg::model::linestring<P> >(zigzag, expected150, 1.5001, 1.0001);
  208. test_with_ax<bg::model::linestring<P> >(zigzag, expected200, 2.0001, 1.0001);
  209. test_with_ax<bg::model::linestring<P> >(zigzag, expected225, 2.25, 1.0001);
  210. #endif
  211. }
  212. template <typename P>
  213. void test_3d()
  214. {
  215. test_geometry<bg::model::linestring<P> >(
  216. "LINESTRING(0 0 0,1 1 1,2 2 0)",
  217. "LINESTRING(0 0 0,2 2 0)", 1.0001);
  218. test_geometry<bg::model::linestring<P> >(
  219. "LINESTRING(0 0 0,1 1 1,2 2 0)",
  220. "LINESTRING(0 0 0,1 1 1,2 2 0)", 0.9999);
  221. }
  222. template <typename P>
  223. void test_spherical()
  224. {
  225. test_geometry<bg::model::linestring<P> >(
  226. "LINESTRING(4.1 52.1,4.2 52.2,4.3 52.3)",
  227. "LINESTRING(4.1 52.1,4.3 52.3)", 0.01);
  228. }
  229. int test_main(int, char* [])
  230. {
  231. // Integer compiles, but simplify-process fails (due to distances)
  232. //test_all<bg::model::d2::point_xy<int> >();
  233. test_all<bg::model::d2::point_xy<double> >();
  234. #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
  235. test_all<bg::model::d2::point_xy<float> >();
  236. test_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
  237. test_spherical<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  238. test_zigzag<bg::model::d2::point_xy<double> >();
  239. #if defined(HAVE_TTMATH)
  240. test_all<bg::model::d2::point_xy<ttmath_big> >();
  241. test_spherical<bg::model::point<ttmath_big, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  242. #endif
  243. #endif
  244. return 0;
  245. }