如何将DMS格式的经纬度转换为十进制格式(反之亦然)?

5

Python是否包含将DMS格式的纬度和经度转换为十进制格式以及反之的库?


我怀疑标准库中是否有这样的东西... - mgilson
他没有问关于标准库的事。这就是他所询问的库:https://pypi.python.org/pypi/LatLon/1.0.2 - Antony Hatchkins
这是一个有效的问题,也有一个有效的答案。 - Mr_and_Mrs_D
1个回答

11
# -*- coding: latin-1 -*-

#example for : 0°25'30"S, 91°7'W

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

来自:Python - 批量将GPS位置转换为纬度和经度十进制数

另一种方法:

def deg_to_dms(deg):
    d = int(deg)
    md = abs(deg - d) * 60
    m = int(md)
    sd = (md - m) * 60
    return [d, m, sd]

#output
>>> deg_to_dms(91.1166666667)
[91, 7, 1.199953203467885e-07]
>>> deg_to_dms(0.425)
[0, 25, 30.0]

来自: 如何将经纬度转换成分钟和秒?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接