How to convert degree minute second to degree decimal

后端 未结 6 2258
小鲜肉
小鲜肉 2021-02-07 13:37

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

<
相关标签:
6条回答
  • 2021-02-07 13:54

    I have slightly modified the re:

    parts = re.split('[^\d\w\.]+', dms)
    

    And as @Falko advised to make it work, you can either use double double quotes or escape your quotes characters

    parse_dms("53°19\'51.8\"N")
    
    0 讨论(0)
  • 2021-02-07 13:57

    For multiple coordinates, you can read them in using pandas. Formatting is important - there should not be any white spaces. White spaces can be removed using the replace function. The outputs can be easily saved as a text file or spreadsheet. I just printed them for validation and rounded the decimals locations out to 4.

    ### read input file
    df = pd.read_excel('dms.xlsx')
    
    n = len(df)
    
    for i in range(n):
      Lat_d = round(parse_dms(df.Lat[i].replace(" ", "")),4)
      Long_d = round(parse_dms(df.Long[i].replace(" ", "")),4)
      print(Lat_d, Long_d)
    
    0 讨论(0)
  • 2021-02-07 14:03

    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""")
    
    0 讨论(0)
  • 2021-02-07 14:04

    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

    0 讨论(0)
  • 2021-02-07 14:04

    I know this is an old question, but for whoever is following along, just thought I'd point out that you seem to have incorrect logic in your dms2dd() function regarding the sign of your decimals. You have:

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

    But it should only be negative if the direction is West (W) of the Prime Meridian or South (S) of the equator. So it should rather be:

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

    Here's a quote from a thorough guide: https://www.ubergizmo.com/how-to/read-gps-coordinates/

    The coordinate for the line of latitude represents north of the Equator because it is positive. If the number is negative, it represents south of the Equator.

    [...] The coordinate for the line of longitude represents east of the Prime Meridian because it is positive. If the number is negative, it represents west of the Prime Meridian.

    0 讨论(0)
  • 2021-02-07 14:06

    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

    0 讨论(0)
提交回复
热议问题