1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2017, 2018.
6 // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
8 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_HAVERSINE_HPP
16 #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_HAVERSINE_HPP
19 #include <boost/geometry/core/access.hpp>
20 #include <boost/geometry/core/cs.hpp>
21 #include <boost/geometry/core/radian_access.hpp>
23 #include <boost/geometry/srs/sphere.hpp>
25 #include <boost/geometry/strategies/distance.hpp>
26 #include <boost/geometry/strategies/spherical/get_radius.hpp>
28 #include <boost/geometry/util/math.hpp>
29 #include <boost/geometry/util/promote_floating_point.hpp>
30 #include <boost/geometry/util/select_calculation_type.hpp>
33 namespace boost { namespace geometry
37 namespace strategy { namespace distance
44 // Comparable haversine.
45 // To compare distances, we can avoid:
46 // - multiplication with radius and 2.0
48 // - applying asin (which is strictly (monotone) increasing)
51 typename RadiusTypeOrSphere = double,
52 typename CalculationType = void
57 template <typename Point1, typename Point2>
58 struct calculation_type
59 : promote_floating_point
61 typename select_calculation_type
70 typedef typename strategy_detail::get_radius
79 template <typename RadiusOrSphere>
80 explicit inline haversine(RadiusOrSphere const& radius_or_sphere)
81 : m_radius(strategy_detail::get_radius
84 >::apply(radius_or_sphere))
87 template <typename Point1, typename Point2>
88 static inline typename calculation_type<Point1, Point2>::type
89 apply(Point1 const& p1, Point2 const& p2)
91 return calculate<typename calculation_type<Point1, Point2>::type>(
92 get_as_radian<0>(p1), get_as_radian<1>(p1),
93 get_as_radian<0>(p2), get_as_radian<1>(p2)
97 template <typename T1, typename T2>
98 inline radius_type meridian(T1 lat1, T2 lat2) const
100 return m_radius * (lat1 - lat2);
103 inline radius_type radius() const
110 template <typename R, typename T1, typename T2>
111 static inline R calculate(T1 const& lon1, T1 const& lat1,
112 T2 const& lon2, T2 const& lat2)
114 return math::hav(lat2 - lat1)
115 + cos(lat1) * cos(lat2) * math::hav(lon2 - lon1);
118 radius_type m_radius;
123 } // namespace comparable
126 \brief Distance calculation for spherical coordinates
127 on a perfect sphere using haversine
129 \tparam RadiusTypeOrSphere \tparam_radius_or_sphere
130 \tparam CalculationType \tparam_calculation
131 \author Adapted from: http://williams.best.vwh.net/avform.htm
132 \see http://en.wikipedia.org/wiki/Great-circle_distance
133 \note (from Wiki:) The great circle distance d between two
134 points with coordinates {lat1,lon1} and {lat2,lon2} is given by:
135 d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
136 A mathematically equivalent formula, which is less subject
137 to rounding error for short distances is:
138 d=2*asin(sqrt((sin((lat1-lat2) / 2))^2
139 + cos(lat1)*cos(lat2)*(sin((lon1-lon2) / 2))^2))
142 [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
147 typename RadiusTypeOrSphere = double,
148 typename CalculationType = void
152 typedef comparable::haversine<RadiusTypeOrSphere, CalculationType> comparable_type;
155 template <typename Point1, typename Point2>
156 struct calculation_type
157 : services::return_type<comparable_type, Point1, Point2>
160 typedef typename strategy_detail::get_radius
166 \brief Default constructor, radius set to 1.0 for the unit sphere
174 \param radius_or_sphere radius of the sphere or sphere model
176 template <typename RadiusOrSphere>
177 explicit inline haversine(RadiusOrSphere const& radius_or_sphere)
178 : m_radius(strategy_detail::get_radius
181 >::apply(radius_or_sphere))
185 \brief applies the distance calculation
186 \return the calculated distance (including multiplying with radius)
187 \param p1 first point
188 \param p2 second point
190 template <typename Point1, typename Point2>
191 inline typename calculation_type<Point1, Point2>::type
192 apply(Point1 const& p1, Point2 const& p2) const
194 typedef typename calculation_type<Point1, Point2>::type calculation_type;
195 calculation_type const a = comparable_type::apply(p1, p2);
196 calculation_type const c = calculation_type(2.0) * asin(math::sqrt(a));
197 return calculation_type(m_radius) * c;
201 \brief meridian distance calculation
202 \return the calculated distance (including multiplying with radius)
203 \param p1 first point
204 \param p2 second point
207 template <typename T1, typename T2>
208 inline radius_type meridian(T1 lat1, T2 lat2) const
210 return m_radius * (lat1 - lat2);
214 \brief access to radius value
217 inline radius_type radius() const
223 radius_type m_radius;
227 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
231 template <typename RadiusType, typename CalculationType>
232 struct tag<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<haversine<RadiusType, CalculationType>, P1, P2>
240 : haversine<RadiusType, CalculationType>::template calculation_type<P1, P2>
244 template <typename RadiusType, typename CalculationType>
245 struct comparable_type<haversine<RadiusType, CalculationType> >
247 typedef comparable::haversine<RadiusType, CalculationType> type;
251 template <typename RadiusType, typename CalculationType>
252 struct get_comparable<haversine<RadiusType, CalculationType> >
255 typedef haversine<RadiusType, CalculationType> this_type;
256 typedef comparable::haversine<RadiusType, CalculationType> comparable_type;
258 static inline comparable_type apply(this_type const& input)
260 return comparable_type(input.radius());
264 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
265 struct result_from_distance<haversine<RadiusType, CalculationType>, P1, P2>
268 typedef haversine<RadiusType, CalculationType> this_type;
269 typedef typename return_type<this_type, P1, P2>::type return_type;
271 template <typename T>
272 static inline return_type apply(this_type const& , T const& value)
274 return return_type(value);
279 // Specializations for comparable::haversine
280 template <typename RadiusType, typename CalculationType>
281 struct tag<comparable::haversine<RadiusType, CalculationType> >
283 typedef strategy_tag_distance_point_point type;
287 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
288 struct return_type<comparable::haversine<RadiusType, CalculationType>, P1, P2>
289 : comparable::haversine<RadiusType, CalculationType>::template calculation_type<P1, P2>
293 template <typename RadiusType, typename CalculationType>
294 struct comparable_type<comparable::haversine<RadiusType, CalculationType> >
296 typedef comparable::haversine<RadiusType, CalculationType> type;
300 template <typename RadiusType, typename CalculationType>
301 struct get_comparable<comparable::haversine<RadiusType, CalculationType> >
304 typedef comparable::haversine<RadiusType, CalculationType> this_type;
306 static inline this_type apply(this_type const& input)
313 template <typename RadiusType, typename CalculationType, typename P1, typename P2>
314 struct result_from_distance<comparable::haversine<RadiusType, CalculationType>, P1, P2>
317 typedef comparable::haversine<RadiusType, CalculationType> strategy_type;
318 typedef typename return_type<strategy_type, P1, P2>::type return_type;
320 template <typename T>
321 static inline return_type apply(strategy_type const& strategy, T const& distance)
323 return_type const s = sin((distance / strategy.radius()) / return_type(2));
329 // Register it as the default for point-types
330 // in a spherical equatorial coordinate system
331 template <typename Point1, typename Point2>
332 struct default_strategy
334 point_tag, point_tag, Point1, Point2,
335 spherical_equatorial_tag, spherical_equatorial_tag
338 typedef strategy::distance::haversine<typename select_coordinate_type<Point1, Point2>::type> type;
341 // Note: spherical polar coordinate system requires "get_as_radian_equatorial"
344 } // namespace services
345 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
348 }} // namespace strategy::distance
351 }} // namespace boost::geometry
354 #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_HAVERSINE_HPP