Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.