2012-06-01 116 views
7

您好我有一个具有一些位置数据的遗留数据库。这些字段只是像这样的字符串的文本字段0°25'30"S, 91°7'W。有什么办法可以将这些转换为Decimal LatitudeDecimal Longitude的两个浮点数?Python - 将GPS位置批量转换为经纬度小数点

编辑:

因此,一个例子是:0°25'30"S, 91°7'W - >0.42591.116667其中原始单场位置产生两个浮动。

任何帮助非常感谢。

+0

会是怎样对应的输出为配合您所提供。此外,纬度有3个数字字段,经度2 ..这是典型的如何指定这些(并在您的数据常数)?你试过什么了? – Levon

+0

所以我想产生浮动:'0°25'30“S,91°7'W' - >'0.425','91.116667'。看起来数据可能会或可能不会有分钟关联。没有那么我可以假设为0。 –

+0

经度 180 W = -180 180 E = 180 纬度 90 N = 90 90 S = -90 实施例必须给-0.425,-91.116667。通过搜索0°检查此例如,Google地图中的25'30“S,91°7'W。 –

回答

2

这将您的输入字符串转换为您的预期输出。它可以处理分钟和秒不存在。

目前,它不占南北,东/西。如果你告诉我你是如何处理这些问题的,我会更新答案。

# -*- 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) 
+0

谢谢。让我看看我能做些什么。 –

16

这种方法可以处理秒和分钟缺席,我认为正确处理罗盘方向:

# -*- 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 
+1

+1不会让我的头受伤..“简单胜于复杂。” – Levon

+1

没有正则表达式= +1。 – Droogans

+0

如果秒数是浮点格式? – ratata

1

一个简单的方法(假设我今天自学有关正则表达式,因为这个问题)。处理遗漏的领域和指南针方向。

# -*- coding: latin-1 -*- 
import re 
s = """0°25'30"S, 91°7'W""" 

def compLat_Long(degs, mins, secs, comp_dir): 
    return (degs + (mins/60) + (secs/3600)) * comp_dir 

def extract_DegMinSec(data): 
    m = re.search(r'(\d+°)*(\d+\')*(\d+")*', data.strip()) 
    deg, mins, secs = [0.0 if m.group(i) is None else float(m.group(i)[:-1]) for i in range(1, 4)] 
    comp_dir = 1 if data[-1] in ('W', 'S') else -1 
    return deg, mins, secs, comp_dir 

s1, s2 = s.split(',') 
dms1 = extract_DegMinSec(s1) 
dms2 = extract_DegMinSec(s2) 
print('{:7.4f} {:7.4f}'.format(compLat_Long(*dms1), compLat_Long(*dms2))) 

产生

0.4250 91.1167