嗨,我有一个包含一些位置数据的遗留数据库。这些字段只是文本字段,字符串看起来像这样 0°25'30"S, 91°7'W
。有没有一种方法可以将它们转换为两个浮点数,分别表示 十进制纬度
和 十进制经度
?
编辑:
所以一个例子是:0°25'30"S, 91°7'W
-> 0.425
,91.116667
,原始的单字段位置产生了两个浮点数。
非常感谢任何帮助。
嗨,我有一个包含一些位置数据的遗留数据库。这些字段只是文本字段,字符串看起来像这样 0°25'30"S, 91°7'W
。有没有一种方法可以将它们转换为两个浮点数,分别表示 十进制纬度
和 十进制经度
?
编辑:
所以一个例子是:0°25'30"S, 91°7'W
-> 0.425
,91.116667
,原始的单字段位置产生了两个浮点数。
非常感谢任何帮助。
这种方法可以处理缺少秒或分钟,并且我认为可以正确地处理方向:
# -*- 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
direction = {'N':1, 'S':-1, 'E': 1, 'W':-1}
。 - rom27°29'04.2"N 89°19'44.6"E
。 - tree em这会将您的输入字符串转换为期望的输出结果。它可以处理分钟和秒钟不存在的情况。
目前,它不考虑北/南,东/西。如果您告诉我如何处理这些,我会更新答案。
# -*- 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)
# -*- 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
clean_lat_long()
。使用pip install dataprep
安装DataPrep。from dataprep.clean import clean_lat_long
df = pd.DataFrame({"coord": ["""0°25'30"S, 91°7'W""", """27°29'04.2"N 89°19'44.6"E"""]})
df2 = clean_lat_long(df, "coord", split=True)
# print(df2)
coord latitude longitude
0 0°25'30"S, 91°7'W -0.4250 -91.1167
1 27°29'04.2"N\t89°19'44.6"E 27.4845 89.3291
尝试这个函数,它处理单个坐标(纬度或经度)。它能够返回有效结果,对于在坐标开头或结尾处放置了罗盘方向和使用","作为小数分隔符的坐标,并且如果无法解码输入,则返回原始字符串。
def dec(coord):
c = coord.upper()
s = 1
if c.find('S')>0 or c.find('W')>0:
s = -1
c = c.replace('N','').replace('E','').replace('S','').replace('W','').replace(',','.').replace(u'°',' ').replace('\'',' ').replace('"',' ')
a = c.split()
a.extend([0,0,0])
try:
return s*(float(a[0])+float(a[1])/60.0+float(a[2])/3600.0)
except:
return coord
0°25'30"S, 91°7'W
->0.425
,91.116667
。数据似乎可能有也可能没有与之相关的分钟数。如果没有,那么我可以假设为0。 - Darwin Tech180 西经 = -180
180 东经 = 180
纬度
90 北纬 = 90
90 南纬 = -90 例如,应该返回-0.425,-91.116667。可以通过在Google地图中搜索0°25'30"S,91°7'W来验证。 - Renat Zaripov