How to convert degree minute second to degree decimal

bikuser picture bikuser · Nov 30, 2015 · Viewed 29.1k times · Source

I receive the latitude and longitude from GPS with this format:

Latitude : 78°55'44.29458"N

I need convert this data to:

latitude: 78.9288888889

I found this code here: link

import re

def dms2dd(degrees, minutes, seconds, direction):
    dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
    if direction == 'E' or direction == 'N':
        dd *= -1
    return dd;

def dd2dms(deg):
    d = int(deg)
    md = abs(deg - d) * 60
    m = int(md)
    sd = (md - m) * 60
    return [d, m, sd]

def parse_dms(dms):
    parts = re.split('[^\d\w]+', dms)
    lat = dms2dd(parts[0], parts[1], parts[2], parts[3])

    return (lat)

dd = parse_dms("78°55'44.33324"N )

print(dd)

It is working for for this format

dd = parse_dms("78°55'44.33324'N" )

but it is not working for my datafromat. Can anyone help me to solve this problem?

Answer

Rodrigo Santiago picture Rodrigo Santiago · Jun 23, 2018

The function above (dms2dd) is incorrect.

Actual (With error):

if direction == 'E' or direction == 'N': dd *= -1

Corrected Condition:

if direction == 'W' or direction == 'S': dd *= -1