Created
June 23, 2018 08:45
-
-
Save mmmatthew/dc16e01c1bba401b184b95c61428ac95 to your computer and use it in GitHub Desktop.
Python function for converting decimal degrees to DMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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