Skip to content

Commit

Permalink
#16
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-pr-p committed Jul 19, 2017
1 parent f08ad44 commit 22f908b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
34 changes: 22 additions & 12 deletions lib/clients/district/district.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,34 @@ defmodule District do
def get_gjs, do: @geojsons
def get_polygon_of(district), do: @geojsons[district]

defp is_short_form(string) do
case Regex.run(~r/[A-Za-z][A-Za-z]-[0-9]+/, string) do
def is_short_form(string) do
case Regex.run(~r/[A-Za-z][A-Za-z][-]?[0-9]+/, string) do
nil -> false
_ -> true
end
end

def normalize(string) do
[state, cd] = String.split(string, "-")

state = String.upcase(state)

cd = case String.length(cd) do
1 -> "0" <> cd
2 -> cd
cond do
String.contains?(string, "-") ->
[state, cd] = String.split(string, "-")
state = String.upcase(state)
cd = case String.length(cd) do
1 -> "0" <> cd
2 -> cd
end
"#{state}-#{cd}"

String.length(string) === 3 ->
state = string |> String.slice(0..1) |> String.upcase()
cd = "0" <> String.slice(string, 2..2)
"#{state}-#{cd}"

String.length(string) === 4 ->
state = string |> String.slice(0..1) |> String.upcase()
cd = String.slice(string, 2..3)
"#{state}-#{cd}"
end

"#{state}-#{cd}"
end

def from_point({lat, lng}) do
Expand Down Expand Up @@ -141,7 +151,7 @@ defmodule District do
:math.sqrt((:math.pow(y2 - y1, 2) + :math.pow(x2 - x1, 2)))
end

def naive_distance({x1, y1}, {x2, y2}) do
def naive_distance_in_miles({x1, y1}, {x2, y2}) do
(:math.sqrt((:math.pow(y2 - y1, 2) + :math.pow(x2 - x1, 2)))) * @miles_per_degree
end
end
2 changes: 1 addition & 1 deletion web/channels/events_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Core.EventsChannel do

near_user = Enum.filter events, fn
%{location: %{latitude: lat, longitude: lng}} ->
District.naive_distance({lat, lng}, centroid) < 20
District.naive_distance_in_miles({lat, lng}, centroid) < 20
_else ->
false
end
Expand Down

0 comments on commit 22f908b

Please sign in to comment.