ついでにRuby版

ruby書くの2年ぶりぐらいかも。

def geopoEncode(latitude, longitude, scale)
  # 64characters (number + big and small letter + hyphen + underscore)
  chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
  geopo = ""

  # Change a degree measure to a decimal number
  latitude = (latitude + 90) / 180 * (8** 10)
  longitude = (longitude + 180) / 360 * (8** 10)

  # Compute a GeoPo code from head and concatenate
  for i in 0..scale
    geopo += chars[(latitude / (8 ** (9 - i)) % 8).floor + (longitude / (8 ** (9 - i)) % 8).floor * 8,1]
  end
  return geopo;
end

def geopoDecode(geopo)
  chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
  latitude = 0
  longitude = 0
  for i in 0..(geopo.size-1)
    # What number of character that equal to a GeoPo code (0-63)
    order = chars.index(geopo[i])
    # Lat/Lng plus geolocation value of scale
    latitude  = latitude  + (order % 8).floor * (8 ** (9 - i))
    longitude = longitude + (order / 8).floor * (8 ** (9 - i))
  end

  # Change a decimal number to a degree measure, and plus revised value that shift center of area
  latitude = latitude * 180.0 / (8 ** 10) + 180 / (8 ** geopo.size) / 2 - 90
  longitude = longitude * 360.0 / (8 ** 10) + 360 / (8 ** geopo.size) / 2 - 180
  scale = geopo.size

  return latitude, longitude, scale
end

p geopoEncode(35.658578, 139.745447, 6) # -> Z4RHXX
p geopoEncode(48.858271, 2.294512, 6)   # -> C1qn6P
p geopoEncode(31.658578, 139.745447, 3) # -> Z3O
p geopoDecode("Z4RHXX")
p geopoDecode("C1qn6P")
p geopoDecode("Z30")

rangeの指定の仕方をすっかり忘れてたのでちょっと時間かかっちゃった。