-
-
Save runeb/938687 to your computer and use it in GitHub Desktop.
Conversions between latitude, longitude and Maidenhead Locator System
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
module MLS | |
extend self | |
GRID = ("A".."R").to_a | |
SQUARE = ("0".."9").to_a | |
SUB = ("a".."x").to_a | |
deg = 180.0 | |
PARTS = [[18, GRID], [10, SQUARE], [24, SUB], [10, SQUARE]] | |
PARTS.map! { |div, part| [deg = deg / div, part] } | |
def from_latlng(lat, lng, precision = 4) | |
lat += 90 | |
lng += 180 | |
PARTS[0...precision].inject("") do |res, (div, part)| | |
part_lat, lat = lat.divmod(div) | |
part_lng, lng = lng.divmod(div * 2) | |
res << part[part_lng] << part[part_lat] | |
end | |
end | |
def to_latlng(mls) | |
mls = mls.split('').reverse | |
lat = lng = m_index = 0 | |
PARTS[0...mls.length / 2].reverse.each do |value, part| | |
lat += value * part.find_index(mls[m_index]) | |
lng += value * part.find_index(mls[m_index + 1]) * 2 | |
m_index += 2 | |
end | |
[lat - 90, lng - 180] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment