Python 如何将度分秒转换为小数点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33997361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert degree minute second to degree decimal
提问by bikuser
I receive the latitude and longitude from GPS with this format:
我使用以下格式从 GPS 接收纬度和经度:
Latitude : 78°55'44.29458"N
纬度:78°55'44.29458"N
I need convert this data to:
我需要将此数据转换为:
latitude: 78.9288888889
纬度:78.9288888889
I found this code here: link
我在这里找到了这个代码:链接
import re
def dms2dd(degrees, minutes, seconds, direction):
dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
if direction == 'E' or direction == 'N':
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)
It is working for for this format
它适用于这种格式
dd = parse_dms("78°55'44.33324'N" )
but it is not working for my datafromat. Can anyone help me to solve this problem?
但它不适用于我的 datafromat。谁能帮我解决这个问题?
采纳答案by Falko
The problem is that the seconds 44.29458 are split at .
.
问题是秒 44.29458 在.
.
You could either define the split characters directly (instead of where notto split):
您可以直接定义拆分字符(而不是不拆分的位置):
>>> re.split('[°\'"]+', """78°55'44.29458"N""")
['78', '55', '44.29458', 'N']
or leave the regular expression as it is and merge parts 2 and 3:
或者保留正则表达式并合并第 2 部分和第 3 部分:
dms2dd(parts[0], parts[1], parts[2] + "." + parts[3], parts[4])
Update:
更新:
Your method call dd = parse_dms("78°55'44.33324"N )
is a syntax error. Add the closing "
and escape the other one. Or use tripple quotes for the string definition:
您的方法调用dd = parse_dms("78°55'44.33324"N )
是语法错误。添加关闭"
并转义另一个。或者对字符串定义使用三重引号:
parse_dms("""78°55'44.29458"N""")
回答by Lili
I have slightly modified the re:
我稍微修改了re:
parts = re.split('[^\d\w\.]+', dms)
And as @Falko advised to make it work, you can either use double double quotes or escape your quotes characters
正如@Falko 建议让它工作的那样,您可以使用双双引号或转义引号字符
parse_dms("53°19\'51.8\"N")
回答by Rodrigo Santiago
The function above (dms2dd) is incorrect.
上面的函数 (dms2dd) 不正确。
Actual (With error):
实际(有错误):
if direction == 'E' or direction == 'N': dd *= -1
如果方向 == 'E' 或方向 == 'N':dd *= -1
Corrected Condition:
修正条件:
if direction == 'W' or direction == 'S': dd *= -1
如果方向 == ' W' 或方向 == ' S': dd *= -1
回答by Inti
Here's my one liner (fine, fine – maybe it's two lines) :)
这是我的一个班轮(很好,很好——也许是两行):)
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)
This outputs 51.60255
这输出 51.60255
回答by bfree67
For multiple coordinates, you can read them in using pandas. Formatting is important - there should not be any white spaces. White spaces can be removed using the replace function. The outputs can be easily saved as a text file or spreadsheet. I just printed them for validation and rounded the decimals locations out to 4.
对于多个坐标,您可以使用 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)
回答by Isaac Asante
I know this is an old question, but for whoever is following along, just thought I'd point out that you seem to have incorrect logic in your dms2dd()
function regarding the sign of your decimals. You have:
我知道这是一个老问题,但是对于跟随的人,我只是想指出您的dms2dd()
函数中关于小数符号的逻辑似乎不正确。你有:
if direction == 'E' or direction == 'N':
dd *= -1
But it should only be negative if the direction is West (W) of the Prime Meridian or South (S) of the equator. So it should rather be:
但只有当方向是本初子午线的西 (W) 或赤道的南 (S) 时,它才应该是负的。所以它应该是:
if direction == 'W' or direction == 'S':
dd *= -1
Here's a quote from a thorough guide: https://www.ubergizmo.com/how-to/read-gps-coordinates/
这是一份详尽指南的引述: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.
纬度线的坐标代表赤道以北,因为它是正的。如果数字为负,则代表赤道以南。
[...] 经度线的坐标代表本初子午线以东,因为它是正的。如果数字为负,则代表本初子午线以西。