1 OBSOLETE
2 
3 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 //
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)
10 //
11 // Doxygen Examples, referred to from the sources
12 
13 #include <boost/tuple/tuple.hpp>
14 
15 #if defined(_MSC_VER)
16 // We deliberately mix float/double's here so turn off warning
17 #pragma warning( disable : 4244 )
18 #endif // defined(_MSC_VER)
19 
20 #include <boost/geometry/geometry.hpp>
21 #include <boost/geometry/geometries/register/point.hpp>
22 #include <boost/geometry/geometries/geometries.hpp>
23 
24 #include <boost/geometry/io/wkt/wkt.hpp>
25 
26 // All functions below are referred to in the documentation of Boost.Geometry
27 // Don't rename them.
example_area_polygon()28 void example_area_polygon()
29 {
30     //[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 >*/
35     //]
36 
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);
43     //]
44 }
45 
example_as_wkt_point()46 void example_as_wkt_point()
47 {
48     typedef boost::geometry::point_xy<double> P;
49     P p(5.12, 6.34);
50     // Points can be streamed like this:
51     std::cout << boost::geometry::dsv<P>(p) << std::endl;
52 
53     // or like this:
54     std::cout << boost::geometry::dsv(p) << std::endl;
55 
56     // or (with extension) like this:
57     std::cout << boost::geometry::wkt(p) << std::endl;
58 }
59 
example_as_wkt_vector()60 void example_as_wkt_vector()
61 {
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));
64 
65     std::cout << boost::geometry::dsv(std::make_pair(v.begin(), v.end())) << std::endl;
66 }
67 
68 
example_centroid_polygon()69 void example_centroid_polygon()
70 {
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;
77 }
78 
79 
example_distance_point_point()80 void example_distance_point_point()
81 {
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;
87 
88     /*
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);
94 
95     std::cout << "Distance Amsterdam-Rotterdam is "
96         << boost::geometry::distance(a, r) / 1000.0
97         << " kilometers " << std::endl;
98     */
99 }
100 
example_distance_point_point_strategy()101 void example_distance_point_point_strategy()
102 {
103     /*
104     Extension, other coordinate system:
105     typedef boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
106     LL a, r;
107     boost::geometry::read_wkt("POINT(4.89222 52.3731)", a);
108     boost::geometry::read_wkt("POINT(4.47917 51.9308)", r);
109 
110     std::cout << "Distance Amsterdam-Rotterdam is "
111         << boost::geometry::distance(a, r,
112                 boost::geometry::strategy::distance::vincenty<LL>() )
113                  / 1000.0
114         << " kilometers " << std::endl;
115     */
116 }
117 
example_from_wkt_point()118 void example_from_wkt_point()
119 {
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;
123 }
124 
example_from_wkt_output_iterator()125 void example_from_wkt_output_iterator()
126 {
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;
130 }
131 
example_from_wkt_linestring()132 void example_from_wkt_linestring()
133 {
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;
137 }
138 
example_from_wkt_polygon()139 void example_from_wkt_polygon()
140 {
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;
144 }
145 
example_point_ll_convert()146 void example_point_ll_convert()
147 {
148     /*
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);
153 
154     std::cout << "point in radians: " << rad << std::endl;
155     */
156 }
157 
example_clip_linestring1()158 void example_clip_linestring1()
159 {
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;
165 
166     std::vector<boost::geometry::linestring<P> > intersection;
167     boost::geometry::intersection_inserter<boost::geometry::linestring<P> >(cb, line, std::back_inserter(intersection));
168 }
169 
example_clip_linestring2()170 void example_clip_linestring2()
171 {
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));
176 
177     boost::geometry::box<P> cb(P(1.5, 1.5), P(4.5, 2.5));
178     typedef std::vector<std::vector<P> > VV;
179     VV vector_out;
180     boost::geometry::intersection_inserter<std::vector<P>  >(cb, vector_in, std::back_inserter(vector_out));
181 
182     std::cout << "Clipped vector(s) " << std::endl;
183     for (VV::const_iterator it = vector_out.begin(); it != vector_out.end(); it++)
184     {
185         std::copy(it->begin(), it->end(), std::ostream_iterator<P>(std::cout, " "));
186         std::cout << std::endl;
187     }
188 }
189 
190 
191 
192 
193 
example_intersection_polygon1()194 void example_intersection_polygon1()
195 {
196     typedef boost::geometry::point_xy<double> P;
197     typedef std::vector<boost::geometry::polygon<P> > PV;
198 
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);
203 
204     PV v;
205     boost::geometry::intersection_inserter<boost::geometry::polygon<P> >(cb, poly, std::back_inserter(v));
206 
207     std::cout << "Clipped polygon(s) " << std::endl;
208     for (PV::const_iterator it = v.begin(); it != v.end(); it++)
209     {
210         std::cout << boost::geometry::dsv(*it) << std::endl;
211     }
212 }
213 
example_simplify_linestring1()214 void example_simplify_linestring1()
215 {
216     //[simplify
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 >*/
221     std::cout
222         << "  original line: " << boost::geometry::dsv(line) << std::endl
223         << "simplified line: " << boost::geometry::dsv(simplified) << std::endl;
224     //]
225 }
226 
example_simplify_linestring2()227 void example_simplify_linestring2()
228 {
229     //[simplify_inserter
230     //` Simplify a linestring using an output iterator
231     typedef boost::geometry::point_xy<double> P;
232     typedef boost::geometry::linestring<P> L;
233     L line;
234 
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);
236 
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());
240     //]
241 }
242 
243 
244 
example_within()245 void example_within()
246 {
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")
252         << " polygon"
253         << std::endl;
254 }
255 
256 /*
257 void example_within_strategy()
258 {
259     // TO BE UPDATED/FINISHED
260     typedef boost::geometry::point_xy<double> P;
261     typedef boost::geometry::polygon<P> POLY;
262     P p;
263     std::cout << within(p, poly, strategy::within::cross_count<P>) << std::endl;
264 }
265 */
266 
example_length_linestring()267 void example_length_linestring()
268 {
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 "
273         << length(line)
274         << " units" << std::endl;
275 
276     /*
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;
294         */
295 }
296 
example_length_linestring_iterators1()297 void example_length_linestring_iterators1()
298 {
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;
304 }
305 
example_length_linestring_iterators2()306 void example_length_linestring_iterators2()
307 {
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;
313 }
314 
example_length_linestring_iterators3()315 void example_length_linestring_iterators3()
316 {
317     /*
318     Extension, other coordinate system:
319     using namespace boost::geometry;
320     typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
321     std::deque<LL> line;
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;
326     */
327 }
328 
329 
example_length_linestring_strategy()330 void example_length_linestring_strategy()
331 {
332     /*
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;
343     */
344 }
345 
346 
example_envelope_linestring()347 void example_envelope_linestring()
348 {
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);
353 
354     std::cout << "envelope is " << boost::geometry::dsv(box) << std::endl;
355 }
356 
example_envelope_polygon()357 void example_envelope_polygon()
358 {
359     /*
360     Extension, other coordinate system:
361     using namespace boost::geometry;
362     typedef point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> >  LL;
363 
364     // Wrangel island, 180 meridian crossing island above Siberia.
365     polygon<LL> wrangel;
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))));
369     // Close it
370     wrangel.outer().push_back(wrangel.outer().front());
371 
372     boost::geometry::box<LL> box;
373     boost::geometry::envelope(wrangel, box);
374 
375     dms<cd_lat> minlat(box.min_corner().lat());
376     dms<cd_lon> minlon(box.min_corner().lon());
377 
378     dms<cd_lat> maxlat(box.max_corner().lat());
379     dms<cd_lon> maxlon(box.max_corner().lon());
380 
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;
384     */
385 }
386 
387 
example_dms()388 void example_dms()
389 {
390     /*
391     Extension, other coordinate system:
392     // Construction with degree/minute/seconds
393     boost::geometry::dms<boost::geometry::east> d1(4, 53, 32.5);
394 
395     // Explicit conversion to double.
396     std::cout << d1.as_value() << std::endl;
397 
398     // Conversion to string, with optional strings
399     std::cout << d1.get_dms(" deg ", " min ", " sec") << std::endl;
400 
401     // Combination with latitude/longitude and cardinal directions
402     {
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;
408     }
409     */
410 }
411 
example_point_ll_construct()412 void example_point_ll_construct()
413 {
414     /*
415     Extension, other coordinate system:
416     using namespace boost::geometry;
417     typedef point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > ll;
418 
419     // Constructions in both orders possible
420     ll juneau(
421         latitude<>(dms<north>(58, 21, 5)),
422         longitude<>(dms<west>(134, 30, 42)));
423     ll wladiwostok(
424         longitude<>(dms<east>(131, 54)),
425         latitude<>(dms<north>(43, 8))
426         );
427     */
428 }
429 
main(void)430 int main(void)
431 {
432     example_area_polygon();
433 
434     example_centroid_polygon();
435 
436     example_distance_point_point();
437     example_distance_point_point_strategy();
438 
439     example_from_wkt_point();
440     example_from_wkt_output_iterator();
441     example_from_wkt_linestring();
442     example_from_wkt_polygon();
443 
444     example_as_wkt_point();
445 
446     example_clip_linestring1();
447     example_clip_linestring2();
448     example_intersection_polygon1();
449 
450     example_simplify_linestring1();
451     example_simplify_linestring2();
452 
453     example_length_linestring();
454     example_length_linestring_iterators1();
455     example_length_linestring_iterators2();
456     example_length_linestring_iterators3();
457     example_length_linestring_strategy();
458 
459     example_envelope_linestring();
460     example_envelope_polygon();
461 
462     example_within();
463 
464     example_point_ll_convert();
465     example_point_ll_construct();
466     example_dms();
467 
468     return 0;
469 }
470