Boost-Geometry-Utils

 view release on metacpan or  search on metacpan

src/boost/geometry/strategies/spherical/distance_haversine.hpp  view on Meta::CPAN

class haversine
{
public :
    typedef typename promote_floating_point
        <
            typename select_calculation_type
                <
                    Point1,
                    Point2,
                    CalculationType
                >::type
        >::type calculation_type;

    inline haversine(calculation_type const& r = 1.0)
        : m_radius(r)
    {}


    static inline calculation_type apply(Point1 const& p1, Point2 const& p2)
    {
        return calculate(get_as_radian<0>(p1), get_as_radian<1>(p1),
                        get_as_radian<0>(p2), get_as_radian<1>(p2));
    }

    inline calculation_type radius() const
    {
        return m_radius;
    }


private :

    static inline calculation_type calculate(calculation_type const& lon1,
            calculation_type const& lat1,
            calculation_type const& lon2,
            calculation_type const& lat2)
    {
        return math::hav(lat2 - lat1)
                + cos(lat1) * cos(lat2) * math::hav(lon2 - lon1);
    }

    calculation_type m_radius;
};



} // namespace comparable

/*!
\brief Distance calculation for spherical coordinates
on a perfect sphere using haversine
\ingroup strategies
\tparam Point1 \tparam_first_point
\tparam Point2 \tparam_second_point
\tparam CalculationType \tparam_calculation
\author Adapted from: http://williams.best.vwh.net/avform.htm
\see http://en.wikipedia.org/wiki/Great-circle_distance
\note It says: <em>The great circle distance d between two
points with coordinates {lat1,lon1} and {lat2,lon2} is given by:
    d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
A mathematically equivalent formula, which is less subject
    to rounding error for short distances is:
    d=2*asin(sqrt((sin((lat1-lat2)/2))^2
    + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
    </em>

\qbk{
[heading See also]
[link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
}

*/
template
<
    typename Point1,
    typename Point2 = Point1,
    typename CalculationType = void
>
class haversine
{
    typedef comparable::haversine<Point1, Point2, CalculationType> comparable_type;

public :

    typedef typename services::return_type<comparable_type>::type calculation_type;

    /*!
    \brief Constructor
    \param radius radius of the sphere, defaults to 1.0 for the unit sphere
    */
    inline haversine(calculation_type const& radius = 1.0)
        : m_radius(radius)
    {}

    /*!
    \brief applies the distance calculation
    \return the calculated distance (including multiplying with radius)
    \param p1 first point
    \param p2 second point
    */
    inline calculation_type apply(Point1 const& p1, Point2 const& p2) const
    {
        calculation_type const a = comparable_type::apply(p1, p2);
        calculation_type const c = calculation_type(2.0) * asin(sqrt(a));
        return m_radius * c;
    }

    /*!
    \brief access to radius value
    \return the radius
    */
    inline calculation_type radius() const
    {
        return m_radius;
    }

private :
    calculation_type m_radius;
};




( run in 0.538 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )