如何将度分秒转换为度十进制 [英] How to convert degree minute second to degree decimal
问题描述
我从GPS接收以下格式的纬度和经度:
I receive the latitude and longitude from GPS with this format:
纬度:78°55'44.29458北
Latitude : 78°55'44.29458"N
我需要将此数据转换为:
I need convert this data to:
纬度:78.9288888889
latitude: 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)
它适用于这种格式
dd = parse_dms("78°55'44.33324'N" )
,但不适用于我的datafromat.谁能帮我解决这个问题?
but it is not working for my datafromat. Can anyone help me to solve this problem?
推荐答案
问题是秒44.29458在.
处被分开.
The problem is that the seconds 44.29458 are split at .
.
您可以直接定义分割字符(而不是分割 not 的位置):
You could either define the split characters directly (instead of where not to split):
>>> re.split('[°\'"]+', """78°55'44.29458"N""")
['78', '55', '44.29458', 'N']
或保留正则表达式,然后合并第2部分和第3部分:
or leave the regular expression as it is and merge parts 2 and 3:
dms2dd(parts[0], parts[1], parts[2] + "." + parts[3], parts[4])
更新:
您的方法调用dd = parse_dms("78°55'44.33324"N )
是语法错误.添加结尾的"
并跳过另一个.或使用三引号引起来的字符串定义:
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:
parse_dms("""78°55'44.29458"N""")
这篇关于如何将度分秒转换为度十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!