Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bounds function #6

Merged
merged 2 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Geohash.adjacent("abx1","n")
# "abx4"
```

- Get bounds

```Elixir
Geohash.bounds("u4pruydqqv")
# %{min_x: 10.407432317733765, min_y: 57.649109959602356, max_x: 10.407443046569824, max_y: 57.649115324020386}
```

## Installation

1. Add geohash to your list of dependencies in `mix.exs`:
Expand Down
35 changes: 31 additions & 4 deletions lib/geohash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ defmodule Geohash do
|> bits_to_coordinates_pair
end

def bounds(geohash) do
geohash
|> decode_to_bits
|> bits_to_bounds
end

@doc ~S"""
Decodes given geohash to a bitstring

Expand All @@ -149,16 +155,33 @@ defmodule Geohash do
defp bits_to_coordinates_pair(bits) do
bitslist = for << bit::1 <- bits >>, do: bit
lat = bitslist
|> min_max_lat
|> rounded_middle
lon = bitslist
|> min_max_lon
|> rounded_middle
{lat, lon}
end

defp bits_to_bounds(bits) do
bitslist = for << bit::1 <- bits >>, do: bit
{min_lat, max_lat} = min_max_lat(bitslist)
{min_lon, max_lon} = min_max_lon(bitslist)
%{min_x: min_lon, min_y: min_lat, max_x: max_lon, max_y: max_lat}
end

defp min_max_lat(bitlist) do
bitlist
|> filter_odd
|> Enum.reduce(fn (bit, acc) -> <<acc::bitstring, bit::bitstring>> end)
|> bits_to_coordinate({-90.0, 90.0})
end

lon = bitslist
defp min_max_lon(bitlist) do
bitlist
|> filter_even
|> Enum.reduce(fn (bit, acc) -> <<acc::bitstring, bit::bitstring>> end)
|> bits_to_coordinate({-180.0, 180.0})

{lat, lon}
end

@neighbor %{
Expand Down Expand Up @@ -270,12 +293,16 @@ defmodule Geohash do
(min + max) / 2
end

defp bits_to_coordinate(<<>>, min_max) do
defp rounded_middle(min_max) do
min_max
|> middle
|> round_coordinate(min_max)
end

defp bits_to_coordinate(<<>>, min_max) do
min_max
end

defp bits_to_coordinate(bits, {min, max}) do
<< bit::1, rest::bitstring >> = bits
mid = middle(min, max)
Expand Down
4 changes: 4 additions & 0 deletions test/geohash_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ defmodule GeohashTest do
assert Geohash.encode(-25.38262, -49.26561, 8) == "6gkzwgjz"
end

test "Geohash.bounds" do
assert Geohash.bounds("u4pruydqqv") == %{min_x: 10.407432317733765, min_y: 57.649109959602356, max_x: 10.407443046569824, max_y: 57.649115324020386}
end

test "Geohash.encode matches elasticsearch geohash example" do
assert Geohash.encode(51.501568, -0.141257, 1) == "g"
assert Geohash.encode(51.501568, -0.141257, 2) == "gc"
Expand Down