Programing Language: latitude-longitude-decimalize

X

Xah Lee

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
 
M

Micky Hulse

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! :)
 
I

Ian Kelly

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.

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
 
T

Thad Floryan

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.

What a waste of time when the following works fine (probably Java):

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top