Skip to content

Instantly share code, notes, and snippets.

@jney
Created April 25, 2012 20:42
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jney/49658505d40e11cb1769 to your computer and use it in GitHub Desktop.
Programing Problem: Decimalize Latitude Longitude
# 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