Skip to content

Instantly share code, notes, and snippets.

@mmmatthew
Created June 23, 2018 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmmatthew/dc16e01c1bba401b184b95c61428ac95 to your computer and use it in GitHub Desktop.
Save mmmatthew/dc16e01c1bba401b184b95c61428ac95 to your computer and use it in GitHub Desktop.
Python function for converting decimal degrees to DMS
# Helper function for converting decimal degrees to DMS (source: http://rextester.com/BRMA94677)
def dd2dms(decimaldegree, direction='x'):
if type(decimaldegree) != 'float':
try:
decimaldegree = float(decimaldegree)
except:
print ('\nERROR: Could not convert %s to float.'%(type(decimaldegree)))
return 0
if decimaldegree < 0:
decimaldegree = -decimaldegree
if direction == 'x':
appendix = 'W'
else:
appendix = 'S'
else:
if direction == 'x':
appendix = 'E'
else:
appendix = 'N'
minutes = decimaldegree%1.0*60
seconds = minutes%1.0*60
return '{0}°{1}\'{2:2.3f}"{3}'.format(int(floor(decimaldegree)), int(floor(minutes)), seconds, appendix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment