dsv_multi.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <sstream>
  10. #include <geometry_test_common.hpp>
  11. #include <boost/geometry/geometries/geometries.hpp>
  12. #include <boost/geometry/io/dsv/write.hpp>
  13. #include <boost/geometry/io/wkt/read.hpp>
  14. template <typename Geometry>
  15. void test_dsv(std::string const& wkt, std::string const& expected, bool json = false)
  16. {
  17. Geometry geometry;
  18. bg::read_wkt(wkt, geometry);
  19. std::ostringstream out;
  20. if (json)
  21. {
  22. out << bg::dsv(geometry, ", ", "[", "]", ", ", "[ ", " ]", ", ");
  23. }
  24. else
  25. {
  26. out << bg::dsv(geometry);
  27. }
  28. BOOST_CHECK_EQUAL(out.str(), expected);
  29. }
  30. template <typename T>
  31. void test_all()
  32. {
  33. using namespace boost::geometry;
  34. typedef model::point<T, 2, cs::cartesian> point_type;
  35. typedef model::multi_point<point_type> mpoint;
  36. typedef model::multi_linestring<model::linestring<point_type> > mline;
  37. typedef model::multi_polygon<model::polygon<point_type> > mpoly;
  38. test_dsv<mpoint>
  39. (
  40. "multipoint((1 2),(3 4))",
  41. "((1, 2), (3, 4))"
  42. );
  43. test_dsv<mline>
  44. (
  45. "multilinestring((1 1,2 2,3 3),(4 4,5 5,6 6))",
  46. "(((1, 1), (2, 2), (3, 3)), ((4, 4), (5, 5), (6, 6)))"
  47. );
  48. test_dsv<mpoly>
  49. (
  50. // Multi with 2 poly's, first has hole, second is triangle
  51. "multipolygon(((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1)),((5 5,6 5,5 6,5 5)))",
  52. "((((0, 0), (0, 4), (4, 4), (4, 0), (0, 0)), ((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))), (((5, 5), (6, 5), (5, 6), (5, 5))))"
  53. );
  54. // http://geojson.org/geojson-spec.html#id5
  55. test_dsv<mpoint>
  56. (
  57. "multipoint((1 2),(3 4))",
  58. "[ [1, 2], [3, 4] ]",
  59. true
  60. );
  61. // http://geojson.org/geojson-spec.html#id6
  62. test_dsv<mline>
  63. (
  64. "multilinestring((1 1,2 2,3 3),(4 4,5 5,6 6))",
  65. "[ [ [1, 1], [2, 2], [3, 3] ], [ [4, 4], [5, 5], [6, 6] ] ]",
  66. true
  67. );
  68. // http://geojson.org/geojson-spec.html#id7
  69. test_dsv<mpoly>
  70. (
  71. "multipolygon(((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1)),((5 5,6 5,5 6,5 5)))",
  72. "[ [ [ [0, 0], [0, 4], [4, 4], [4, 0], [0, 0] ], [ [1, 1], [1, 2], [2, 2], [2, 1], [1, 1] ] ], [ [ [5, 5], [6, 5], [5, 6], [5, 5] ] ] ]",
  73. true
  74. );
  75. }
  76. int test_main(int, char* [])
  77. {
  78. test_all<double>();
  79. test_all<int>();
  80. return 0;
  81. }