如何将度分秒转换为度小数
How to convert degree minute second to degree decimal
我从 GPS 接收的纬度和经度格式如下:
纬度 : 78°55'44.29458"N
我需要将此数据转换为:
纬度:78.9288888889
我在这里找到了这段代码:link
import re
def dms2dd(degrees, minutes, seconds, direction):
dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
if direction == 'E' or direction == 'S':
dd *= -1
return dd;
def dd2dms(deg):
d = int(deg)
md = abs(deg - d) * 60
m = int(md)
sd = (md - m) * 60
return [d, m, sd]
def parse_dms(dms):
parts = re.split('[^\d\w]+', dms)
lat = dms2dd(parts[0], parts[1], parts[2], parts[3])
return (lat)
dd = parse_dms("78°55'44.33324"N )
print(dd)
它正在为这种格式工作
dd = parse_dms("78°55'44.33324'N" )
但它不适用于我的数据格式。谁能帮我解决这个问题?
问题是 44.29458 秒在 .
处拆分。
您可以直接定义拆分字符(而不是 where not 拆分):
>>> re.split('[°\'"]+', """78°55'44.29458"N""")
['78', '55', '44.29458', 'N']
或保留正则表达式并合并第 2 部分和第 3 部分:
dms2dd(parts[0], parts[1], parts[2] + "." + parts[3], parts[4])
更新:
您的方法调用 dd = parse_dms("78°55'44.33324"N )
是一个语法错误。添加结尾 "
并转义另一个。或者对字符串定义使用三重引号:
parse_dms("""78°55'44.29458"N""")
我稍微修改了回复:
parts = re.split('[^\d\w\.]+', dms)
正如@Falko 建议的那样,您可以使用双引号或转义引号字符
parse_dms("53°19\'51.8\"N")
上面的函数 (dms2dd) 不正确。
实际(有错误):
如果方向 == 'E' 或方向 == 'N':
dd *= -1
更正条件:
如果方向 == 'W' 或方向 == 'S':
dd *= -1
这是我的一条线(很好,很好——也许是两行):)
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)
这输出 51.60255
对于多个坐标,您可以使用pandas读取它们。格式很重要 - 不应有任何空格。可以使用替换功能删除空格。输出可以很容易地保存为文本文件或电子表格。我只是打印它们进行验证并将小数点四舍五入到 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)
我知道这是一个老问题,但对于后续问题,我只是想指出您的 dms2dd()
函数中关于小数符号的逻辑似乎不正确。你有:
if direction == 'E' or direction == 'N':
dd *= -1
但只有方向为本初子午线西 (W) 或赤道南 (S) 时才应为负。所以它应该是:
if direction == 'W' or direction == 'S':
dd *= -1
这里引用了一篇详尽的指南: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.
如果您的数据在 DataFrame 中,您可以使用函数 clean_lat_long()
from the library DataPrep。使用 pip install dataprep
.
安装 DataPrep
from dataprep.clean import clean_lat_long
df = pd.DataFrame({"Latitude": ["78°55'44.29458''N", "51°36'9.18''N"]})
df2 = clean_lat_long(df, lat_col="Latitude")
Latitude Latitude_clean
0 78°55'44.29458''N 78.9290
1 51°36'9.18''N 51.6026
你可以使用这个模块https://pypi.org/project/dms2dec/
将 DMS 转换为十进制。
用法
from dms2dec.dms_convert import dms2dec
dms2dec('''36°44'47.69"N''') # converts to dec
dms2dec('''3° 2'33.53"E''') # converts to dec
我从 GPS 接收的纬度和经度格式如下:
纬度 : 78°55'44.29458"N
我需要将此数据转换为:
纬度:78.9288888889
我在这里找到了这段代码:link
import re
def dms2dd(degrees, minutes, seconds, direction):
dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
if direction == 'E' or direction == 'S':
dd *= -1
return dd;
def dd2dms(deg):
d = int(deg)
md = abs(deg - d) * 60
m = int(md)
sd = (md - m) * 60
return [d, m, sd]
def parse_dms(dms):
parts = re.split('[^\d\w]+', dms)
lat = dms2dd(parts[0], parts[1], parts[2], parts[3])
return (lat)
dd = parse_dms("78°55'44.33324"N )
print(dd)
它正在为这种格式工作
dd = parse_dms("78°55'44.33324'N" )
但它不适用于我的数据格式。谁能帮我解决这个问题?
问题是 44.29458 秒在 .
处拆分。
您可以直接定义拆分字符(而不是 where not 拆分):
>>> re.split('[°\'"]+', """78°55'44.29458"N""")
['78', '55', '44.29458', 'N']
或保留正则表达式并合并第 2 部分和第 3 部分:
dms2dd(parts[0], parts[1], parts[2] + "." + parts[3], parts[4])
更新:
您的方法调用 dd = parse_dms("78°55'44.33324"N )
是一个语法错误。添加结尾 "
并转义另一个。或者对字符串定义使用三重引号:
parse_dms("""78°55'44.29458"N""")
我稍微修改了回复:
parts = re.split('[^\d\w\.]+', dms)
正如@Falko 建议的那样,您可以使用双引号或转义引号字符
parse_dms("53°19\'51.8\"N")
上面的函数 (dms2dd) 不正确。
实际(有错误):
如果方向 == 'E' 或方向 == 'N': dd *= -1
更正条件:
如果方向 == 'W' 或方向 == 'S': dd *= -1
这是我的一条线(很好,很好——也许是两行):)
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)
这输出 51.60255
对于多个坐标,您可以使用pandas读取它们。格式很重要 - 不应有任何空格。可以使用替换功能删除空格。输出可以很容易地保存为文本文件或电子表格。我只是打印它们进行验证并将小数点四舍五入到 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)
我知道这是一个老问题,但对于后续问题,我只是想指出您的 dms2dd()
函数中关于小数符号的逻辑似乎不正确。你有:
if direction == 'E' or direction == 'N':
dd *= -1
但只有方向为本初子午线西 (W) 或赤道南 (S) 时才应为负。所以它应该是:
if direction == 'W' or direction == 'S':
dd *= -1
这里引用了一篇详尽的指南: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.
如果您的数据在 DataFrame 中,您可以使用函数 clean_lat_long()
from the library DataPrep。使用 pip install dataprep
.
from dataprep.clean import clean_lat_long
df = pd.DataFrame({"Latitude": ["78°55'44.29458''N", "51°36'9.18''N"]})
df2 = clean_lat_long(df, lat_col="Latitude")
Latitude Latitude_clean
0 78°55'44.29458''N 78.9290
1 51°36'9.18''N 51.6026
你可以使用这个模块https://pypi.org/project/dms2dec/
将 DMS 转换为十进制。
用法
from dms2dec.dms_convert import dms2dec
dms2dec('''36°44'47.69"N''') # converts to dec
dms2dec('''3° 2'33.53"E''') # converts to dec