Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Batch convert GPS positions to Lat Lon decimals

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 these to two floating point numbers for Decimal Latitude and Decimal Longitude?

EDIT:

So an example would be: 0°25'30"S, 91°7'W -> 0.425, 91.116667 where the original single field position yields two floats.

Any help much appreciated.

like image 852
Darwin Tech Avatar asked Jun 01 '12 15:06

Darwin Tech


2 Answers

This approach can deal with seconds and minutes being absent, and I think handles the compass directions correctly:

# -*- coding: latin-1 -*-

def conversion(old):
    direction = {'N':1, 'S':-1, 'E': 1, 'W':-1}
    new = old.replace(u'°',' ').replace('\'',' ').replace('"',' ')
    new = new.split()
    new_dir = new.pop()
    new.extend([0,0,0])
    return (int(new[0])+int(new[1])/60.0+int(new[2])/3600.0) * direction[new_dir]

lat, lon = u'''0°25'30"S, 91°7'W'''.split(', ')
print conversion(lat), conversion(lon)
#Output:
0.425 91.1166666667
like image 155
fraxel Avatar answered Sep 27 '22 19:09

fraxel


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<lat_deg>\d+)°      # Latitude Degrees
                         (?:(?P<lat_min>\d+)')? # Latitude Minutes (Optional)
                         (?:(?P<lat_sec>\d+)")? # Latitude Seconds (Optional)
                         (?P<north_south>[NS])  # North or South
                         ,[ ]
                         (?P<lon_deg>\d+)°      # Longitude Degrees
                         (?:(?P<lon_min>\d+)')? # Longitude Minutes (Optional)
                         (?:(?P<lon_sec>\d+)")? # Longitude Seconds (Optional)
                         (?P<east_west>[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)
like image 30
Jon-Eric Avatar answered Sep 27 '22 21:09

Jon-Eric