Convert DD (decimal degrees) to DMS (degrees minutes seconds) in Python?

后端 未结 9 1922
花落未央
花落未央 2020-12-05 19:53

How do you convert Decimal Degrees to Degrees Minutes Seconds In Python? Is there a Formula already written?

相关标签:
9条回答
  • 2020-12-05 20:01

    If you want to handle negatives properly, the first non-zero measure is set negative. It is counter to common practice to specify all of degrees, minutes and seconds as negative (Wikipedia shows 40° 26.7717, -79° 56.93172 as a valid example of degrees-minutes notation, in which degrees are negative and minutes have no sign), and setting degrees as negative does not have any effect if the degrees portion is 0. Here is a function that adequately handles this, based on Paul McGuire's and baens' functions:

    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)
    
    0 讨论(0)
  • 2020-12-05 20:01

    Now we can use LatLon library...

    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')`
    
    0 讨论(0)
  • 2020-12-05 20:03

    This is exactly what divmod was invented for:

    >>> 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)
    
    0 讨论(0)
  • 2020-12-05 20:08

    The sign has better be returned separately, so that it can be used to choose from ('N', 'S') or ('E', 'W'), for example.

    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
    
    0 讨论(0)
  • 2020-12-05 20:09

    Improving @chqrlie answer:

        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)
    
    0 讨论(0)
  • 2020-12-05 20:10

    Use fmod and rounding to get the degrees and fraction separated. Multiply the fraction by 60 and repeat to get minutes and a remainder. Then multiply that last part by 60 again to get the number of seconds.

    0 讨论(0)
提交回复
热议问题