-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix #71: Add Haversine distance * Add Haversine to README.md * Add Haversine to benchmarks * Add benchmark results for Haversine to README.md * Add helper function haversine() with default Earth radius * Add tests for Haversine distance * Use 4 spaces for indentation * Use 4 spaces for indentation on test suite * Remove default radius for Haversine distance * Fix typo in Haversine tests * fixups
- Loading branch information
1 parent
a6de2f8
commit 2b0ab92
Showing
6 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Haversine(radius) | ||
The haversine distance between two locations on a sphere of given `radius`. | ||
Locations are described with longitude and latitude in degrees. | ||
The computed distance has the same units as that of the radius. | ||
""" | ||
struct Haversine{T<:Real} <: Metric | ||
radius::T | ||
end | ||
|
||
const VecOrLengthTwoTuple{T} = Union{AbstractVector{T}, NTuple{2, T}} | ||
|
||
function evaluate(dist::Haversine, x::VecOrLengthTwoTuple, y::VecOrLengthTwoTuple) | ||
length(x) == length(y) == 2 || haversine_error() | ||
|
||
@inbounds begin | ||
# longitudes | ||
Δλ = deg2rad(y[1] - x[1]) | ||
|
||
# latitudes | ||
φ₁ = deg2rad(x[2]) | ||
φ₂ = deg2rad(y[2]) | ||
end | ||
|
||
Δφ = φ₂ - φ₁ | ||
|
||
# haversine formula | ||
a = sin(Δφ/2)^2 + cos(φ₁)*cos(φ₂)*sin(Δλ/2)^2 | ||
c = 2atan2(√a, √(1-a)) | ||
|
||
# distance on the sphere | ||
c*dist.radius | ||
end | ||
|
||
haversine(x::VecOrLengthTwoTuple, y::VecOrLengthTwoTuple, radius::Real) = evaluate(Haversine(radius), x, y) | ||
|
||
@noinline haversine_error() = throw(ArgumentError("expected both inputs to have length 2 in Haversine distance")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters