How to convert latitude and longitude in DMS format into decimal format (and vice versa)? [closed]

↘锁芯ラ 提交于 2021-02-04 18:47:11

问题


Does python contain any library to convert latitude and longitude in DMS format into decimal format and vice versa?


回答1:


# -*- 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

from : Python - Batch convert GPS positions to Lat Lon decimals

and for the other way :

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]

from : Lat Long to Minutes and Seconds?



来源:https://stackoverflow.com/questions/17193351/how-to-convert-latitude-and-longitude-in-dms-format-into-decimal-format-and-vic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!