Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert degree minute second to degree decimal

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 == 'S':
        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?

like image 417
bikuser Avatar asked Nov 30 '15 11:11

bikuser


People also ask

When you convert degrees minutes seconds to decimal you need to divide minutes with?

Converting Degrees, Minutes, and Seconds to Decimal Degrees Step 1: Convert the minutes to degrees by dividing by 60. Step 2: Convert the seconds to degrees by dividing by 3,600. Step 3: Find the decimal degree by adding the degrees to the results from steps 1 and 2.

How do you convert angle measures to decimal degrees?

Add up the integer number of degrees and minute/second fractions to convert the angle magnitude into the decimal form. In this example, the angle of 27 degrees, 12 minutes and 45 seconds corresponds to 27 + 0.2 + 0.0125 = 27.2125 degrees.


4 Answers

Here's my one liner (fine, fine – maybe it's two lines) :)

import re
lat = '''51°36'9.18"N'''
deg, minutes, seconds, direction =  re.split('[°\'"]', lat)
(float(deg) + float(minutes)/60 + float(seconds)/(60*60)) * (-1 if direction in ['W', 'S'] else 1)

This outputs 51.60255

like image 59
Inti Avatar answered Sep 30 '22 04:09

Inti


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

like image 29
Rodrigo Santiago Avatar answered Sep 30 '22 03:09

Rodrigo Santiago


The problem is that the seconds 44.29458 are split at ..

You could either define the split characters directly (instead of where not to split):

>>> re.split('[°\'"]+', """78°55'44.29458"N""")
['78', '55', '44.29458', 'N']

or leave the regular expression as it is and merge parts 2 and 3:

dms2dd(parts[0], parts[1], parts[2] + "." + parts[3], parts[4])

Update:

Your method call dd = parse_dms("78°55'44.33324"N ) is a syntax error. Add the closing " and escape the other one. Or use tripple quotes for the string definition:

parse_dms("""78°55'44.29458"N""")
like image 44
Falko Avatar answered Sep 30 '22 02:09

Falko


You can use this module https://pypi.org/project/dms2dec/

Convert DMS to Decimal.

Usage

from dms2dec.dms_convert import dms2dec

dms2dec('''36°44'47.69"N''') # converts to dec
dms2dec('''3° 2'33.53"E''') # converts to dec
like image 24
Victor Stanescu Avatar answered Sep 30 '22 03:09

Victor Stanescu