• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Custom Linestring Example
12 
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 
17 #include <boost/geometry/geometry.hpp>
18 #include <boost/geometry/geometries/register/point.hpp>
19 #include <boost/geometry/geometries/register/linestring.hpp>
20 
21 #include <boost/geometry/extensions/algorithms/parse.hpp>
22 
23 // Define a GPS point with coordinates in latitude/longitude and some additional values
24 struct gps_point
25 {
26     double latitude, longitude, height;
27     double speed;
28     // Date/time, heading, etc could be added
29 
30     // The default constructor is required if being used in a vector
gps_pointgps_point31     gps_point() {}
32 
33     // Define a constructor to create the point in one line. Order of latitude/longitude
34     // does not matter as long as "E", "N", etc are included
gps_pointgps_point35     gps_point(std::string const& c1, std::string const& c2, double h, double s)
36         : height(h)
37         , speed(s)
38     {
39         boost::geometry::parse(*this, c1, c2);
40     }
41 };
42 
43 // Declare a custom linestring which will have the GPS points
44 struct gps_track : std::vector<gps_point>
45 {
46     std::string owner;
47     int route_identifier;
48     // etc
49 
gps_trackgps_track50     gps_track(int i, std::string const& o)
51         : owner(o)
52         , route_identifier(i)
53     {}
54 };
55 
56 
57 // Register this point as being a recognizable point by Boost.Geometry
BOOST_GEOMETRY_REGISTER_POINT_2D(gps_point,double,cs::geographic<degree>,longitude,latitude)58 BOOST_GEOMETRY_REGISTER_POINT_2D(gps_point, double, cs::geographic<degree>, longitude, latitude)
59 
60 // Register the track as well, as being a "linestring"
61 BOOST_GEOMETRY_REGISTER_LINESTRING(gps_track)
62 
63 
64 int main()
65 {
66     // Declare a "GPS Track" and add some GPS points
67     gps_track track(23, "Mister G");
68     track.push_back(gps_point("52 22 23 N", "4 53 32 E", 50, 180));
69     track.push_back(gps_point("52 10 00 N", "4 59 59 E", 110, 170));
70     track.push_back(gps_point("52 5 20 N", "5 6 56 E", 0, 90));
71 
72     std::cout
73         << "track:  " << track.route_identifier << std::endl
74         << "from:   " << track.owner << std::endl
75         << "as wkt: " << boost::geometry::dsv(track) << std::endl
76         << "length: " << boost::geometry::length(track)/1000.0 << " km" << std::endl;
77 
78     // Above gives the idea, shows that custom linestrings can be useful.
79     // We could of course do anything with this track which the library can handle, e.g.:
80     // - simplify it
81     // - calculate distance of point-to-line
82     // - project it to UTM, then transform it to a GIF image (see p03_example)
83 
84     return 0;
85 }
86