Created
June 15, 2010 08:59
-
-
Save jrobertson/438873 to your computer and use it in GitHub Desktop.
This file contains 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
# converted from JavaScript; source: http://blog.jgc.org/2010/06/1010-code.html | |
def calculate_tt(lat, lon) | |
alphabet = "ABCDEFGHJKMNPQRVWXY0123456789".split(//) | |
base = alphabet.length | |
lat += 90.0 | |
lon += 180.0 | |
lat *= 10000.0 | |
lon *= 10000.0 | |
lat = lat.floor | |
lon = lon.floor | |
p = lat * 3600000.0 + lon | |
tt_num = p * base | |
c = 0 | |
(1..9).each do |i| | |
c += ( p % base ) * i | |
p = ( p / base ).floor | |
end | |
c %= 29 | |
tt_num += c | |
tt_num = tt_num.floor | |
tt = "" | |
(0..9).each do |i| | |
d = tt_num % base | |
if ( ( i == 4 ) || ( i == 7 ) ) then | |
tt = " " + tt | |
end | |
tt = alphabet.at(d.to_i).to_s + tt.to_s | |
tt_num = ( tt_num / base ).floor | |
end | |
return tt | |
end | |
calculate_tt 51.09559, 1.12207 | |
#=> "QPP YMD MGQA" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment