I get data via an HTML document attached to an E-Mail (don't ask why...). I need to read GPS-Coordinates form this file and want to generate a route with OSM. I have no problem getting the GPS-Coordinates as a string but I'm really struggling to form them into something OSM can use.
The GPS-coordinates look like this: N53°09.20 E009°11.82, the split is not a problem, but I need to form those into the normal lat and longs like (53.119897, 7.944012).
Is there anyone that had the same problem or is there a library I can use?
The below code can be used to convert degrees, minutes and seconds in the format you’ve provided into decimal longitude and latitude:
import re
coords = "N53°09.20 E009°11.82"
regex = "N(\d+)°(\d+)\.(\d+) E(\d+)°(\d+)\.(\d+)"
match = re.split(regex, coords)
x = int(match[1]) + (int(match[2]) / 60) + (int(match[3]) / 3600)
y = int(match[4]) + (int(match[5]) / 60) + (int(match[6]) / 3600)
print("%f, %f" %(x, y))
Output:
53.155556, 9.206111
If the coordinates you have are only degrees and decimal minutes, then the code can be modified slightly, as shown below:
import re
coords = "N53°09.20 E009°11.82"
regex = "N(\d+)°(\d+)\.(\d+) E(\d+)°(\d+)\.(\d+)"
match = re.split(regex, coords)
x = int(match[1]) + ((int(match[2]) + (int(match[3]) / 100)) / 60)
y = int(match[4]) + ((int(match[5]) + (int(match[6]) / 100)) / 60)
print("%f, %f" %(x, y))
Output:
53.153333, 9.197000