1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // (Unit) Test
3
4 // Copyright (c) 2018 Barend Gehrels, Amsterdam, the Netherlands.
5
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
10 #include <boost/geometry.hpp>
11 #include <geometry_test_common.hpp>
12
13 #include <boost/foreach.hpp>
14
15 #include <iomanip>
16
17 #if defined(TEST_WITH_SVG)
18 # include <boost/geometry/io/svg/svg_mapper.hpp>
19 #endif
20
21
22
23 template <typename MultiPolygon>
read_from_file(std::string const & filename)24 std::string read_from_file(std::string const& filename)
25 {
26 MultiPolygon mp;
27 std::ifstream in(filename.c_str());
28 while (in.good())
29 {
30 std::string line;
31 std::getline(in, line);
32 if (! line.empty())
33 {
34 typename boost::range_value<MultiPolygon>::type pol;
35 bg::read_wkt(line, pol);
36 mp.push_back(pol);
37 }
38 }
39 std::ostringstream out;
40 if (! mp.empty())
41 {
42 out << std::fixed << std::setprecision(19) << bg::wkt(mp);
43 }
44
45 BOOST_CHECK(! out.str().empty());
46
47 return out.str();
48 }
49
50
51 template <typename MultiPolygon>
test_one(std::string const & caseid,std::string const & wkt,double distance_in_meters,double expected_area_ratio,double expected_perimeter_ratio,std::size_t expected_polygon_count=0,std::size_t expected_interior_count=0,std::size_t expected_point_count=0)52 void test_one(std::string const& caseid, std::string const& wkt,
53 double distance_in_meters,
54 double expected_area_ratio, double expected_perimeter_ratio,
55 std::size_t expected_polygon_count = 0,
56 std::size_t expected_interior_count = 0,
57 std::size_t expected_point_count = 0)
58 {
59 boost::ignore_unused(caseid);
60
61 MultiPolygon geometry, simplified;
62 bg::read_wkt(wkt, geometry);
63 bg::correct(geometry);
64 bg::simplify(geometry, simplified, distance_in_meters);
65
66 double const area_ratio = bg::area(simplified) / bg::area(geometry);
67 double const perimeter_ratio = bg::perimeter(simplified) / bg::perimeter(geometry);
68
69 BOOST_CHECK_CLOSE(perimeter_ratio, expected_perimeter_ratio, 0.01);
70 BOOST_CHECK_CLOSE(area_ratio, expected_area_ratio, 0.01);
71 BOOST_CHECK_EQUAL(expected_polygon_count, boost::size(simplified));
72 BOOST_CHECK_EQUAL(expected_interior_count, bg::num_interior_rings(simplified));
73 BOOST_CHECK_EQUAL(expected_point_count, bg::num_points(simplified));
74
75 // To add new tests, this is convenient and write the test itself:
76 // std::cout << "test_one<mp>(\"" << caseid << "\", " << caseid
77 // << ", " << distance_in_meters
78 // << std::setprecision(6)
79 // << ", " << area_ratio
80 // << ", " << perimeter_ratio
81 // << ", " << boost::size(simplified)
82 // << ", " << bg::num_interior_rings(simplified)
83 // << ", " << bg::num_points(simplified)
84 // << ");"
85 // << std::endl;
86
87 #if defined(TEST_WITH_SVG)
88 {
89 typedef typename boost::range_value<MultiPolygon>::type polygon;
90
91 std::ostringstream filename;
92 filename << "simplify_" << caseid << "_" << distance_in_meters << ".svg";
93
94 std::ofstream svg(filename.str().c_str());
95
96 bg::svg_mapper
97 <
98 typename bg::point_type<MultiPolygon>::type
99 > mapper(svg, 1200, 800);
100 mapper.add(geometry);
101 mapper.add(simplified);
102
103 mapper.map(geometry, "fill-opacity:0.5;fill:rgb(153,204,0);"
104 "stroke:rgb(153,204,0);stroke-width:1");
105 BOOST_FOREACH(polygon const& pol, simplified)
106 {
107 mapper.map(pol,
108 bg::area(pol) > 0 ? "fill:none;stroke:rgb(255,0,0);stroke-width:1"
109 : "fill:none;stroke:rgb(255,0,255);stroke-width:1");
110 }
111 }
112 #endif
113 }
114
115
116 template <bool Clockwise, typename P>
test_all()117 void test_all()
118 {
119 typedef bg::model::polygon<P, Clockwise> polygon;
120 typedef bg::model::multi_polygon<polygon> mp;
121
122 // The unit test uses countries originally added for buffer unit test
123 std::string base_folder = "buffer/data/";
124
125 // Verify for Greece, Italy, Netherlands, Norway and UK
126 std::string gr = read_from_file<mp>(base_folder + "gr.wkt");
127 std::string it = read_from_file<mp>(base_folder + "it.wkt");
128 std::string nl = read_from_file<mp>(base_folder + "nl.wkt");
129 std::string no = read_from_file<mp>(base_folder + "no.wkt");
130 std::string uk = read_from_file<mp>(base_folder + "uk.wkt");
131
132 // Gradually simplify more aggresively.
133 // Area ratio (first) can increase or decrease
134 // Perimeter ratio (second) should decrease.
135 // Polygons, interior rings, points should decrease
136 test_one<mp>("gr", gr, 100, 0.999905, 0.999758, 68, 0, 2520);
137 test_one<mp>("gr", gr, 200, 0.999773, 0.998865, 68, 0, 2019);
138 test_one<mp>("gr", gr, 500, 0.999026, 0.995931, 68, 0, 1468);
139 test_one<mp>("gr", gr, 1000, 0.997782, 0.991475, 68, 0, 1132);
140 test_one<mp>("gr", gr, 2000, 0.994448, 0.9793, 65, 0, 854);
141 test_one<mp>("gr", gr, 5000, 0.979743, 0.910266, 50, 0, 471);
142 test_one<mp>("gr", gr, 10000, 0.968349, 0.778863, 28, 0, 245);
143 test_one<mp>("gr", gr, 20000, 0.961943, 0.607009, 10, 0, 97); // Many islands disappear
144
145 test_one<mp>("it", it, 100, 1.00001, 0.999813, 22, 1, 1783);
146 test_one<mp>("it", it, 200, 1.00009, 0.9991, 22, 1, 1406);
147 test_one<mp>("it", it, 500, 1.00019, 0.996848, 22, 1, 1011);
148 test_one<mp>("it", it, 1000, 1.00041, 0.99294, 22, 1, 749);
149 test_one<mp>("it", it, 2000, 1.00086, 0.985144, 22, 1, 546);
150 test_one<mp>("it", it, 5000, 1.00147, 0.93927, 11, 1, 283);
151 test_one<mp>("it", it, 10000, 1.01089, 0.882198, 4, 1, 153);
152 test_one<mp>("it", it, 20000, 1.00893, 0.828774, 4, 0, 86); // San Marino disappears
153
154 test_one<mp>("nl", nl, 100, 0.999896, 0.999804, 8, 0, 789);
155 test_one<mp>("nl", nl, 200, 0.999733, 0.999095, 8, 0, 633);
156 test_one<mp>("nl", nl, 500, 0.999423, 0.996313, 8, 0, 436);
157 test_one<mp>("nl", nl, 1000, 0.997893, 0.991951, 8, 0, 331);
158 test_one<mp>("nl", nl, 2000, 0.996129, 0.981998, 8, 0, 234);
159 test_one<mp>("nl", nl, 5000, 0.986128, 0.896, 5, 0, 132);
160 test_one<mp>("nl", nl, 10000, 0.973917, 0.832522, 4, 0, 75);
161 test_one<mp>("nl", nl, 20000, 0.970675, 0.739275, 3, 0, 40);
162
163 test_one<mp>("no", no, 100, 0.999966, 0.999975, 95, 0, 7650);
164 test_one<mp>("no", no, 200, 0.999812, 0.999731, 95, 0, 6518);
165 test_one<mp>("no", no, 500, 0.99929, 0.998092, 95, 0, 4728);
166 test_one<mp>("no", no, 1000, 0.998473, 0.994075, 95, 0, 3524);
167 test_one<mp>("no", no, 2000, 0.996674, 0.985863, 92, 0, 2576);
168 test_one<mp>("no", no, 5000, 0.99098, 0.965689, 87, 0, 1667);
169 test_one<mp>("no", no, 10000, 0.978207, 0.906525, 69, 0, 1059);
170 test_one<mp>("no", no, 20000, 0.955223, 0.786546, 38, 0, 593);
171
172 test_one<mp>("uk", uk, 100, 0.999942, 0.999878, 48, 0, 3208);
173 test_one<mp>("uk", uk, 200, 0.999843, 0.999291, 48, 0, 2615);
174 test_one<mp>("uk", uk, 500, 0.999522, 0.996888, 48, 0, 1885);
175 test_one<mp>("uk", uk, 1000, 0.999027, 0.992306, 48, 0, 1396);
176 test_one<mp>("uk", uk, 2000, 0.998074, 0.983839, 47, 0, 1032);
177 test_one<mp>("uk", uk, 5000, 0.991901, 0.943496, 35, 0, 611);
178 test_one<mp>("uk", uk, 10000, 0.990039, 0.871969, 23, 0, 359);
179 test_one<mp>("uk", uk, 20000, 0.979171, 0.737577, 11, 0, 193);
180
181 }
182
test_main(int,char * [])183 int test_main(int, char* [])
184 {
185 test_all<true, bg::model::point<double, 2, bg::cs::cartesian> >();
186 return 0;
187 }
188
189