18

如何在 Python 中将十进制度数转换为度分秒?是否已经编写了公式?

4

11 回答 11

21

这正是divmod发明的目的:

>>> def decdeg2dms(dd):
...   mnt,sec = divmod(dd*3600,60)
...   deg,mnt = divmod(mnt,60)
...   return deg,mnt,sec

>>> dd = 45 + 30.0/60 + 1.0/3600
>>> print dd
45.5002777778
>>> decdeg2dms(dd)
(45.0, 30.0, 1.0)
于 2010-04-05T18:32:02.777 回答
11

这是我基于 Paul McGuire 的更新版本。这个应该正确处理底片。

def decdeg2dms(dd):
   is_positive = dd >= 0
   dd = abs(dd)
   minutes,seconds = divmod(dd*3600,60)
   degrees,minutes = divmod(minutes,60)
   degrees = degrees if is_positive else -degrees
   return (degrees,minutes,seconds)
于 2012-04-23T19:07:47.197 回答
10

如果要正确处理负数,则将第一个非零度量设置为负数。将所有度数、分钟数和秒数指定为负数是违反惯例的(维基百科显示 40° 26.7717、-79° 56.93172 作为度数-分钟符号的有效示例,其中度数为负数,分钟数没有符号),如果度数部分为 0,则将度数设置为负数没有任何效果。这是一个充分处理此问题的函数,基于 Paul McGuire 和 baens 的函数:

def decdeg2dms(dd):
    negative = dd < 0
    dd = abs(dd)
    minutes,seconds = divmod(dd*3600,60)
    degrees,minutes = divmod(minutes,60)
    if negative:
        if degrees > 0:
            degrees = -degrees
        elif minutes > 0:
            minutes = -minutes
        else:
            seconds = -seconds
    return (degrees,minutes,seconds)
于 2012-10-05T00:27:06.013 回答
6

Just a couple of * 60 multiplications and a couple of int truncations, i.e.:

>>> decdegrees = 31.125
>>> degrees = int(decdegrees)
>>> temp = 60 * (decdegrees - degrees)
>>> minutes = int(temp)
>>> seconds = 60 * (temp - minutes)
>>> print degrees, minutes, seconds
31 7 30.0
>>> 
于 2010-04-05T17:56:02.933 回答
4

这是我的 Python 代码:

def DecimaltoDMS(Decimal):
    d = int(Decimal)
    m = int((Decimal - d) * 60)
    s = (Decimal - d - m/60) * 3600.00
    z= round(s, 2)
    if d >= 0:
        print ("N ", abs(d), "º ", abs(m), "' ", abs(z), '" ')
    else:
        print ("S ", abs(d), "º ", abs(m), "' ", abs(z), '" ')
于 2015-04-11T09:31:46.550 回答
3

改进@chqrlie 答案:

    def deg_to_dms(deg, type='lat'):
        decimals, number = math.modf(deg)
        d = int(number)
        m = int(decimals * 60)
        s = (deg - d - m / 60) * 3600.00
        compass = {
            'lat': ('N','S'),
            'lon': ('E','W')
        }
        compass_str = compass[type][0 if d >= 0 else 1]
        return '{}º{}\'{:.2f}"{}'.format(abs(d), abs(m), abs(s), compass_str)
于 2018-09-17T16:16:52.437 回答
2

该标志最好单独返回,以便它可以用于选择('N', 'S')('E', 'W'),例如。

import math

def dd_to_dms(degs):
    neg = degs < 0
    degs = (-1) ** neg * degs
    degs, d_int = math.modf(degs)
    mins, m_int = math.modf(60 * degs)
    secs        =           60 * mins
    return neg, d_int, m_int, secs
于 2015-06-01T22:49:30.510 回答
2

这是我稍微不同的方法,它与我的 HP Prime 上的正负十进制度相同......

def dms(deg):
    f,d = math.modf(deg)
    s,m = math.modf(abs(f) * 60)
    return (d,m,s * 60)
于 2020-12-17T16:47:38.353 回答
0

使用fmod和舍入来分离度数和分数。将分数乘以 60 并重复以获得分钟和余数。然后将最后一部分再次乘以 60 以获得秒数。

于 2010-04-05T16:51:27.720 回答
0

现在我们可以使用 LatLon 库...

https://pypi.org/project/LatLon/

>> palmyra = LatLon(Latitude(5.8833), Longitude(-162.0833)) # Location of Palmyra Atoll in decimal degrees
>> palmyra = LatLon(5.8833, -162.0833) # Same thing but simpler! 
>> palmyra = LatLon(Latitude(degree = 5, minute = 52, second = 59.88),
                     Longitude(degree = -162, minute = -4.998) # or more complicated!
>> print palmyra.to_string('d% %m% %S% %H') # Print coordinates to degree minute second
('5 52 59.88 N', '162 4 59.88 W')`
于 2020-03-12T10:50:20.220 回答
0

如果您的数据在 DataFrame 中,您可以使用clean_lat_long()DataPrep中的函数。安装 DataPrep 与pip install dataprep.

from dataprep.clean import clean_lat_long
df = pd.DataFrame({"coord": [(45.5003, -122.4420), (5.8833, -162.0833)]})

df2 = clean_lat_long(df, "coord", output_format="dms")
# print(df2)
                 coord                        coord_clean
0  (45.5003, -122.442)  45° 30′ 1.08″ N, 122° 26′ 31.2″ W
1  (5.8833, -162.0833)  5° 52′ 59.88″ N, 162° 4′ 59.88″ W

或者,如果纬度和经度在不同的列中:

df = pd.DataFrame({"latitude": [45.5003, 5.8833], "longitude": [-122.4420, -162.0833]})

df2 = clean_lat_long(df, lat_col="latitude", long_col="longitude", output_format="dms")
# print(df2)
   latitude  longitude                 latitude_longitude
0   45.5003  -122.4420  45° 30′ 1.08″ N, 122° 26′ 31.2″ W
1    5.8833  -162.0833  5° 52′ 59.88″ N, 162° 4′ 59.88″ W
于 2021-02-23T05:58:43.193 回答