Programing Problem: Decimalize Latitude Longitude
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
# Decimalize latitude longitude | |
# "37°26′36.42″N 06°15′14.28″W" --> (37.44345 -6.253966666666667) | |
$regex = %r{ | |
(?<lat_deg>\d{1,2})\D+ | |
(?<lat_min>\d{1,2})\D+ | |
(?<lat_sec>\d{1,2}(\.\d+))\D+ | |
(?<lat_hemi>[NS]) | |
\s+ | |
(?<lng_deg>\d{1,3})\D+ | |
(?<lng_min>\d{1,2})\D+ | |
(?<lng_sec>\d{1,2}(\.\d+))\D+ | |
(?<lng_hemi>[EW]) | |
}x | |
def decimalize_location(location) | |
if g = $regex.match(location) | |
lat = g['lat_deg'].to_i + g['lat_min'].to_f/60 + g['lat_sec'].to_f/3600 | |
lat = -lat if g['lat_hemi'] == 'S' | |
lng = g['lng_deg'].to_i + g['lng_min'].to_f/60 + g['lng_sec'].to_f/3600 | |
lng = -lng if g['lng_hemi'] == 'W' | |
{y:lat, x:lng} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment