Map coordinates were traditionally represented as degrees, minutes, and seconds (DMS) for maritime navigation. However, in GIS (which is computer-based), latitude and longitude are represented as decimal numbers known as decimal degrees. The degrees, minutes, and seconds format is still used. Sometimes, you have to convert between that format and decimal degrees to perform calculations and output reports.
In this example, we'll create two functions that can convert either format into the other:
- First, we import the math module to do conversions and the re regular expression module to parse the coordinate string:
import math
import re
- We have our function to convert decimal degrees into a degrees, minutes, and seconds string:
def dd2dms(lat, lon):
"""Convert decimal degrees to degrees, minutes, seconds""...