Created
August 10, 2011 22:22
-
-
Save mivanov/1138441 to your computer and use it in GitHub Desktop.
Convert latitude/longitude string in degrees to decimal
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
import re | |
def parse_lonlat(coord): | |
""" Pass in string in degrees like "( 24d37'55.25\"W, 73d42'10.75\"S)" | |
Returns decimal tuple (lon, lat) | |
""" | |
latlon_regex = r"\(\s*(\d+)d(\d+)'([\d.]+)\"([WE]),\s*(\d+)d(\d+)'([\d.]+)\"([NS])\s*\)" | |
m = re.match(latlon_regex, coord) | |
parts = m.groups() | |
lat = int(parts[0]) + float(parts[1]) / 60 + float(parts[2]) / 3600 | |
if parts[3] == 'W': | |
lat *= -1 | |
lon = int(parts[4]) + float(parts[5]) / 60 + float(parts[6]) / 3600 | |
if parts[7] == 'S': | |
lon *= -1 | |
return (lon, lat) | |
print parse_lonlat("( 24d37'55.25\"W, 73d42'10.75\"S)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment