Skip to content

Commit

Permalink
add default value of Earth's radius to Haversine
Browse files Browse the repository at this point in the history
I don't think it should be necessary to look up the radius of Earth every time one wants to use the Haversine formula, so I've added the equatorial radius of Earth in meters as a default. I hope most imperial unit users will agree that metric is a reasonable default for scientific libraries. Of course Earth has different radii depending on where it is measured, as it isn't a perfect sphere. Uses sensitive to this should use more specific values. FYI this same default value is used in R. source https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
  • Loading branch information
mkborregaard authored Sep 1, 2020
1 parent 44036b5 commit 74cfa17
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/haversine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The computed distance has the same units as that of the radius.
struct Haversine{T<:Real} <: Metric
radius::T
end
Haversine() = Haversine(6378137.0)

const VecOrLengthTwoTuple{T} = Union{AbstractVector{T}, NTuple{2, T}}

Expand All @@ -33,6 +34,6 @@ function (dist::Haversine)(x::VecOrLengthTwoTuple, y::VecOrLengthTwoTuple)
2 * dist.radius * asin( min(a, one(a)) ) # take care of floating point errors
end

haversine(x::VecOrLengthTwoTuple, y::VecOrLengthTwoTuple, radius::Real) = Haversine(radius)(x, y)
haversine(x::VecOrLengthTwoTuple, y::VecOrLengthTwoTuple, radius::Real = 6378137.0) = Haversine(radius)(x, y)

@noinline haversine_error() = throw(ArgumentError("expected both inputs to have length 2 in Haversine distance"))

0 comments on commit 74cfa17

Please sign in to comment.