Skip to content

Commit

Permalink
add geohash
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Jul 31, 2022
1 parent 5519121 commit 0a745b3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies:
github: geocrystal/haversine
convex_hull:
github: geocrystal/convex_hull
geohash:
github: geocrystal/geohash

development_dependencies:
ameba:
Expand Down
9 changes: 9 additions & 0 deletions spec/geo/coord_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ describe Geo::Coord do
end
end

context "geohash" do
context "encode to geohash" do
pos = Geo::Coord.new(50.004444, 36.231389)

it { pos.geohash.should eq("ubcu2rnbuxcx") }
it { pos.geohash(5).should eq("ubcu2") }
end
end

context "to_s" do
context "is convertible to string" do
pos = Geo::Coord.new(50.004444, 36.231389)
Expand Down
1 change: 1 addition & 0 deletions src/geo.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "convex_hull"
require "geohash"
require "./geo/utils"
require "./geo/coord"
require "./geo/polygon"
Expand Down
5 changes: 5 additions & 0 deletions src/geo/coord.cr
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ module Geo
end
end

# Returns a geohash representing coordinates.
def geohash(precision = 12)
Geohash.encode(lat.to_f, lng.to_f, precision)
end

# Returns a string representing coordinates.
#
# ```
Expand Down

0 comments on commit 0a745b3

Please sign in to comment.