Hello,
I need to convert GPS coordinates from DMS to DD.
Provided there's no even better way, what's the right regex to 1) find the orientation (N, S, W, E) and 2) keep in memory using brackets?
Thank you.
I need to convert GPS coordinates from DMS to DD.
Provided there's no even better way, what's the right regex to 1) find the orientation (N, S, W, E) and 2) keep in memory using brackets?
Thank you.
#From Degrees, minutes, and seconds (DMS) to Decimal degrees (DD)
import re
coords = "45° 46' 52\" N 108° 30' 14\" W"
coords = coords.replace(" ","") #remove spaces -> 45°46'52"N108°30'14"W
coords = re.match(r"(\d+)°(\d+)'(.+?)\"([NSWE])",coords)
coords = re.match(r"(\d+)°(\d+)'(.+?)\"[(NSWE)]",coords)
coords = re.match(r"(\d+)°(\d+)'(.+?)\"([NSWE].)",coords)
coords = re.match(r"(\d+)°(\d+)'(.+?)\"[(NSWE).]",coords)
if coords:
#OK print(f"{coords.group(0)},{coords.group(1)},{coords.group(2)},{coords.group(3)}")
#4: IndexError: no such group
print(f"{coords.group(1)},{coords.group(2)},{coords.group(3)},{coords.group(4)}")
