num_geometries.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_geometries
  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_geometries.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_geometries
  30. {
  31. static inline void apply(Geometry const& geometry,
  32. std::size_t expected)
  33. {
  34. std::size_t detected = bg::num_geometries(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_geometries<point>::apply("POINT(0 0)", 1);
  51. }
  52. BOOST_AUTO_TEST_CASE( test_segment )
  53. {
  54. test_num_geometries<segment>::apply("SEGMENT(0 0,1 1)", 1);
  55. }
  56. BOOST_AUTO_TEST_CASE( test_box )
  57. {
  58. test_num_geometries<box>::apply("BOX(0 0,1 1)", 1);
  59. }
  60. BOOST_AUTO_TEST_CASE( test_linestring )
  61. {
  62. test_num_geometries<linestring>::apply("LINESTRING(0 0,1 1,2 2)", 1);
  63. }
  64. BOOST_AUTO_TEST_CASE( test_multipoint )
  65. {
  66. typedef test_num_geometries<multi_point> tester;
  67. tester::apply("MULTIPOINT()", 0);
  68. tester::apply("MULTIPOINT(0 0)", 1);
  69. tester::apply("MULTIPOINT(0 0,0 0)", 2);
  70. tester::apply("MULTIPOINT(0 0,0 0,1 1)", 3);
  71. }
  72. BOOST_AUTO_TEST_CASE( test_multilinestring )
  73. {
  74. typedef test_num_geometries<multi_linestring> tester;
  75. tester::apply("MULTILINESTRING()", 0);
  76. tester::apply("MULTILINESTRING((0 0,1 0))", 1);
  77. tester::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", 2);
  78. tester::apply("MULTILINESTRING((),(),(0 0,1 0))", 3);
  79. }
  80. BOOST_AUTO_TEST_CASE( test_ring )
  81. {
  82. test_num_geometries<ring>::apply("POLYGON((0 0,1 0,0 1,0 0))", 1);
  83. }
  84. BOOST_AUTO_TEST_CASE( test_polygon )
  85. {
  86. typedef test_num_geometries<polygon> tester;
  87. tester::apply("POLYGON((0 0,10 0,0 10,0 0))", 1);
  88. tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 1))", 1);
  89. 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))", 1);
  90. }
  91. BOOST_AUTO_TEST_CASE( test_multipolygon )
  92. {
  93. typedef test_num_geometries<multi_polygon> tester;
  94. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", 1);
  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)))", 1);
  96. 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);
  97. 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)))", 2);
  98. }
  99. BOOST_AUTO_TEST_CASE( test_variant )
  100. {
  101. typedef boost::variant
  102. <
  103. linestring, multi_linestring
  104. > variant_geometry_type;
  105. typedef test_num_geometries<variant_geometry_type> tester;
  106. linestring ls;
  107. bg::read_wkt("LINESTRING(0 0,1 1,2 2)", ls);
  108. multi_linestring mls;
  109. bg::read_wkt("MULTILINESTRING((0 0,1 1,2 2),(3 3,4 4),(5 5,6 6,7 7,8 8))",
  110. mls);
  111. variant_geometry_type variant_geometry;
  112. variant_geometry = ls;
  113. tester::apply(variant_geometry, 1);
  114. variant_geometry = mls;
  115. tester::apply(variant_geometry, 3);
  116. }