• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6 
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Linestring Polygon Overlay Example
12 
13 // NOTE: this example is obsolete. Boost.Geometry can now
14 // overlay linestrings/polygons.
15 // This sample will be removed in next version.
16 
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 
22 #include <boost/foreach.hpp>
23 
24 
25 #include <boost/geometry/geometry.hpp>
26 #include <boost/geometry/geometries/linestring.hpp>
27 #include <boost/geometry/geometries/point_xy.hpp>
28 #include <boost/geometry/geometries/polygon.hpp>
29 #include <boost/geometry/geometries/adapted/c_array.hpp>
30 
31 #if defined(HAVE_SVG)
32 #  include <boost/geometry/io/svg/svg_mapper.hpp>
33 #endif
34 
35 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian);
36 
37 
main(void)38 int main(void)
39 {
40     namespace bg = boost::geometry;
41 
42     typedef bg::model::d2::point_xy<double> point_2d;
43 
44     bg::model::linestring<point_2d> ls;
45     {
46         const double c[][2] = { {0, 1}, {2, 5}, {5, 3} };
47         bg::assign_points(ls, c);
48     }
49 
50     bg::model::polygon<point_2d> p;
51     {
52         const double c[][2] = { {3, 0}, {0, 3}, {4, 5}, {3, 0} };
53         bg::assign_points(p, c);
54     }
55     bg::correct(p);
56 
57 #if defined(HAVE_SVG)
58     // Create SVG-mapper
59     std::ofstream stream("05_b_overlay_linestring_polygon_example.svg");
60     bg::svg_mapper<point_2d> svg(stream, 500, 500);
61     // Determine extend by adding geometries
62     svg.add(p);
63     svg.add(ls);
64     // Map geometries
65     svg.map(ls, "opacity:0.6;stroke:rgb(255,0,0);stroke-width:2;");
66     svg.map(p, "opacity:0.6;fill:rgb(0,0,255);");
67 #endif
68 
69     // Calculate intersection points (turn points)
70     typedef bg::segment_ratio_type<point_2d, bg::detail::no_rescale_policy>::type segment_ratio;
71     typedef bg::detail::overlay::turn_info<point_2d, segment_ratio> turn_info;
72     std::vector<turn_info> turns;
73     bg::detail::get_turns::no_interrupt_policy policy;
74     bg::detail::no_rescale_policy rescale_policy;
75     bg::get_turns<false, false, bg::detail::overlay::assign_null_policy>(ls, p, rescale_policy, turns, policy);
76 
77     std::cout << "Intersection of linestring/polygon" << std::endl;
78     BOOST_FOREACH(turn_info const& turn, turns)
79     {
80         std::string action = "intersecting";
81         if (turn.operations[0].operation
82                 == bg::detail::overlay::operation_intersection)
83         {
84             action = "entering";
85         }
86         else if (turn.operations[0].operation
87                 == bg::detail::overlay::operation_union)
88         {
89             action = "leaving";
90 
91         }
92         std::cout << action << " polygon at " << bg::dsv(turn.point) << std::endl;
93 #if defined(HAVE_SVG)
94         svg.map(turn.point, "fill:rgb(255,128,0);stroke:rgb(0,0,100);stroke-width:1");
95         svg.text(turn.point, action, "fill:rgb(0,0,0);font-family:Arial;font-size:10px");
96 #endif
97     }
98 
99     return 0;
100 }
101