c03_custom_linestring_example.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Custom Linestring Example
  10. #include <iostream>
  11. #include <string>
  12. #include <vector>
  13. #include <boost/geometry/geometry.hpp>
  14. #include <boost/geometry/geometries/register/point.hpp>
  15. #include <boost/geometry/geometries/register/linestring.hpp>
  16. #include <boost/geometry/extensions/algorithms/parse.hpp>
  17. // Define a GPS point with coordinates in latitude/longitude and some additional values
  18. struct gps_point
  19. {
  20. double latitude, longitude, height;
  21. double speed;
  22. // Date/time, heading, etc could be added
  23. // The default constructor is required if being used in a vector
  24. gps_point() {}
  25. // Define a constructor to create the point in one line. Order of latitude/longitude
  26. // does not matter as long as "E", "N", etc are included
  27. gps_point(std::string const& c1, std::string const& c2, double h, double s)
  28. : height(h)
  29. , speed(s)
  30. {
  31. boost::geometry::parse(*this, c1, c2);
  32. }
  33. };
  34. // Declare a custom linestring which will have the GPS points
  35. struct gps_track : std::vector<gps_point>
  36. {
  37. std::string owner;
  38. int route_identifier;
  39. // etc
  40. gps_track(int i, std::string const& o)
  41. : owner(o)
  42. , route_identifier(i)
  43. {}
  44. };
  45. // Register this point as being a recognizable point by Boost.Geometry
  46. BOOST_GEOMETRY_REGISTER_POINT_2D(gps_point, double, cs::geographic<degree>, longitude, latitude)
  47. // Register the track as well, as being a "linestring"
  48. BOOST_GEOMETRY_REGISTER_LINESTRING(gps_track)
  49. int main()
  50. {
  51. // Declare a "GPS Track" and add some GPS points
  52. gps_track track(23, "Mister G");
  53. track.push_back(gps_point("52 22 23 N", "4 53 32 E", 50, 180));
  54. track.push_back(gps_point("52 10 00 N", "4 59 59 E", 110, 170));
  55. track.push_back(gps_point("52 5 20 N", "5 6 56 E", 0, 90));
  56. std::cout
  57. << "track: " << track.route_identifier << std::endl
  58. << "from: " << track.owner << std::endl
  59. << "as wkt: " << boost::geometry::dsv(track) << std::endl
  60. << "length: " << boost::geometry::length(track)/1000.0 << " km" << std::endl;
  61. // Above gives the idea, shows that custom linestrings can be useful.
  62. // We could of course do anything with this track which the library can handle, e.g.:
  63. // - simplify it
  64. // - calculate distance of point-to-line
  65. // - project it to UTM, then transform it to a GIF image (see p03_example)
  66. return 0;
  67. }