]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/doxy/doxygen_input/sourcecode/doxygen_1.cpp
3 // Boost.Geometry (aka GGL, Generic Geometry Library)
5 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
6 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
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)
11 // Doxygen Examples, referred to from the sources
13 #include <boost/tuple/tuple.hpp>
16 // We deliberately mix float/double's here so turn off warning
17 #pragma warning( disable : 4244 )
18 #endif // defined(_MSC_VER)
20 #include <boost/geometry/geometry.hpp>
21 #include <boost/geometry/geometries/register/point.hpp>
22 #include <boost/geometry/geometries/geometries.hpp>
24 #include <boost/geometry/io/wkt/wkt.hpp>
26 // All functions below are referred to in the documentation of Boost.Geometry
28 void example_area_polygon()
31 //` Calculate the area of a polygon
32 boost::geometry::polygon
<boost::geometry::point_xy
<double> > poly
; /*< Declare >*/
33 boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly
); /*< Fill, in this case with WKT >*/
34 double area
= boost::geometry::area(poly
); /*< Calculate area >*/
37 //[area_polygon_spherical
38 //` Calculate the area of a *spherical* polygon
39 namespace bg
= boost::geometry
;
40 bg::polygon
<bg::point
<float, 2, bg::cs::spherical
<bg::degree
> > > sph_poly
;
41 bg::read_wkt("POLYGON((0 0,0 45,45 0,0 0))", sph_poly
);
42 double area
= bg::area(sph_poly
);
46 void example_as_wkt_point()
48 typedef boost::geometry::point_xy
<double> P
;
50 // Points can be streamed like this:
51 std::cout
<< boost::geometry::dsv
<P
>(p
) << std::endl
;
54 std::cout
<< boost::geometry::dsv(p
) << std::endl
;
56 // or (with extension) like this:
57 std::cout
<< boost::geometry::wkt(p
) << std::endl
;
60 void example_as_wkt_vector()
62 std::vector
<boost::geometry::point_xy
<int> > v
;
63 boost::geometry::read_wkt
<boost::geometry::point_xy
<int> >("linestring(1 1,2 2,3 3,4 4)", std::back_inserter(v
));
65 std::cout
<< boost::geometry::dsv(std::make_pair(v
.begin(), v
.end())) << std::endl
;
69 void example_centroid_polygon()
71 boost::geometry::polygon
<boost::geometry::point_xy
<double> > poly
;
72 boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly
);
73 // Center of polygon might have different type than points of polygon
74 boost::geometry::point_xy
<float> center
;
75 boost::geometry::centroid(poly
, center
);
76 std::cout
<< "Centroid: " << boost::geometry::dsv(center
) << std::endl
;
80 void example_distance_point_point()
82 boost::geometry::point_xy
<double> p1(1, 1);
83 boost::geometry::point_xy
<double> p2(2, 3);
84 std::cout
<< "Distance p1-p2 is "
85 << boost::geometry::distance(p1
, p2
)
86 << " units" << std::endl
;
89 Extension, other coordinate system:
90 // Read 2 Dutch cities from WKT texts (in decimal degrees)
91 boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > a, r;
92 boost::geometry::read_wkt("POINT(4.89222 52.3731)", a);
93 boost::geometry::read_wkt("POINT(4.47917 51.9308)", r);
95 std::cout << "Distance Amsterdam-Rotterdam is "
96 << boost::geometry::distance(a, r) / 1000.0
97 << " kilometers " << std::endl;
101 void example_distance_point_point_strategy()
104 Extension, other coordinate system:
105 typedef boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
107 boost::geometry::read_wkt("POINT(4.89222 52.3731)", a);
108 boost::geometry::read_wkt("POINT(4.47917 51.9308)", r);
110 std::cout << "Distance Amsterdam-Rotterdam is "
111 << boost::geometry::distance(a, r,
112 boost::geometry::strategy::distance::vincenty<LL>() )
114 << " kilometers " << std::endl;
118 void example_from_wkt_point()
120 boost::geometry::point_xy
<int> point
;
121 boost::geometry::read_wkt("Point(1 2)", point
);
122 std::cout
<< point
.x() << "," << point
.y() << std::endl
;
125 void example_from_wkt_output_iterator()
127 std::vector
<boost::geometry::point_xy
<int> > v
;
128 boost::geometry::read_wkt
<boost::geometry::point_xy
<int> >("linestring(1 1,2 2,3 3,4 4)", std::back_inserter(v
));
129 std::cout
<< "vector has " << v
.size() << " coordinates" << std::endl
;
132 void example_from_wkt_linestring()
134 boost::geometry::linestring
<boost::geometry::point_xy
<double> > line
;
135 boost::geometry::read_wkt("linestring(1 1,2 2,3 3,4 4)", line
);
136 std::cout
<< "linestring has " << line
.size() << " coordinates" << std::endl
;
139 void example_from_wkt_polygon()
141 boost::geometry::polygon
<boost::geometry::point_xy
<double> > poly
;
142 boost::geometry::read_wkt("POLYGON((0 0,0 1,1 1,1 0,0 0))", poly
);
143 std::cout
<< "Polygon has " << poly
.outer().size() << " coordinates in outer ring" << std::endl
;
146 void example_point_ll_convert()
149 Extension, other coordinate system:
150 boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > deg(boost::geometry::latitude<>(33.0), boost::geometry::longitude<>(-118.0));
151 boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::radian> > rad;
152 boost::geometry::transform(deg, rad);
154 std::cout << "point in radians: " << rad << std::endl;
158 void example_clip_linestring1()
160 typedef boost::geometry::point_xy
<double> P
;
161 boost::geometry::linestring
<P
> line
;
162 boost::geometry::read_wkt("linestring(1.1 1.1, 2.5 2.1, 3.1 3.1, 4.9 1.1, 3.1 1.9)", line
);
163 boost::geometry::box
<P
> cb(P(1.5, 1.5), P(4.5, 2.5));
164 std::cout
<< "Clipped linestring(s) " << std::endl
;
166 std::vector
<boost::geometry::linestring
<P
> > intersection
;
167 boost::geometry::intersection_inserter
<boost::geometry::linestring
<P
> >(cb
, line
, std::back_inserter(intersection
));
170 void example_clip_linestring2()
172 typedef boost::geometry::point_xy
<double> P
;
173 std::vector
<P
> vector_in
;
174 boost::geometry::read_wkt
<P
>("linestring(1.1 1.1, 2.5 2.1, 3.1 3.1, 4.9 1.1, 3.1 1.9)",
175 std::back_inserter(vector_in
));
177 boost::geometry::box
<P
> cb(P(1.5, 1.5), P(4.5, 2.5));
178 typedef std::vector
<std::vector
<P
> > VV
;
180 boost::geometry::intersection_inserter
<std::vector
<P
> >(cb
, vector_in
, std::back_inserter(vector_out
));
182 std::cout
<< "Clipped vector(s) " << std::endl
;
183 for (VV::const_iterator it
= vector_out
.begin(); it
!= vector_out
.end(); it
++)
185 std::copy(it
->begin(), it
->end(), std::ostream_iterator
<P
>(std::cout
, " "));
186 std::cout
<< std::endl
;
194 void example_intersection_polygon1()
196 typedef boost::geometry::point_xy
<double> P
;
197 typedef std::vector
<boost::geometry::polygon
<P
> > PV
;
199 boost::geometry::box
<P
> cb(P(1.5, 1.5), P(4.5, 2.5));
200 boost::geometry::polygon
<P
> poly
;
201 boost::geometry::read_wkt("POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
202 ",(4 2,4.2 1.4,4.8 1.9,4.4 2.2,4 2))", poly
);
205 boost::geometry::intersection_inserter
<boost::geometry::polygon
<P
> >(cb
, poly
, std::back_inserter(v
));
207 std::cout
<< "Clipped polygon(s) " << std::endl
;
208 for (PV::const_iterator it
= v
.begin(); it
!= v
.end(); it
++)
210 std::cout
<< boost::geometry::dsv(*it
) << std::endl
;
214 void example_simplify_linestring1()
217 //` Simplify a linestring
218 boost::geometry::linestring
<boost::geometry::point_xy
<double> > line
, simplified
;
219 boost::geometry::read_wkt("linestring(1.1 1.1, 2.5 2.1, 3.1 3.1, 4.9 1.1, 3.1 1.9)", line
);
220 boost::geometry::simplify(line
, simplified
, 0.5); /*< Simplify it, using distance of 0.5 units >*/
222 << " original line: " << boost::geometry::dsv(line
) << std::endl
223 << "simplified line: " << boost::geometry::dsv(simplified
) << std::endl
;
227 void example_simplify_linestring2()
230 //` Simplify a linestring using an output iterator
231 typedef boost::geometry::point_xy
<double> P
;
232 typedef boost::geometry::linestring
<P
> L
;
235 boost::geometry::read_wkt("linestring(1.1 1.1, 2.5 2.1, 3.1 3.1, 4.9 1.1, 3.1 1.9)", line
);
237 typedef boost::geometry::strategy::distance::projected_point
<P
, P
> DS
;
238 typedef boost::geometry::strategy::simplify::douglas_peucker
<P
, DS
> simplification
;
239 boost::geometry::simplify_inserter(line
, std::ostream_iterator
<P
>(std::cout
, "\n"), 0.5, simplification());
245 void example_within()
247 boost::geometry::polygon
<boost::geometry::point_xy
<double> > poly
;
248 boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly
);
249 boost::geometry::point_xy
<float> point(3, 3);
250 std::cout
<< "Point is "
251 << (boost::geometry::within(point
, poly
) ? "IN" : "NOT in")
257 void example_within_strategy()
259 // TO BE UPDATED/FINISHED
260 typedef boost::geometry::point_xy<double> P;
261 typedef boost::geometry::polygon<P> POLY;
263 std::cout << within(p, poly, strategy::within::cross_count<P>) << std::endl;
267 void example_length_linestring()
269 using namespace boost::geometry
;
270 linestring
<point_xy
<double> > line
;
271 read_wkt("linestring(0 0,1 1,4 8,3 2)", line
);
272 std::cout
<< "linestring length is "
274 << " units" << std::endl
;
277 Extension, other coordinate system:
278 // Linestring in latlong, filled with
279 // explicit degree-minute-second values
280 typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
281 linestring<LL> line_ll;
282 line_ll.push_back(LL(
283 latitude<float>(dms<north, float>(52, 22, 23)),
284 longitude<float>(dms<east, float>(4, 53, 32))));
285 line_ll.push_back(LL(
286 latitude<float>(dms<north, float>(51, 55, 51)),
287 longitude<float>(dms<east, float>(4, 28, 45))));
288 line_ll.push_back(LL(
289 latitude<float>(dms<north, float>(52, 4, 48)),
290 longitude<float>(dms<east, float>(4, 18, 0))));
291 std::cout << "linestring length is "
292 << length(line_ll) / 1000
293 << " kilometers " << std::endl;
297 void example_length_linestring_iterators1()
299 boost::geometry::linestring
<boost::geometry::point_xy
<double> > line
;
300 boost::geometry::read_wkt("linestring(0 0,1 1,4 8,3 2)", line
);
301 std::cout
<< "linestring length is "
302 << boost::geometry::length(line
)
303 << " units" << std::endl
;
306 void example_length_linestring_iterators2()
308 std::vector
<boost::geometry::point_xy
<double> > line
;
309 boost::geometry::read_wkt
<boost::geometry::point_xy
<double> >("linestring(0 0,1 1,4 8,3 2)", std::back_inserter(line
));
310 std::cout
<< "linestring length is "
311 << boost::geometry::length(line
)
312 << " units" << std::endl
;
315 void example_length_linestring_iterators3()
318 Extension, other coordinate system:
319 using namespace boost::geometry;
320 typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
322 boost::geometry::read_wkt<LL>("linestring(0 51,1 51,2 52)", std::back_inserter(line));
323 std::cout << "linestring length is "
324 << 0.001 * boost::geometry::length(line, boost::geometry::strategy::distance::vincenty<LL>())
325 << " kilometers" << std::endl;
330 void example_length_linestring_strategy()
333 Extension, other coordinate system:
334 using namespace boost::geometry;
335 typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
336 linestring<LL> line_ll;
337 line_ll.push_back(LL(latitude<float>(dms<north, float>(52, 22, 23)), longitude<float>(dms<east, float>(4, 53, 32))));
338 line_ll.push_back(LL(latitude<float>(dms<north, float>(51, 55, 51)), longitude<float>(dms<east, float>(4, 28, 45))));
339 line_ll.push_back(LL(latitude<float>(dms<north, float>(52, 4, 48)), longitude<float>(dms<east, float>(4, 18, 0))));
340 std::cout << "linestring length is "
341 << length(line_ll, strategy::distance::vincenty<LL, LL>() )/(1000)
342 << " kilometers " << std::endl;
347 void example_envelope_linestring()
349 boost::geometry::linestring
<boost::geometry::point_xy
<double> > line
;
350 boost::geometry::read_wkt("linestring(0 0,1 1,4 8,3 2)", line
);
351 boost::geometry::box
<boost::geometry::point_xy
<double> > box
;
352 boost::geometry::envelope(line
, box
);
354 std::cout
<< "envelope is " << boost::geometry::dsv(box
) << std::endl
;
357 void example_envelope_polygon()
360 Extension, other coordinate system:
361 using namespace boost::geometry;
362 typedef point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
364 // Wrangel island, 180 meridian crossing island above Siberia.
366 wrangel.outer().push_back(LL(latitude<>(dms<north>(70, 47, 7)), longitude<>(dms<west>(178, 47, 9))));
367 wrangel.outer().push_back(LL(latitude<>(dms<north>(71, 14, 0)), longitude<>(dms<east>(177, 28, 33))));
368 wrangel.outer().push_back(LL(latitude<>(dms<north>(71, 34, 24)), longitude<>(dms<east>(179, 44, 37))));
370 wrangel.outer().push_back(wrangel.outer().front());
372 boost::geometry::box<LL> box;
373 boost::geometry::envelope(wrangel, box);
375 dms<cd_lat> minlat(box.min_corner().lat());
376 dms<cd_lon> minlon(box.min_corner().lon());
378 dms<cd_lat> maxlat(box.max_corner().lat());
379 dms<cd_lon> maxlon(box.max_corner().lon());
381 std::cout << wrangel << std::endl;
382 std::cout << "min: " << minlat.get_dms() << " , " << minlon.get_dms() << std::endl;
383 std::cout << "max: " << maxlat.get_dms() << " , " << maxlon.get_dms() << std::endl;
391 Extension, other coordinate system:
392 // Construction with degree/minute/seconds
393 boost::geometry::dms<boost::geometry::east> d1(4, 53, 32.5);
395 // Explicit conversion to double.
396 std::cout << d1.as_value() << std::endl;
398 // Conversion to string, with optional strings
399 std::cout << d1.get_dms(" deg ", " min ", " sec") << std::endl;
401 // Combination with latitude/longitude and cardinal directions
403 using namespace boost::geometry;
404 point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > canberra(
405 latitude<>(dms<south>(35, 18, 27)),
406 longitude<>(dms<east>(149, 7, 27.9)));
407 std::cout << canberra << std::endl;
412 void example_point_ll_construct()
415 Extension, other coordinate system:
416 using namespace boost::geometry;
417 typedef point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > ll;
419 // Constructions in both orders possible
421 latitude<>(dms<north>(58, 21, 5)),
422 longitude<>(dms<west>(134, 30, 42)));
424 longitude<>(dms<east>(131, 54)),
425 latitude<>(dms<north>(43, 8))
432 example_area_polygon();
434 example_centroid_polygon();
436 example_distance_point_point();
437 example_distance_point_point_strategy();
439 example_from_wkt_point();
440 example_from_wkt_output_iterator();
441 example_from_wkt_linestring();
442 example_from_wkt_polygon();
444 example_as_wkt_point();
446 example_clip_linestring1();
447 example_clip_linestring2();
448 example_intersection_polygon1();
450 example_simplify_linestring1();
451 example_simplify_linestring2();
453 example_length_linestring();
454 example_length_linestring_iterators1();
455 example_length_linestring_iterators2();
456 example_length_linestring_iterators3();
457 example_length_linestring_strategy();
459 example_envelope_linestring();
460 example_envelope_polygon();
464 example_point_ll_convert();
465 example_point_ll_construct();