Python - Batch convert GPS positions to Lat Lon decimals

后端 未结 3 1033
故里飘歌
故里飘歌 2020-12-20 17:00

Hi I have a legacy db with some positional data. The fields are just text fields with strings like this 0°25\'30\"S, 91°7\'W. Is there some way I can convert th

3条回答
  •  误落风尘
    2020-12-20 17:30

    This converts your input string to your expected output. It can handle minutes and seconds not being present.

    Currently, it does not account for North/South, East/West. If you'll tell me how you'd like those handled, I'll update the answer.

    # -*- coding: latin-1 -*-
    import re
    
    PATTERN = re.compile(r"""(?P\d+)°      # Latitude Degrees
                             (?:(?P\d+)')? # Latitude Minutes (Optional)
                             (?:(?P\d+)")? # Latitude Seconds (Optional)
                             (?P[NS])  # North or South
                             ,[ ]
                             (?P\d+)°      # Longitude Degrees
                             (?:(?P\d+)')? # Longitude Minutes (Optional)
                             (?:(?P\d+)")? # Longitude Seconds (Optional)
                             (?P[EW])    # East or West
                          """, re.VERBOSE)
    
    LAT_FIELDS = ("lat_deg", "lat_min", "lat_sec")
    LON_FIELDS = ("lon_deg", "lon_min", "lon_sec")
    
    def parse_dms_string(s, out_type=float):
        """
        Convert a string of the following form to a tuple of out_type latitude, longitude.
    
        Example input:
        0°25'30"S, 91°7'W
        """
        values = PATTERN.match(s).groupdict()
    
        return tuple(sum(out_type(values[field] or 0) / out_type(60 ** idx) for idx, field in enumerate(field_names)) for field_names in (LAT_FIELDS, LON_FIELDS))
    
    
    INPUT = """0°25'30"S, 91°7'W"""
    
    print parse_dms_string(INPUT) # Prints: (0.42500000000000004, 91.11666666666666)
    

提交回复
热议问题