Created
March 6, 2015 10:25
-
-
Save pandazx/b9076c2d9b0b25f23fd5 to your computer and use it in GitHub Desktop.
Geohashの計算ロジックの勉強
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# | |
# Geohash encode/decode Test for tmp binary | |
# | |
# | |
LEVEL = 8 | |
LENGTH = LEVEL*5/2 | |
def encode(lon, str, min, max) | |
return str if str.size == LENGTH | |
puts d1 = max - lon | |
puts d2 = lon - min | |
# ライブラリによっては、d1 == d2 の時の挙動が異なる | |
if d1 < d2 | |
str += "1" | |
min += (max - min) / 2 | |
else | |
str += "0" | |
max -= (max - min) / 2 | |
end | |
puts "#{str},#{min},#{max}" | |
return encode(lon, str, min, max) | |
end | |
def decode(arr, min, max) | |
return if arr.size == 0 | |
puts arr.join() | |
val = arr.shift | |
case val | |
when "0" | |
max -= (max - min) / 2 | |
when "1" | |
min += (max - min) / 2 | |
else | |
raise "ERROR" | |
end | |
puts "#{val},#{min},#{max}" | |
decode(arr, min, max) | |
end | |
lon = 138.818702697754 | |
lat = 36.9964599609375 | |
puts "encode test: input #{lon}" | |
puts encode(lon, "", min=-180.0, max=180.0) | |
puts "--------------------------------------------" | |
puts "encode test: input #{lat}" | |
puts encode(lat, "", min=-90.0, max=90.0) | |
puts "--------------------------------------------" | |
# 以下は上記緯度経度のencode後の文字列 | |
lon = "11100010101101110010" | |
lat = "10110100100111100000" | |
puts "decode test input: #{lon}" | |
decode(lon.split(""), min=-180.0, max=180.0) | |
puts "--------------------------------------------" | |
puts "decode test input: #{lat}" | |
decode(lat.split(""), min=-90.0, max=90.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment