doxygen_1.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. OBSOLETE
  2. // Boost.Geometry (aka GGL, Generic Geometry Library)
  3. //
  4. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  5. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Doxygen Examples, referred to from the sources
  11. #include <boost/tuple/tuple.hpp>
  12. #if defined(_MSC_VER)
  13. // We deliberately mix float/double's here so turn off warning
  14. #pragma warning( disable : 4244 )
  15. #endif // defined(_MSC_VER)
  16. #include <boost/geometry/geometry.hpp>
  17. #include <boost/geometry/geometries/register/point.hpp>
  18. #include <boost/geometry/geometries/geometries.hpp>
  19. #include <boost/geometry/io/wkt/wkt.hpp>
  20. // All functions below are referred to in the documentation of Boost.Geometry
  21. // Don't rename them.
  22. void example_area_polygon()
  23. {
  24. //[area_polygon
  25. //` Calculate the area of a polygon
  26. boost::geometry::polygon<boost::geometry::point_xy<double> > poly; /*< Declare >*/
  27. boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly); /*< Fill, in this case with WKT >*/
  28. double area = boost::geometry::area(poly); /*< Calculate area >*/
  29. //]
  30. //[area_polygon_spherical
  31. //` Calculate the area of a *spherical* polygon
  32. namespace bg = boost::geometry;
  33. bg::polygon<bg::point<float, 2, bg::cs::spherical<bg::degree> > > sph_poly;
  34. bg::read_wkt("POLYGON((0 0,0 45,45 0,0 0))", sph_poly);
  35. double area = bg::area(sph_poly);
  36. //]
  37. }
  38. void example_as_wkt_point()
  39. {
  40. typedef boost::geometry::point_xy<double> P;
  41. P p(5.12, 6.34);
  42. // Points can be streamed like this:
  43. std::cout << boost::geometry::dsv<P>(p) << std::endl;
  44. // or like this:
  45. std::cout << boost::geometry::dsv(p) << std::endl;
  46. // or (with extension) like this:
  47. std::cout << boost::geometry::wkt(p) << std::endl;
  48. }
  49. void example_as_wkt_vector()
  50. {
  51. std::vector<boost::geometry::point_xy<int> > v;
  52. boost::geometry::read_wkt<boost::geometry::point_xy<int> >("linestring(1 1,2 2,3 3,4 4)", std::back_inserter(v));
  53. std::cout << boost::geometry::dsv(std::make_pair(v.begin(), v.end())) << std::endl;
  54. }
  55. void example_centroid_polygon()
  56. {
  57. boost::geometry::polygon<boost::geometry::point_xy<double> > poly;
  58. boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly);
  59. // Center of polygon might have different type than points of polygon
  60. boost::geometry::point_xy<float> center;
  61. boost::geometry::centroid(poly, center);
  62. std::cout << "Centroid: " << boost::geometry::dsv(center) << std::endl;
  63. }
  64. void example_distance_point_point()
  65. {
  66. boost::geometry::point_xy<double> p1(1, 1);
  67. boost::geometry::point_xy<double> p2(2, 3);
  68. std::cout << "Distance p1-p2 is "
  69. << boost::geometry::distance(p1, p2)
  70. << " units" << std::endl;
  71. /*
  72. Extension, other coordinate system:
  73. // Read 2 Dutch cities from WKT texts (in decimal degrees)
  74. boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > a, r;
  75. boost::geometry::read_wkt("POINT(4.89222 52.3731)", a);
  76. boost::geometry::read_wkt("POINT(4.47917 51.9308)", r);
  77. std::cout << "Distance Amsterdam-Rotterdam is "
  78. << boost::geometry::distance(a, r) / 1000.0
  79. << " kilometers " << std::endl;
  80. */
  81. }
  82. void example_distance_point_point_strategy()
  83. {
  84. /*
  85. Extension, other coordinate system:
  86. typedef boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
  87. LL a, r;
  88. boost::geometry::read_wkt("POINT(4.89222 52.3731)", a);
  89. boost::geometry::read_wkt("POINT(4.47917 51.9308)", r);
  90. std::cout << "Distance Amsterdam-Rotterdam is "
  91. << boost::geometry::distance(a, r,
  92. boost::geometry::strategy::distance::vincenty<LL>() )
  93. / 1000.0
  94. << " kilometers " << std::endl;
  95. */
  96. }
  97. void example_from_wkt_point()
  98. {
  99. boost::geometry::point_xy<int> point;
  100. boost::geometry::read_wkt("Point(1 2)", point);
  101. std::cout << point.x() << "," << point.y() << std::endl;
  102. }
  103. void example_from_wkt_output_iterator()
  104. {
  105. std::vector<boost::geometry::point_xy<int> > v;
  106. boost::geometry::read_wkt<boost::geometry::point_xy<int> >("linestring(1 1,2 2,3 3,4 4)", std::back_inserter(v));
  107. std::cout << "vector has " << v.size() << " coordinates" << std::endl;
  108. }
  109. void example_from_wkt_linestring()
  110. {
  111. boost::geometry::linestring<boost::geometry::point_xy<double> > line;
  112. boost::geometry::read_wkt("linestring(1 1,2 2,3 3,4 4)", line);
  113. std::cout << "linestring has " << line.size() << " coordinates" << std::endl;
  114. }
  115. void example_from_wkt_polygon()
  116. {
  117. boost::geometry::polygon<boost::geometry::point_xy<double> > poly;
  118. boost::geometry::read_wkt("POLYGON((0 0,0 1,1 1,1 0,0 0))", poly);
  119. std::cout << "Polygon has " << poly.outer().size() << " coordinates in outer ring" << std::endl;
  120. }
  121. void example_point_ll_convert()
  122. {
  123. /*
  124. Extension, other coordinate system:
  125. boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > deg(boost::geometry::latitude<>(33.0), boost::geometry::longitude<>(-118.0));
  126. boost::geometry::point_ll<double, boost::geometry::cs::geographic<boost::geometry::radian> > rad;
  127. boost::geometry::transform(deg, rad);
  128. std::cout << "point in radians: " << rad << std::endl;
  129. */
  130. }
  131. void example_clip_linestring1()
  132. {
  133. typedef boost::geometry::point_xy<double> P;
  134. boost::geometry::linestring<P> line;
  135. 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);
  136. boost::geometry::box<P> cb(P(1.5, 1.5), P(4.5, 2.5));
  137. std::cout << "Clipped linestring(s) " << std::endl;
  138. std::vector<boost::geometry::linestring<P> > intersection;
  139. boost::geometry::intersection_inserter<boost::geometry::linestring<P> >(cb, line, std::back_inserter(intersection));
  140. }
  141. void example_clip_linestring2()
  142. {
  143. typedef boost::geometry::point_xy<double> P;
  144. std::vector<P> vector_in;
  145. 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)",
  146. std::back_inserter(vector_in));
  147. boost::geometry::box<P> cb(P(1.5, 1.5), P(4.5, 2.5));
  148. typedef std::vector<std::vector<P> > VV;
  149. VV vector_out;
  150. boost::geometry::intersection_inserter<std::vector<P> >(cb, vector_in, std::back_inserter(vector_out));
  151. std::cout << "Clipped vector(s) " << std::endl;
  152. for (VV::const_iterator it = vector_out.begin(); it != vector_out.end(); it++)
  153. {
  154. std::copy(it->begin(), it->end(), std::ostream_iterator<P>(std::cout, " "));
  155. std::cout << std::endl;
  156. }
  157. }
  158. void example_intersection_polygon1()
  159. {
  160. typedef boost::geometry::point_xy<double> P;
  161. typedef std::vector<boost::geometry::polygon<P> > PV;
  162. boost::geometry::box<P> cb(P(1.5, 1.5), P(4.5, 2.5));
  163. boost::geometry::polygon<P> poly;
  164. 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)"
  165. ",(4 2,4.2 1.4,4.8 1.9,4.4 2.2,4 2))", poly);
  166. PV v;
  167. boost::geometry::intersection_inserter<boost::geometry::polygon<P> >(cb, poly, std::back_inserter(v));
  168. std::cout << "Clipped polygon(s) " << std::endl;
  169. for (PV::const_iterator it = v.begin(); it != v.end(); it++)
  170. {
  171. std::cout << boost::geometry::dsv(*it) << std::endl;
  172. }
  173. }
  174. void example_simplify_linestring1()
  175. {
  176. //[simplify
  177. //` Simplify a linestring
  178. boost::geometry::linestring<boost::geometry::point_xy<double> > line, simplified;
  179. 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);
  180. boost::geometry::simplify(line, simplified, 0.5); /*< Simplify it, using distance of 0.5 units >*/
  181. std::cout
  182. << " original line: " << boost::geometry::dsv(line) << std::endl
  183. << "simplified line: " << boost::geometry::dsv(simplified) << std::endl;
  184. //]
  185. }
  186. void example_simplify_linestring2()
  187. {
  188. //[simplify_inserter
  189. //` Simplify a linestring using an output iterator
  190. typedef boost::geometry::point_xy<double> P;
  191. typedef boost::geometry::linestring<P> L;
  192. L line;
  193. 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);
  194. typedef boost::geometry::strategy::distance::projected_point<P, P> DS;
  195. typedef boost::geometry::strategy::simplify::douglas_peucker<P, DS> simplification;
  196. boost::geometry::simplify_inserter(line, std::ostream_iterator<P>(std::cout, "\n"), 0.5, simplification());
  197. //]
  198. }
  199. void example_within()
  200. {
  201. boost::geometry::polygon<boost::geometry::point_xy<double> > poly;
  202. boost::geometry::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly);
  203. boost::geometry::point_xy<float> point(3, 3);
  204. std::cout << "Point is "
  205. << (boost::geometry::within(point, poly) ? "IN" : "NOT in")
  206. << " polygon"
  207. << std::endl;
  208. }
  209. /*
  210. void example_within_strategy()
  211. {
  212. // TO BE UPDATED/FINISHED
  213. typedef boost::geometry::point_xy<double> P;
  214. typedef boost::geometry::polygon<P> POLY;
  215. P p;
  216. std::cout << within(p, poly, strategy::within::cross_count<P>) << std::endl;
  217. }
  218. */
  219. void example_length_linestring()
  220. {
  221. using namespace boost::geometry;
  222. linestring<point_xy<double> > line;
  223. read_wkt("linestring(0 0,1 1,4 8,3 2)", line);
  224. std::cout << "linestring length is "
  225. << length(line)
  226. << " units" << std::endl;
  227. /*
  228. Extension, other coordinate system:
  229. // Linestring in latlong, filled with
  230. // explicit degree-minute-second values
  231. typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
  232. linestring<LL> line_ll;
  233. line_ll.push_back(LL(
  234. latitude<float>(dms<north, float>(52, 22, 23)),
  235. longitude<float>(dms<east, float>(4, 53, 32))));
  236. line_ll.push_back(LL(
  237. latitude<float>(dms<north, float>(51, 55, 51)),
  238. longitude<float>(dms<east, float>(4, 28, 45))));
  239. line_ll.push_back(LL(
  240. latitude<float>(dms<north, float>(52, 4, 48)),
  241. longitude<float>(dms<east, float>(4, 18, 0))));
  242. std::cout << "linestring length is "
  243. << length(line_ll) / 1000
  244. << " kilometers " << std::endl;
  245. */
  246. }
  247. void example_length_linestring_iterators1()
  248. {
  249. boost::geometry::linestring<boost::geometry::point_xy<double> > line;
  250. boost::geometry::read_wkt("linestring(0 0,1 1,4 8,3 2)", line);
  251. std::cout << "linestring length is "
  252. << boost::geometry::length(line)
  253. << " units" << std::endl;
  254. }
  255. void example_length_linestring_iterators2()
  256. {
  257. std::vector<boost::geometry::point_xy<double> > line;
  258. boost::geometry::read_wkt<boost::geometry::point_xy<double> >("linestring(0 0,1 1,4 8,3 2)", std::back_inserter(line));
  259. std::cout << "linestring length is "
  260. << boost::geometry::length(line)
  261. << " units" << std::endl;
  262. }
  263. void example_length_linestring_iterators3()
  264. {
  265. /*
  266. Extension, other coordinate system:
  267. using namespace boost::geometry;
  268. typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
  269. std::deque<LL> line;
  270. boost::geometry::read_wkt<LL>("linestring(0 51,1 51,2 52)", std::back_inserter(line));
  271. std::cout << "linestring length is "
  272. << 0.001 * boost::geometry::length(line, boost::geometry::strategy::distance::vincenty<LL>())
  273. << " kilometers" << std::endl;
  274. */
  275. }
  276. void example_length_linestring_strategy()
  277. {
  278. /*
  279. Extension, other coordinate system:
  280. using namespace boost::geometry;
  281. typedef point_ll<float, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
  282. linestring<LL> line_ll;
  283. line_ll.push_back(LL(latitude<float>(dms<north, float>(52, 22, 23)), longitude<float>(dms<east, float>(4, 53, 32))));
  284. line_ll.push_back(LL(latitude<float>(dms<north, float>(51, 55, 51)), longitude<float>(dms<east, float>(4, 28, 45))));
  285. line_ll.push_back(LL(latitude<float>(dms<north, float>(52, 4, 48)), longitude<float>(dms<east, float>(4, 18, 0))));
  286. std::cout << "linestring length is "
  287. << length(line_ll, strategy::distance::vincenty<LL, LL>() )/(1000)
  288. << " kilometers " << std::endl;
  289. */
  290. }
  291. void example_envelope_linestring()
  292. {
  293. boost::geometry::linestring<boost::geometry::point_xy<double> > line;
  294. boost::geometry::read_wkt("linestring(0 0,1 1,4 8,3 2)", line);
  295. boost::geometry::box<boost::geometry::point_xy<double> > box;
  296. boost::geometry::envelope(line, box);
  297. std::cout << "envelope is " << boost::geometry::dsv(box) << std::endl;
  298. }
  299. void example_envelope_polygon()
  300. {
  301. /*
  302. Extension, other coordinate system:
  303. using namespace boost::geometry;
  304. typedef point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > LL;
  305. // Wrangel island, 180 meridian crossing island above Siberia.
  306. polygon<LL> wrangel;
  307. wrangel.outer().push_back(LL(latitude<>(dms<north>(70, 47, 7)), longitude<>(dms<west>(178, 47, 9))));
  308. wrangel.outer().push_back(LL(latitude<>(dms<north>(71, 14, 0)), longitude<>(dms<east>(177, 28, 33))));
  309. wrangel.outer().push_back(LL(latitude<>(dms<north>(71, 34, 24)), longitude<>(dms<east>(179, 44, 37))));
  310. // Close it
  311. wrangel.outer().push_back(wrangel.outer().front());
  312. boost::geometry::box<LL> box;
  313. boost::geometry::envelope(wrangel, box);
  314. dms<cd_lat> minlat(box.min_corner().lat());
  315. dms<cd_lon> minlon(box.min_corner().lon());
  316. dms<cd_lat> maxlat(box.max_corner().lat());
  317. dms<cd_lon> maxlon(box.max_corner().lon());
  318. std::cout << wrangel << std::endl;
  319. std::cout << "min: " << minlat.get_dms() << " , " << minlon.get_dms() << std::endl;
  320. std::cout << "max: " << maxlat.get_dms() << " , " << maxlon.get_dms() << std::endl;
  321. */
  322. }
  323. void example_dms()
  324. {
  325. /*
  326. Extension, other coordinate system:
  327. // Construction with degree/minute/seconds
  328. boost::geometry::dms<boost::geometry::east> d1(4, 53, 32.5);
  329. // Explicit conversion to double.
  330. std::cout << d1.as_value() << std::endl;
  331. // Conversion to string, with optional strings
  332. std::cout << d1.get_dms(" deg ", " min ", " sec") << std::endl;
  333. // Combination with latitude/longitude and cardinal directions
  334. {
  335. using namespace boost::geometry;
  336. point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > canberra(
  337. latitude<>(dms<south>(35, 18, 27)),
  338. longitude<>(dms<east>(149, 7, 27.9)));
  339. std::cout << canberra << std::endl;
  340. }
  341. */
  342. }
  343. void example_point_ll_construct()
  344. {
  345. /*
  346. Extension, other coordinate system:
  347. using namespace boost::geometry;
  348. typedef point_ll<double, boost::geometry::cs::geographic<boost::geometry::degree> > ll;
  349. // Constructions in both orders possible
  350. ll juneau(
  351. latitude<>(dms<north>(58, 21, 5)),
  352. longitude<>(dms<west>(134, 30, 42)));
  353. ll wladiwostok(
  354. longitude<>(dms<east>(131, 54)),
  355. latitude<>(dms<north>(43, 8))
  356. );
  357. */
  358. }
  359. int main(void)
  360. {
  361. example_area_polygon();
  362. example_centroid_polygon();
  363. example_distance_point_point();
  364. example_distance_point_point_strategy();
  365. example_from_wkt_point();
  366. example_from_wkt_output_iterator();
  367. example_from_wkt_linestring();
  368. example_from_wkt_polygon();
  369. example_as_wkt_point();
  370. example_clip_linestring1();
  371. example_clip_linestring2();
  372. example_intersection_polygon1();
  373. example_simplify_linestring1();
  374. example_simplify_linestring2();
  375. example_length_linestring();
  376. example_length_linestring_iterators1();
  377. example_length_linestring_iterators2();
  378. example_length_linestring_iterators3();
  379. example_length_linestring_strategy();
  380. example_envelope_linestring();
  381. example_envelope_polygon();
  382. example_within();
  383. example_point_ll_convert();
  384. example_point_ll_construct();
  385. example_dms();
  386. return 0;
  387. }