A geographic coordinates parser for Python that returns decimal degrees for a wide range of input formats.
Translated directly from the Javascript version, see that page for more info as well as examples of formats that can be converted.
Please note that decimal precision needs to be reasonable. The maximum decimal places allowed in DMS, for example, is three (3). DMS coordinates with four decimal places in seconds are measuring position at a precision of less than 10mm, which is well beyond what even the best professional GPS devices can provide.
convert expects a single string with both latitude and longitude and checks that the formats of these are consistent with each other. The function may throw an error for various reasons so always wrap in a try... except and handle accordingly.
from converter import convert
try:
converted = convert("N 48° 30,6410', E 18° 57,4583'")
except Exception as ex:
# do something with ex
print(converted.decimalCoordinates)
convert returns an object of class ConvertedCoordinates which provides the results in both atomic and combined formats. The result object also has a method to() which can be used to convert the coordinates to other formats. Options are DD, DM, and DMS.
try:
converted = convert("N 48° 30,6410', E 18° 57,4583'")
except Exception as ex:
# do something with ex
print(converted.to('DMS')) # gives 48° 30' 38.4" N, 18° 57' 27.5" E
...or even:
dms = convert("N 48° 30,6410', E 18° 57,4583'").to('DMS')
Note that input coordinates can be decimal degrees already (which helps when converting large numbers of input coordinates with varing formats), and converted.to('DD') will provide a nicely formatted decimal degree string.
If you encounter a coordinate format that is not converted successfully please open a Github issue.