1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
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)
9 #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_HAVERSINE_HPP
10 #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_HAVERSINE_HPP
13 #include <boost/geometry/core/cs.hpp>
14 #include <boost/geometry/core/access.hpp>
15 #include <boost/geometry/core/radian_access.hpp>
17 #include <boost/geometry/util/math.hpp>
18 #include <boost/geometry/util/select_calculation_type.hpp>
19 #include <boost/geometry/util/promote_floating_point.hpp>
21 #include <boost/geometry/strategies/distance.hpp>
25 namespace boost { namespace geometry
29 namespace strategy { namespace distance
36 // Comparable haversine.
37 // To compare distances, we can avoid:
38 // - multiplication with radius and 2.0
40 // - applying asin (which is strictly (monotone) increasing)
44 typename CalculationType = void
49 template <typename Point1, typename Point2>
50 struct calculation_type
51 : promote_floating_point
53 typename select_calculation_type
62 typedef RadiusType radius_type;
64 explicit inline haversine(RadiusType const& r = 1.0)
68 template <typename Point1, typename Point2>
69 static inline typename calculation_type<Point1, Point2>::type
70 apply(Point1 const& p1, Point2 const& p2)
72 return calculate<typename calculation_type<Point1, Point2>::type>(
73 get_as_radian<0>(p1), get_as_radian<1>(p1),
74 get_as_radian<0>(p2), get_as_radian<1>(p2)
78 inline RadiusType radius() const
85 template <typename R, typename T1, typename T2>
86 static inline R calculate(T1 const& lon1, T1 const& lat1,
87 T2 const& lon2, T2 const& lat2)
89 return math::hav(lat2 - lat1)
90 + cos(lat1) * cos(lat2) * math::hav(lon2 - lon1);
98 } // namespace comparable
101 \brief Distance calculation for spherical coordinates
102 on a perfect sphere using haversine
104 \tparam RadiusType \tparam_radius
105 \tparam CalculationType \tparam_calculation
106 \author Adapted from: http://williams.best.vwh.net/avform.htm
107 \see http://en.wikipedia.org/wiki/Great-circle_distance
108 \note (from Wiki:) The great circle distance d between two
109 points with coordinates {lat1,lon1} and {lat2,lon2} is given by:
110 d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
111 A mathematically equivalent formula, which is less subject
112 to rounding error for short distances is:
113 d=2*asin(sqrt((sin((lat1-lat2) / 2))^2
114 + cos(lat1)*cos(lat2)*(sin((lon1-lon2) / 2))^2))
119 [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
126 typename CalculationType = void
130 typedef comparable::haversine<RadiusType, CalculationType> comparable_type;
133 template <typename Point1, typename Point2>
134 struct calculation_type
135 : services::return_type<comparable_type, Point1, Point2>
138 typedef RadiusType radius_type;
142 \param radius radius of the sphere, defaults to 1.0 for the unit sphere
144 inline haversine(RadiusType const& radius = 1.0)
149 \brief applies the distance calculation
150 \return the calculated distance (including multiplying with radius)
151 \param p1 first point
152 \param p2 second point
154 template <typename Point1, typename Point2>
155 inline typename calculation_type<Point1, Point2>::type
156 apply(Point1 const& p1, Point2 const& p2) const
158 typedef typename calculation_type<Point1, Point2>::type calculation_type;
159 calculation_type const a = comparable_type::apply(p1, p2);
160 calculation_type const c = calculation_type(2.0) * asin(math::sqrt(a));
161 return calculation_type(m_radius) * c;
165 \brief access to radius value
168 inline RadiusType radius() const
178 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
182 template <typename RadiusType, typename CalculationType>
183 struct tag<haversine<RadiusType, CalculationType> >
185 typedef strategy_tag_distance_point_point type;
189 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
190 struct return_type<haversine<RadiusType, CalculationType>, P1, P2>
191 : haversine<RadiusType, CalculationType>::template calculation_type<P1, P2>
195 template <typename RadiusType, typename CalculationType>
196 struct comparable_type<haversine<RadiusType, CalculationType> >
198 typedef comparable::haversine<RadiusType, CalculationType> type;
202 template <typename RadiusType, typename CalculationType>
203 struct get_comparable<haversine<RadiusType, CalculationType> >
206 typedef haversine<RadiusType, CalculationType> this_type;
207 typedef comparable::haversine<RadiusType, CalculationType> comparable_type;
209 static inline comparable_type apply(this_type const& input)
211 return comparable_type(input.radius());
215 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
216 struct result_from_distance<haversine<RadiusType, CalculationType>, P1, P2>
219 typedef haversine<RadiusType, CalculationType> this_type;
220 typedef typename return_type<this_type, P1, P2>::type return_type;
222 template <typename T>
223 static inline return_type apply(this_type const& , T const& value)
225 return return_type(value);
230 // Specializations for comparable::haversine
231 template <typename RadiusType, typename CalculationType>
232 struct tag<comparable::haversine<RadiusType, CalculationType> >
234 typedef strategy_tag_distance_point_point type;
238 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
239 struct return_type<comparable::haversine<RadiusType, CalculationType>, P1, P2>
240 : comparable::haversine<RadiusType, CalculationType>::template calculation_type<P1, P2>
244 template <typename RadiusType, typename CalculationType>
245 struct comparable_type<comparable::haversine<RadiusType, CalculationType> >
247 typedef comparable::haversine<RadiusType, CalculationType> type;
251 template <typename RadiusType, typename CalculationType>
252 struct get_comparable<comparable::haversine<RadiusType, CalculationType> >
255 typedef comparable::haversine<RadiusType, CalculationType> this_type;
257 static inline this_type apply(this_type const& input)
264 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
265 struct result_from_distance<comparable::haversine<RadiusType, CalculationType>, P1, P2>
268 typedef comparable::haversine<RadiusType, CalculationType> strategy_type;
269 typedef typename return_type<strategy_type, P1, P2>::type return_type;
271 template <typename T>
272 static inline return_type apply(strategy_type const& strategy, T const& distance)
274 return_type const s = sin((distance / strategy.radius()) / return_type(2));
280 // Register it as the default for point-types
281 // in a spherical equatorial coordinate system
282 template <typename Point1, typename Point2>
283 struct default_strategy
285 point_tag, point_tag, Point1, Point2,
286 spherical_equatorial_tag, spherical_equatorial_tag
289 typedef strategy::distance::haversine<typename select_coordinate_type<Point1, Point2>::type> type;
292 // Note: spherical polar coordinate system requires "get_as_radian_equatorial"
295 } // namespace services
296 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
299 }} // namespace strategy::distance
302 }} // namespace boost::geometry
305 #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_HAVERSINE_HPP