Skip to content

Instantly share code, notes, and snippets.

@csulfrian
Last active February 23, 2023 07:33
Show Gist options
  • Save csulfrian/95c6e0ae03a3ae38b5783f7df7ad2b6e to your computer and use it in GitHub Desktop.
Save csulfrian/95c6e0ae03a3ae38b5783f7df7ad2b6e to your computer and use it in GitHub Desktop.
Regex search patterns for finding GNSS Latitude & Longitude in a string. Includes DD, DDM, & DMS variants.
# For Decimal Degrees (DD)
dd_regex = r"""(-*\d{1,3}.\d+°*[NnSsWwEe]*)"""
# For Degrees, Decimal Minutes (DDM)
ddm_regex = r"""(\d{1,3}°*\s*\d{1,2}.\d+[`\']\s*[NnSsWwEe]*)"""
# For Degrees, Minutes, Seconds (DMS)
dms_regex = r"""(-*\d{1,3}°\s*\d{1,2}[`\']\s*\d{1,2}.*\d+[\"″][NnSsWwEe]*)"""
# test coordinates
dd = """40.182769°, -104.725968°"""
ddm = """40° 10.966'N, 104° 43.558'W"""
dms = """40°10'57.96"N, 104°43'33.48"W"""
# re.findall() returns the results in a list
dd_coords = re.findall(dd_regex, dd, re.X)
print(dd_coords)
ddm_coords = re.findall(ddm_regex, ddm, re.X)
print(ddm_coords)
dms_coords = re.findall(dms_regex, dms, re.X)
print(dms_coords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment