geohash.rb Geohash library for pure ruby Distributed under the MIT License
Based library is // github.com/davetroy/geohash-js/blob/master/geohash.js // geohash.js // Geohash library for Javascript // © 2008 David Troy // Distributed under the MIT License
Calculate adjacents geohash
# File lib/pr_geohash.rb, line 66 def adjacent(geohash, dir) base, lastChr = geohash[0..-2], geohash[-1,1] type = (geohash.length % 2)==1 ? :odd : :even if BORDERS[dir][type].include?(lastChr) base = adjacent(base, dir) end base + BASE32[NEIGHBORS[dir][type].index(lastChr),1] end
Decode from geohash
geohash code
decoded bounding box [[north latitude, west longitude],[south latitude, east longitude]]
# File lib/pr_geohash.rb, line 22 def decode(geohash) latlng = [[-90.0, 90.0], [-180.0, 180.0]] is_lng = 1 geohash.downcase.scan(/./) do |c| BITS.each do |mask| latlng[is_lng][(BASE32.index(c) & mask)==0 ? 1 : 0] = (latlng[is_lng][0] + latlng[is_lng][1]) / 2 is_lng ^= 1 end end latlng.transpose end
Encode latitude and longitude into geohash
# File lib/pr_geohash.rb, line 37 def encode(latitude, longitude, precision=12) latlng = [latitude, longitude] points = [[-90.0, 90.0], [-180.0, 180.0]] is_lng = 1 (0...precision).map { ch = 0 5.times do |bit| mid = (points[is_lng][0] + points[is_lng][1]) / 2 points[is_lng][latlng[is_lng] > mid ? 0 : 1] = mid ch |= BITS[bit] if latlng[is_lng] > mid is_lng ^= 1 end BASE32[ch,1] }.join end
Calculate neighbors (8 adjacents) geohash
# File lib/pr_geohash.rb, line 56 def neighbors(geohash) [[:top, :right], [:right, :bottom], [:bottom, :left], [:left, :top]].map{ |dirs| point = adjacent(geohash, dirs[0]) [point, adjacent(point, dirs[1])] }.flatten end
Calculate adjacents geohash
# File lib/pr_geohash.rb, line 66 def adjacent(geohash, dir) base, lastChr = geohash[0..-2], geohash[-1,1] type = (geohash.length % 2)==1 ? :odd : :even if BORDERS[dir][type].include?(lastChr) base = adjacent(base, dir) end base + BASE32[NEIGHBORS[dir][type].index(lastChr),1] end
Decode from geohash
geohash code
decoded bounding box [[north latitude, west longitude],[south latitude, east longitude]]
# File lib/pr_geohash.rb, line 22 def decode(geohash) latlng = [[-90.0, 90.0], [-180.0, 180.0]] is_lng = 1 geohash.downcase.scan(/./) do |c| BITS.each do |mask| latlng[is_lng][(BASE32.index(c) & mask)==0 ? 1 : 0] = (latlng[is_lng][0] + latlng[is_lng][1]) / 2 is_lng ^= 1 end end latlng.transpose end
Encode latitude and longitude into geohash
# File lib/pr_geohash.rb, line 37 def encode(latitude, longitude, precision=12) latlng = [latitude, longitude] points = [[-90.0, 90.0], [-180.0, 180.0]] is_lng = 1 (0...precision).map { ch = 0 5.times do |bit| mid = (points[is_lng][0] + points[is_lng][1]) / 2 points[is_lng][latlng[is_lng] > mid ? 0 : 1] = mid ch |= BITS[bit] if latlng[is_lng] > mid is_lng ^= 1 end BASE32[ch,1] }.join end
Calculate neighbors (8 adjacents) geohash
# File lib/pr_geohash.rb, line 56 def neighbors(geohash) [[:top, :right], [:right, :bottom], [:bottom, :left], [:left, :top]].map{ |dirs| point = adjacent(geohash, dirs[0]) [point, adjacent(point, dirs[1])] }.flatten end