class GeoHashr::GeoHash
Attributes
box[RW]
geohash[RW]
x[RW]
y[RW]
Public Class Methods
[](geohash)
click to toggle source
# File lib/geohashr/geohash.rb, line 5 def self.[](geohash) new(geohash) end
new(geohash)
click to toggle source
# File lib/geohashr/geohash.rb, line 9 def initialize(geohash) @geohash = geohash decode end
Public Instance Methods
adjacent(dir, source_hash=geohash)
click to toggle source
# File lib/geohashr/geohash.rb, line 59 def adjacent(dir, source_hash=geohash) source_hash = source_hash.to_s base, lastChr = source_hash[0..-2], source_hash[-1,1] type = (source_hash.length % 2)==1 ? :odd : :even p [source_hash, dir, type, base, lastChr] if BORDERS[dir][type].include?(lastChr) p [:before, dir, base] base = adjacent(dir, base) end GeoHash.new(base + BASE32[NEIGHBORS[dir][type].index(lastChr),1]) end
decode()
click to toggle source
# File lib/geohashr/geohash.rb, line 14 def decode @box = [[-90.0, 90.0], [-180.0, 180.0]] is_lng = 1 positions = ['',''] geohash.downcase.scan(/./) do |c| BITS.each do |mask| bit = (BASE32.index(c) & mask).zero? ? 1 : 0 refine = box[is_lng] positions[is_lng] += (bit ^ 1).to_s refine[bit] = (refine[0] + refine[1]) / 2 is_lng ^= 1 end end @x = positions.last.to_i(2) @y = positions.first.to_i(2) @box = box.transpose end
inspect()
click to toggle source
# File lib/geohashr/geohash.rb, line 88 def inspect "#<GeoHash:#{geohash} #{x},#{y} #{box}>" end
neighbors()
click to toggle source
Calculate neighbors (8 adjacents) geohash
# File lib/geohashr/geohash.rb, line 52 def neighbors [[:top, :right], [:right, :bottom], [:bottom, :left], [:left, :top]].map{ |dirs| point = adjacent(dirs[0]) [point, adjacent(dirs[1], point)] }.flatten end
to_s()
click to toggle source
GeoHash.new(base + BASE32[NEIGHBORS[dir][type].index(lastChr),1])
end
# File lib/geohashr/geohash.rb, line 84 def to_s geohash end