Convert Decimal-Degrees (DD) to Degrees-Minutes-Seconds (DMS) with Python

Table of Contents

Introduction

In this blog post we will present a function to convert from decimal degrees (dd) to degree-minutes-seconds (dms) using Python Programming. This conversion can come in handy when you need dms for report writing for example.

This course is designed to instill the basics of Python Programming by incrementally increasing your knowledge session-upon-session. In each section you will be given new material for a workbook to fill out and by the end of this course you will have your very own Python reference handbook. So how does this course have a GIS focus? Simple, most elements of the course have GIS and geospatial data in mind. Instead of using non-descript variables and values, we will use terms such as population, city, x_coord, y_coord, and so on. This will aid participants with pinpointing how they can relate geospatial data to Python. 

Using Python to Convert from Decimal Degrees to Degrees-Minutes-Seconds

The code below uses the math module from the Standard Python Library to convert from dd to dms.

				
					import math

def dd2dms(longitude, latitude):

    ## math.modf() splits whole number and decimal into tuple
    ## eg 53.3478 becomes (0.3478, 53)
    split_degx = math.modf(longitude)

    ## the whole number [index 1] is the degrees
    degrees_x = int(split_degx[1])

    ## multiply the decimal part by 60: 0.3478 * 60 = 20.868
    ## split the whole number part of the total as the minutes: 20
    ## abs() absoulte value - no negative
    minutes_x = abs(int(math.modf(split_degx[0] * 60)[1]))

    ## multiply the decimal part of the split above by 60 to get the seconds
    ## 0.868 x 60 = 52.08, round excess decimal places to 2 places
    ## abs() absoulte value - no negative
    seconds_x = abs(round(math.modf(split_degx[0] * 60)[0] * 60,2))

    ## repeat for latitude
    split_degy = math.modf(latitude)
    degrees_y = int(split_degy[1])
    minutes_y = abs(int(math.modf(split_degy[0] * 60)[1]))
    seconds_y = abs(round(math.modf(split_degy[0] * 60)[0] * 60,2))

    ## account for E/W & N/S
    if degrees_x < 0:
        EorW = "W"
    else:
        EorW = "E"

    if degrees_y < 0:
        NorS = "S"
    else:
        NorS = "N"

    ## abs() remove negative from degrees, was only needed for if-else above
    print("\t" + str(abs(degrees_x)) + u"\u00b0 " + str(minutes_x) + "' " + str(seconds_x) + "\" " + EorW)
    print("\t" + str(abs(degrees_y)) + u"\u00b0 " + str(minutes_y) + "' " + str(seconds_y) + "\" " + NorS)
				
			

The Conversion in Action

				
					## some coords of cities
coords = [["Dublin", -6.2597, 53.3478],["Paris", 2.3508, 48.8567],["Sydney", 151.2094, -33.8650]]

## run dd2dms()
for city,x,y in coords:
    print(f"{city}:")
    dd2dms(x, y)
				
			
Convert Decimal Degrees to Degrees Minutes Seconds with Python

At Final Draft Mapping we provide comprehensive courses for automating tasks within ArcGIS Pro and ArcGIS Online with ArcPy and the ArcGIS API for Python. Courses range from beginner to advanced workflows and all paid courses provide extra support where you can ask questions. Automation within ArcGIS is a highly sought after skill, by adding these skills to your arsenal you are placing yourself at the forefront of that demand. 

We appreciate our blog readers, you can get 25% off any (non-sale) course at any time with the code FDMBLOG25

Leave a Comment

Your email address will not be published. Required fields are marked *