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

后端 未结 9 1923
花落未央
花落未央 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:13

    Here is my updated version based upon Paul McGuire's. This one should handle negatives correctly.

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

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

    This is my Python code:

    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), '" ')
    
    0 讨论(0)
提交回复
热议问题