Search code examples
python-3.xgps

Converting dd° mm.mmm' to dd.ddddd° (latitude & longitude) in Python 3


I have GPS coordinates in the form

Lat = 3648.5518 Lon = 17443.258

I want to convert then to the more traditional format

Lat = -36.8091667 Lon = 174.7209667

I can do this through string manipulation as shown below. However this approach seems very "wordy".Is there a better way (any inbuilt functionality) to achieve this conversion in Python 3.4? my searches have come up blank. Any advice would be much appreciated.

Steve (New to python/programming)

Lat = 3648.5518
Lat = str(Lat)
Latdd = float(Lat[:2])
Latmmmmm = float(Lat[2:])
Latddmmmm = Latdd+(Latmmmmm/60.0)
print(Latddmmmm)


Lon = 17443.258
Lon = str(Lon)
Londd = float(Lon[:3])
Lonmmmmm = float(Lon[3:])
Londdmmmm = Londd+(Lonmmmmm/60.0)
print(Londdmmmm)

Solution

  • It looks like the input is an abuse of decimal notation to place write deg min as 100*deg + min.

    You can extract the degree, minute and second parts with modulo division. Split into two functions for clarity.

    def dm(x):
        degrees = int(x) // 100
        minutes = x - 100*degrees
    
        return degrees, minutes
    
    def decimal_degrees(degrees, minutes):
        return degrees + minutes/60 
    
    print (decimal_degrees(*dm(3648.5518)))