Skip to content

Instantly share code, notes, and snippets.

@ityonemo
Created June 1, 2017 01:39
Show Gist options
  • Save ityonemo/efb228fe5a2762c780d3cf0f30d59cdd to your computer and use it in GitHub Desktop.
Save ityonemo/efb228fe5a2762c780d3cf0f30d59cdd to your computer and use it in GitHub Desktop.
playing with elixir
defmodule Geoserver do
@moduledoc """
Documentation for Geoserver.
"""
@doc """
latlongdistance({ϕ1, λ1}, {ϕ2, λ2}) gives the distance, in kilometers, between
two points on the globe defined by {ϕ1, λ1}, {ϕ2, λ2}. Negative ϕ values are
in the southern hemisphere and negative λ are in the western hemisphere.
New York to Berlin:
iex> Geoserver.latlongdistance({40.6, -74.0}, {52.5, 13.1})
6375.72021777002
"""
def latlongdistance({ϕd1, λd1}, {ϕd2, λd2}) do
[ϕ1, λ1, ϕ2, λ2] = [ϕd1, λd1, ϕd2, λd2] |> Enum.map(fn x -> x * (:math.pi() / 180.0) end)
#calculate (x1,y1,z1), and (x2,y2,z2) and do the dot product.
dp = :math.cos(ϕ1) * :math.cos(λ1) * :math.cos(ϕ2) * :math.cos(λ2) + :math.cos(ϕ1) * :math.sin(λ1) * :math.cos(ϕ2) * :math.sin(λ2) + :math.sin(ϕ1) * :math.sin(ϕ2)
:math.acos(dp) * 6371.0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment