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 // Example: Custom coordinate system example
12 
13 #include <iostream>
14 
15 #include <boost/geometry/geometry.hpp>
16 
17 // 1: declare a coordinate system. For example for Mars
18 //    Like for the Earth, we let the use choose between degrees or radians
19 //    (Unfortunately, in real life Mars has two coordinate systems:
20 //     http://planetarynames.wr.usgs.gov/Page/MARS/system)
21 template<typename DegreeOrRadian>
22 struct martian
23 {
24     typedef DegreeOrRadian units;
25 };
26 
27 // 2: give it also a family
28 struct martian_tag;
29 
30 // 3: register to which coordinate system family it belongs to
31 //    this must be done in namespace boost::geometry::traits
32 namespace boost { namespace geometry { namespace traits
33 {
34 
35 template <typename DegreeOrRadian>
36 struct cs_tag<martian<DegreeOrRadian> >
37 {
38     typedef martian_tag type;
39 };
40 
41 }}} // namespaces
42 
43 
44 // NOTE: if the next steps would not be here,
45 // compiling a distance function call with martian coordinates
46 // would result in a MPL assertion
47 
48 // 4: so register a distance strategy as its default strategy
49 namespace boost { namespace geometry { namespace strategy { namespace distance { namespace services
50 {
51 
52 template <typename Point1, typename Point2>
53 struct default_strategy<point_tag, point_tag, Point1, Point2, martian_tag, martian_tag>
54 {
55     typedef haversine<double> type;
56 };
57 
58 }}}}} // namespaces
59 
60 // 5: not worked out. To implement a specific distance strategy for Mars,
61 //    e.g. with the Mars radius given by default,
62 //    you will have to implement (/register) several other metafunctions:
63 //      tag, return_type, similar_type, comparable_type,
64 //    and structs:
65 //      get_similar, get_comparable, result_from_distance
66 //   See e.g. .../boost/geometry/extensions/gis/geographic/strategies/andoyer.hpp
67 
main()68 int main()
69 {
70     typedef boost::geometry::model::point
71         <
72             double, 2, martian<boost::geometry::degree>
73         > mars_point;
74 
75     // Declare two points
76     // (Source: http://nssdc.gsfc.nasa.gov/planetary/mars_mileage_guide.html)
77     // (Other sources: Wiki and Google give slightly different coordinates, resulting
78     //  in other distance, 20 km off)
79     mars_point viking1(-48.23, 22.54); // Viking 1 landing site in Chryse Planitia
80     mars_point pathfinder(-33.55, 19.33); // Pathfinder landing site in Ares Vallis
81 
82     double d = boost::geometry::distance(viking1, pathfinder); // Distance in radians on unit-sphere
83 
84     // Using the Mars mean radius
85     // (Source: http://nssdc.gsfc.nasa.gov/planetary/factsheet/marsfact.html)
86     std::cout << "Distance between Viking1 and Pathfinder landing sites: "
87         << d * 3389.5 << " km" << std::endl;
88 
89     // We would get 832.616 here, same order as the 835 (rounded on 5 km) listed
90     // on the mentioned site
91 
92 #ifdef OPTIONALLY_ELLIPSOIDAL
93     // Optionally the distance can be calculated more accurate by an Ellipsoidal approach,
94     // giving 834.444 km
95     d = boost::geometry::distance(viking1, pathfinder,
96         boost::geometry::strategy::distance::andoyer<mars_point>
97             (boost::geometry::srs::spheroid<double>(3396.2, 3376.2)));
98     std::cout << "Ellipsoidal distance: " << d << " km" << std::endl;
99 #endif
100 
101     return 0;
102 }
103