1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
4 
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Multipolygon DP simplification example from the mailing list discussion
10 // about the DP algorithm issue:
11 // http://lists.osgeo.org/pipermail/ggl/2011-September/001533.html
12 
13 #include <boost/geometry.hpp>
14 #include <boost/geometry/geometries/point_xy.hpp>
15 #include <boost/geometry/geometries/polygon.hpp>
16 #include <boost/geometry/geometries/ring.hpp>
17 #include <boost/geometry/geometries/multi_polygon.hpp>
18 #include <boost/foreach.hpp>
19 
main()20 int main()
21 {
22     typedef boost::geometry::model::d2::point_xy<double> point_xy;
23     typedef boost::geometry::model::polygon<point_xy > polygon;
24     typedef boost::geometry::model::ring<point_xy > ring;
25     typedef boost::geometry::model::multi_polygon<polygon > multi_polygon;
26 
27     multi_polygon original_1;
28     multi_polygon simplified_1;
29 
30     // Values between 0..1 and simplified with 1/2048
31     boost::geometry::read_wkt("MULTIPOLYGON(((0.561648 1,1 1,1 0,0.468083 0,0.52758 0.00800554,0.599683 0.0280924,0.601611 0.265374,0.622693 0.316765,0.69507 0.357497,0.695623 0.429711,0.655111 0.502298,0.696467 0.543147,0.840712 0.593546,0.882583 0.66546,0.852357 0.748213,0.84264 0.789567,0.832667 0.841202,0.832667 0.841202,0.740538 0.873004,0.617349 0.905045,0.566576 0.977697,0.561648 1)),((0 0.801979,0.0308575 0.786234,0.0705513 0.631135,0.141616 0.527248,0.233985 0.505872,0.264777 0.526263,0.336631 0.505009,0.356603 0.422321,0.355803 0.350038,0.375252 0.205364,0.415206 0.0709182,0.45479 0,0 0,0 0,0 0.801979)))", original_1);
32 
33     std::cout << "Original: \n" << boost::geometry::num_points(original_1) << " points.\n\n";
34 
35     boost::geometry::simplify(original_1, simplified_1, 1.0 / 2048.0);
36 
37     std::cout << "Polygon with values 0..1 and simplified with 1.0 / 2048.0 \n"
38         << "Result: \n" << boost::geometry::wkt(simplified_1) << "\n" << boost::geometry::num_points(simplified_1) << " points.\n\n";
39 
40     // Multiply every points from original_1 by 2047
41     multi_polygon original_2(original_1);
42     BOOST_FOREACH(polygon& p, original_2)
43     {
44         BOOST_FOREACH(point_xy& pt, p.outer())
45         {
46             pt.x(pt.x() * 2047.0);
47             pt.y(pt.y() * 2047.0);
48         }
49 
50         BOOST_FOREACH(ring& r, p.inners())
51         {
52             BOOST_FOREACH(point_xy& pt, r)
53             {
54                 pt.x(pt.x() * 2047.0);
55                 pt.y(pt.y() * 2047.0);
56             }
57         }
58     }
59 
60     multi_polygon simplified_2;
61     boost::geometry::simplify(original_2, simplified_2, 1.0);
62     std::cout << "Same values but multiplied by 2047.0 and simplified with 1.0\n"
63         << "Result: \n" << boost::geometry::wkt(simplified_2) << "\n" << boost::geometry::num_points(simplified_2) << " points.\n";
64 
65     return 0;
66 }
67 
68