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