From 0a745b3a72e172d7e0efe4b2d8ff763b9bccb91e Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Sun, 31 Jul 2022 17:02:12 +0300 Subject: [PATCH] add geohash --- shard.yml | 2 ++ spec/geo/coord_spec.cr | 9 +++++++++ src/geo.cr | 1 + src/geo/coord.cr | 5 +++++ 4 files changed, 17 insertions(+) diff --git a/shard.yml b/shard.yml index b89b88b..f586fc0 100644 --- a/shard.yml +++ b/shard.yml @@ -8,6 +8,8 @@ dependencies: github: geocrystal/haversine convex_hull: github: geocrystal/convex_hull + geohash: + github: geocrystal/geohash development_dependencies: ameba: diff --git a/spec/geo/coord_spec.cr b/spec/geo/coord_spec.cr index c8a588a..e9bf473 100644 --- a/spec/geo/coord_spec.cr +++ b/spec/geo/coord_spec.cr @@ -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) diff --git a/src/geo.cr b/src/geo.cr index 433b022..abf09f0 100644 --- a/src/geo.cr +++ b/src/geo.cr @@ -1,4 +1,5 @@ require "convex_hull" +require "geohash" require "./geo/utils" require "./geo/coord" require "./geo/polygon" diff --git a/src/geo/coord.cr b/src/geo/coord.cr index d81d020..094fa4b 100644 --- a/src/geo/coord.cr +++ b/src/geo/coord.cr @@ -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. # # ```