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 // Polygon Example
12 
13 #include <algorithm> // for reverse, unique
14 #include <iostream>
15 #include <string>
16 
17 #include <boost/geometry/geometry.hpp>
18 #include <boost/geometry/geometries/point_xy.hpp>
19 #include <boost/geometry/geometries/polygon.hpp>
20 #include <boost/geometry/geometries/adapted/c_array.hpp>
21 #include <boost/geometry/geometries/multi_polygon.hpp>
22 
BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)23 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
24 
25 
26 std::string boolstr(bool v)
27 {
28     return v ? "true" : "false";
29 }
30 
main(void)31 int main(void)
32 {
33     using namespace boost::geometry;
34 
35     typedef model::d2::point_xy<double> point_2d;
36     typedef model::polygon<point_2d> polygon_2d;
37     typedef model::box<point_2d> box_2d;
38 
39     // Define a polygon and fill the outer ring.
40     // In most cases you will read it from a file or database
41     polygon_2d poly;
42     {
43         const double coor[][2] = {
44             {2.0, 1.3}, {2.4, 1.7}, {2.8, 1.8}, {3.4, 1.2}, {3.7, 1.6},
45             {3.4, 2.0}, {4.1, 3.0}, {5.3, 2.6}, {5.4, 1.2}, {4.9, 0.8}, {2.9, 0.7},
46             {2.0, 1.3} // closing point is opening point
47             };
48         assign_points(poly, coor);
49     }
50 
51     // Polygons should be closed, and directed clockwise. If you're not sure if that is the case,
52     // call the correct algorithm
53     correct(poly);
54 
55     // Polygons can be streamed as text
56     // (or more precisely: as DSV (delimiter separated values))
57     std::cout << dsv(poly) << std::endl;
58 
59     // As with lines, bounding box of polygons can be calculated
60     box_2d b;
61     envelope(poly, b);
62     std::cout << dsv(b) << std::endl;
63 
64     // The area of the polygon can be calulated
65     std::cout << "area: " << area(poly) << std::endl;
66 
67     // And the centroid, which is the center of gravity
68     point_2d cent;
69     centroid(poly, cent);
70     std::cout << "centroid: " << dsv(cent) << std::endl;
71 
72 
73     // The number of points can be requested per ring (using .size())
74     // or per polygon (using num_points)
75     std::cout << "number of points in outer ring: " << poly.outer().size() << std::endl;
76 
77     // Polygons can have one or more inner rings, also called holes, islands, interior rings.
78     // Let's add one
79     {
80         poly.inners().resize(1);
81         model::ring<point_2d>& inner = poly.inners().back();
82 
83         const double coor[][2] = { {4.0, 2.0}, {4.2, 1.4}, {4.8, 1.9}, {4.4, 2.2}, {4.0, 2.0} };
84         assign_points(inner, coor);
85     }
86 
87     correct(poly);
88 
89     std::cout << "with inner ring:" << dsv(poly) << std::endl;
90     // The area of the polygon is changed of course
91     std::cout << "new area of polygon: " << area(poly) << std::endl;
92     centroid(poly, cent);
93     std::cout << "new centroid: " << dsv(cent) << std::endl;
94 
95     // You can test whether points are within a polygon
96     std::cout << "point in polygon:"
97         << " p1: "  << boolstr(within(make<point_2d>(3.0, 2.0), poly))
98         << " p2: "  << boolstr(within(make<point_2d>(3.7, 2.0), poly))
99         << " p3: "  << boolstr(within(make<point_2d>(4.4, 2.0), poly))
100         << std::endl;
101 
102     // As with linestrings and points, you can derive from polygon to add, for example,
103     // fill color and stroke color. Or SRID (spatial reference ID). Or Z-value. Or a property map.
104     // We don't show this here.
105 
106     // Clip the polygon using a box
107     box_2d cb(make<point_2d>(1.5, 1.5), make<point_2d>(4.5, 2.5));
108     typedef std::vector<polygon_2d> polygon_list;
109     polygon_list v;
110 
111     intersection(cb, poly, v);
112     std::cout << "Clipped output polygons" << std::endl;
113     for (polygon_list::const_iterator it = v.begin(); it != v.end(); ++it)
114     {
115         std::cout << dsv(*it) << std::endl;
116     }
117 
118     typedef model::multi_polygon<polygon_2d> polygon_set;
119     polygon_set ps;
120     union_(cb, poly, ps);
121 
122     polygon_2d hull;
123     convex_hull(poly, hull);
124     std::cout << "Convex hull:" << dsv(hull) << std::endl;
125 
126     // If you really want:
127     //   You don't have to use a vector, you can define a polygon with a deque
128     //   You can specify the container for the points and for the inner rings independantly
129 
130     typedef model::polygon<point_2d, true, true, std::deque, std::deque> deque_polygon;
131     deque_polygon poly2;
132     ring_type<deque_polygon>::type& ring = exterior_ring(poly2);
133     append(ring, make<point_2d>(2.8, 1.9));
134     append(ring, make<point_2d>(2.9, 2.4));
135     append(ring, make<point_2d>(3.3, 2.2));
136     append(ring, make<point_2d>(3.2, 1.8));
137     append(ring, make<point_2d>(2.8, 1.9));
138     std::cout << dsv(poly2) << std::endl;
139 
140     return 0;
141 }
142