num_interior_rings.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2014, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_TEST_MODULE
  8. #define BOOST_TEST_MODULE test_num_interior_rings
  9. #endif
  10. #include <iostream>
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/variant/variant.hpp>
  13. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  14. #include <boost/geometry/core/closure.hpp>
  15. #include <boost/geometry/geometries/geometries.hpp>
  16. #include <boost/geometry/io/wkt/wkt.hpp>
  17. #include <boost/geometry/io/dsv/write.hpp>
  18. namespace bg = boost::geometry;
  19. typedef bg::model::point<double, 2, bg::cs::cartesian> point;
  20. typedef bg::model::linestring<point> linestring;
  21. typedef bg::model::segment<point> segment;
  22. typedef bg::model::box<point> box;
  23. typedef bg::model::ring<point> ring;
  24. typedef bg::model::polygon<point> polygon;
  25. typedef bg::model::multi_point<point> multi_point;
  26. typedef bg::model::multi_linestring<linestring> multi_linestring;
  27. typedef bg::model::multi_polygon<polygon> multi_polygon;
  28. template <typename Geometry>
  29. struct test_num_interior_rings
  30. {
  31. static inline void apply(Geometry const& geometry,
  32. std::size_t expected)
  33. {
  34. std::size_t detected = bg::num_interior_rings(geometry);
  35. BOOST_CHECK_MESSAGE( detected == expected,
  36. "Expected: " << expected
  37. << " detected: " << detected
  38. << " wkt: " << bg::wkt(geometry) );
  39. }
  40. static inline void apply(std::string const& wkt,
  41. std::size_t expected)
  42. {
  43. Geometry geometry;
  44. bg::read_wkt(wkt, geometry);
  45. apply(geometry, expected);
  46. }
  47. };
  48. BOOST_AUTO_TEST_CASE( test_point )
  49. {
  50. test_num_interior_rings<point>::apply("POINT(0 0)", 0);
  51. }
  52. BOOST_AUTO_TEST_CASE( test_segment )
  53. {
  54. test_num_interior_rings<segment>::apply("SEGMENT(0 0,1 1)", 0);
  55. }
  56. BOOST_AUTO_TEST_CASE( test_box )
  57. {
  58. test_num_interior_rings<box>::apply("BOX(0 0,1 1)", 0);
  59. }
  60. BOOST_AUTO_TEST_CASE( test_linestring )
  61. {
  62. test_num_interior_rings<linestring>::apply("LINESTRING(0 0,1 1)", 0);
  63. }
  64. BOOST_AUTO_TEST_CASE( test_multipoint )
  65. {
  66. test_num_interior_rings<multi_point>::apply("MULTIPOINT(0 0,1 1)", 0);
  67. }
  68. BOOST_AUTO_TEST_CASE( test_multilinestring )
  69. {
  70. test_num_interior_rings
  71. <
  72. multi_linestring
  73. >::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", 0);
  74. }
  75. BOOST_AUTO_TEST_CASE( test_ring )
  76. {
  77. typedef test_num_interior_rings<ring> tester;
  78. tester::apply("POLYGON((0 0,1 0,0 1,0 0))", 0);
  79. tester::apply("POLYGON((0 0,1 0,1 0,0 1,0 0))", 0);
  80. }
  81. BOOST_AUTO_TEST_CASE( test_polygon )
  82. {
  83. typedef test_num_interior_rings<polygon> tester;
  84. tester::apply("POLYGON((0 0,10 0,0 10,0 0))", 0);
  85. tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 2,1 1))", 1);
  86. tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5))", 2);
  87. }
  88. BOOST_AUTO_TEST_CASE( test_multipolygon )
  89. {
  90. typedef test_num_interior_rings<multi_polygon> tester;
  91. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))", 0);
  92. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", 1);
  93. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)))", 2);
  94. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101)))", 2);
  95. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101),(105 105,106 105,106 106,105 106,105 105)))", 4);
  96. }
  97. BOOST_AUTO_TEST_CASE( test_variant )
  98. {
  99. typedef boost::variant
  100. <
  101. linestring, polygon, multi_polygon
  102. > variant_geometry_type;
  103. typedef test_num_interior_rings<variant_geometry_type> tester;
  104. linestring ls;
  105. bg::read_wkt("LINESTRING(0 0,1 1,2 2)", ls);
  106. polygon poly;
  107. bg::read_wkt("POLYGON((0 0,0 10,10 10,10 0,0 0),(1 1,9 1,9 9,1 9,1 1))", poly);
  108. multi_polygon mpoly;
  109. bg::read_wkt("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0),(1 1,2 1,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)))", mpoly);
  110. variant_geometry_type variant_geometry;
  111. variant_geometry = ls;
  112. tester::apply(variant_geometry, 0);
  113. variant_geometry = poly;
  114. tester::apply(variant_geometry, 1);
  115. variant_geometry = mpoly;
  116. tester::apply(variant_geometry, 2);
  117. }