Heatherbee
Heatherbee

Reputation: 21

How do I convert these DMS geocoords to decimal in Python?

I have a very long list of urls and geocoords in a text file like this

42°21′50″N,71°06′05″W 35°29′23″N,77°58′57″W

and I need to select just the geocoords and convert the degrees minutes seconds (portrayed here as °′″) to decimal degrees.

I have found a way to do the conversion but I need them in the ° ' " format and I don't know how to reformat them. Help?

Upvotes: 2

Views: 216

Answers (2)

secretmike
secretmike

Reputation: 9884

This is a character encoding issue. Your text file is encoded using UTF-8 but your editor thinks it's a MacOS encoding (This one: see http://en.wikipedia.org/wiki/Mac_OS_Roman).

If you have the data above in a variable you can decode it like this:

my_coords = raw_data.decode("utf8")
print(my_coords)

This will give you something like this: 42°21′50″N,71°06′05″W 35°29′23″N,77°58′57″W

Note that the ° ′ and ″ are unicode U+B0 (DEGREE SIGN), U+2032 (PRIME), and U+2033 (DOUBLE PRIME) respectively.

You're not the only one to experience issues with this. President Barack Obama himself came across this :)

http://www.hanselman.com/blog/WhyTheAskObamaTweetWasGarbledOnScreenKnowYourUTF8UnicodeASCIIAndANSIDecodingMrPresident.aspx

Upvotes: 1

jcollado
jcollado

Reputation: 40384

I think you're looking for something like this:

>>> coords = "42°21′50″N,71°06′05″W 35°29′23″N,77°58′57″W"
>>> coords.replace('¬∞', '°').replace('Ä≤', "'").replace('Ä≥', '"')
42°21‚'50‚"N,71°06‚'05‚"W 35°29‚'23‚"N,77°58‚'57‚"W

Basically, you can chain some str.replace calls to perform the replacements needed.

Anyway, this is probably related to some encoding problem, so it would be worth to have a look at the original data to find out what encoding is bein used.

Upvotes: 1

Related Questions