Understanding coordinate format conversion
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 DMS 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 there
regular expression module to parse the coordinate string:import math import re
- We have our function to convert decimal degrees into a DMS string:
def dd2dms(lat, lon): """Convert decimal degrees to degrees, minutes, seconds""" latf, latn = math.modf(lat) lonf, lonn = math.modf(lon) latd = int(latn...