Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Programing Problem: Decimalize Latitude Longitude

View gist:49658505d40e11cb1769
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 
# 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
Something went wrong with that request. Please try again.