Python: Convert DMS to DD (not sure if format is correct)

Viewed 57

I have a list of coordinates in DMS that I need to convert to DD. Code works, however, I think it's not splitting degrees, minutes and seconds the way I want, because once I import those coordinates to a map, they're not where I know they should be. The table I have has the following coordinates: enter image description here

I think instead of splitting coordinates as 30°06.499' N and -87°47.964'W, code is doing it as 30°06'499''N and -87°47'964''W. I DO NOT have seconds(") in my data, only minutes(06.499'), which is making my results slightly different. Do you know how to fix this?

My code is the following:

    import re
    import pandas as pd
    import csv
    
    #conversion function
    def dms2dd(degrees, minutes, seconds, direction):
        dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
        if direction == 'S' or direction == 'W':
            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])
        lng = dms2dd(parts[4], parts[5], parts[6], parts[7])
        return (lat, lng)
    
    #coordinates
    dd1 = parse_dms("30°06.499' N 87°47.964' W")
    dd2 = parse_dms("30°06.493' N 87°47.965' W")
    dd3 = parse_dms("30°06.497' N 87°47.965' W")
    dd4 = parse_dms("30°06.324' N 87°48.231' W")
print(dd1)
(30.238611111111112, -88.05111111111111)
0 Answers
Related