Masthead

Converting Coordinates

1. Introduction

There are a lot of situations when you'll want to find strings or individual characters in another string. Let's say we have a coordinate that is in degrees, minutes, and seconds and we want to convert it to decimal degrees. An example would be: 40° 21' 32" E, 105° 30' 41" W. We need to breakup the string into pieces, convert the pieces to numeric values and then put them together.

2. Converting DMS to DD (more on parsing)

We are going to develop a function to convert a degree, minute, and second coordinate to decimal degrees. To get started we need to create a test strings to test the function. Take a look at the first line below and you'll see that we had to use ("\"") to insert a double-quote into a string. In the example below we've inserted a degree symbol with "\xf8" after the 40 and the 105.

TheCoordinate="40\xf8 21' 32\" E, 105\xf8 30' 40\""
print("Coordinate="+TheCoordinate) # print the coordinate so we can see it before the conversion

TheElements=TheCoordinate.split(",") # break up the coordinate into the latitude and longitude
TheLatitude=TheElements[0] 
TheLongitude=TheElements[1]
print("Latitude="+TheLatitude)
print("Longitude="+TheLongitude)

Now we're going to get each piece of the string so we need to find the degree symbol, the single quote, and the double quote. The "find()" function will return an index to the first occurrence of a string in another string. Notice that I'm printing out the index right after I get it. This is a good way to make sure your programs are working correctly. As soon as the code is working correctly, you can remove the print functions by commenting them out.

TheDegreeIndex=TheLatitude.find("\xf8")
print("TheDegreeIndex="+format(TheDegreeIndex))

TheQuoteIndex=TheLatitude.find("'")
print("TheQuoteIndex="+format(TheQuoteIndex))

TheDoubleQuoteIndex=TheLatitude.find("\"")
print("TheDoubleQuoteIndex="+format(TheDoubleQuoteIndex))

Next we can use the indexes to "substring" the string using indexes we discussed before. We need to get the degree, minute, second portion of the string but we also will need to get the letter that indicates if the latitude is in the Northern or Southern hemisphere and if the longitude is in the Eastern or Western hemisphere.

TheDegreeString=TheLatitude[0:TheDegreeIndex]
print("TheDegreeString="+TheDegreeString)

TheMinuteString=TheLatitude[TheDegreeIndex+1:TheQuoteIndex]
print("TheMinuteString="+TheMinuteString)

TheSecondString=TheLatitude[TheQuoteIndex+1:TheDoubleQuoteIndex]
print("TheSecondString="+TheSecondString)

TheHemisphereString=TheLatitude[TheDoubleQuoteIndex+1:]
print("TheHemisphereString="+TheHemisphereString)

Now we can convert the strings to values. Notice that I've added a "strip()" function to remove any white space:

TheDegreeValue=float(TheDegreeString.strip())
print("TheDegreeValue="+format(TheDegreeValue))

TheMinuteValue=float(TheMinuteString.strip())
print("TheMinuteValue="+format(TheMinuteValue))

TheSecondValue=float(TheSecondString.strip())
print("TheSecondValue="+format(TheSecondValue))

TheHemisphereLetter=TheHemisphereString.strip()
print("TheHemisphereLetter="+TheHemisphereLetter)

The final step is to convert the value of degrees, minutes, and seconds to a single decimal degree value. Since a minute is 1/60th of a degree we divide the minute value by 60 and add it to the degrees. Then we divide seconds by 1/3600th and add it to the value (3600 is 60 times 60).

TheLatitudeValue=TheDegreeValue+(TheMinuteValue/60)+(TheSecondValue/3600)
print("TheLatitudeValue="+format(TheLatitudeValue))

if (TheHemisphereLetter=="W" or TheHemisphereLetter=="S"):
 TheLatitudeValue= 0-TheLatitudeValue

While this was not particularly difficult code to write, there is a lot of it. Rather than repeating the code for longitude we can define a function that converts a degree, minute, and second value into a "function". It's easy to write your own functions in Python. You just define them with a "def", indent the contents of the function, and return the desired value.

def DMSToDecimalDegrees(DMSString):
 # put the contents of the function here.
 return TheLatitudeValue

TheCoordinate="40\xf8 21' 32\" E, 105\xf8 30' 40\" W"

TheElements=TheCoordinate.split(",")
TheLatitude=TheElements[0]
TheLongitude=TheElements[1]
print("TheLatitude="+TheLatitude)
print("TheLongitude="+TheLongitude)

TheLatitudeValue=DMSToDecimalDegrees(TheLatitude)
print("TheLatitudeValue="+TheLatitudeValue)

TheLongitudeValue=DMSToDecimalDegrees(TheLongitude)
print("TheLongitudeValue="+TheLongitudeValue)

Important: Make sure you change the name of "TheLatitude" in your function to DMSString. Otherwise, your function will use the global variable "TheLatitude" instead of the value you pass in. This means that your latitude and longitude will end up with the same value.

After you complete the function, run it several times using the debugger to step "into" the function and watch how it works.

Additional Resources

Python Documentation: String functions

Python Documentation: Defining Functions

 

© Copyright 2018 HSU - All rights reserved.