# Programing Language: latitude-longitude-decimalize

120 views

### Xah Lee

Nov 29, 2011, 5:53:15 PM11/29/11
to
fun programing exercise. Write a function “latitude-longitude-
decimalize”.

It should take a string like this: 「"37°26′36.42″N 06°15′14.28″W"」.
The return value should be a pair of numbers, like this: 「[37.44345
-6.25396]」.

Feel free to use perl, python, ruby, lisp, etc. I'll post a emacs lisp
solution in a couple of days.

Xah

### Micky Hulse

Nov 29, 2011, 6:29:29 PM11/29/11
to pytho...@python.org
Last time I did this was using AS3. The format I used was DMS:

GPSLongitude: 122,42,47.79
GPSLongitudeRef: W
GPSLatitude: 45,30,30.390001198897014
GPSLatitudeRef: N

Here's the method:

<https://gist.github.com/1407126>

Not shown in above code: If West longitude or South latitude I would
make that DD (decimal degree) value negative.

Anyway, that was a fun learning experience! :)

### Ian Kelly

Nov 29, 2011, 6:49:18 PM11/29/11
to Xah Lee, pytho...@python.org
For Python 3:

import re

def latitude_longitude_decimalize(string):
regex = r"""(\d+)\xb0(\d+)'([\d+.]+)"([NS])\s*(\d+)\xb0(\d+)'([\d+.]+)"([EW])"""
match = re.match(regex, string)
if not match:
raise ValueError("Invalid input string: {0:r}".format(string))
def decimalize(degrees, minutes, seconds, direction):
decimal = int(degrees) + int(minutes) / 60 + float(seconds) / 3600
if direction in 'SW':
decimal = -decimal
return decimal
latitude = decimalize(*match.groups()[:4])
longitude = decimalize(*match.groups()[4:8])
return latitude, longitude

Nov 29, 2011, 6:50:33 PM11/29/11
to
What a waste of time when the following works fine (probably Java):

<http://transition.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html>

### Jürgen Exner

Nov 29, 2011, 8:14:23 PM11/29/11
to
>On 11/29/2011 2:53 PM, Xah Lee wrote:

Thanks

jue

Nov 29, 2011, 9:10:17 PM11/29/11
to
Mea culpa, you're correct. I responded only because the subject
is something with which I'm familiar, e.g., one of my posts from
1988 (23 years ago):