Created
March 31, 2016 19:51
-
-
Save adamrobbie/2eb3531d24fa783b2535540f13087803 to your computer and use it in GitHub Desktop.
Geodistance (havenshine function) in elixir
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
defmodule Havershine do | |
def distance(lng1, lat1, lng2, lat2) do | |
[rLng1, rLat1, rLng2, rLat2] = for deg <- [lng1, lat1, lng2, lat2], do: deg2rad(deg) | |
dLon = rLng2 - rLng1 | |
dLat = rLat2 - rLat1 | |
a = :math.pow(:math.sin(dLat/2), 2) + :math.cos(rLat1) * :math.cos(rLat2) * :math.pow(:math.sin(dLon/2), 2) | |
c = 2 * :math.asin(:math.sqrt(a)) | |
km = 6372.8 * c | |
end | |
defp deg2rad(deg), do: :math.pi()*deg/180 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment