Latitude-Longitude String Parser
lat-lon-parser is a Python library designed for robustly parsing and converting latitude and longitude strings across various formats, including decimal degrees, degrees decimal minutes, and degrees minutes seconds. It provides functionality to parse coordinate strings into numerical values and to format numerical coordinates back into different string representations. The library's current version is 1.3.1, with the latest update on November 13, 2024, indicating active maintenance.
Common errors
-
ValueError: Could not parse latitude/longitude string
cause The input string does not match any of the expected latitude-longitude formats, often due to extraneous characters, incorrect separators, or unsupported notation.fixSimplify the input string to contain only the coordinate values and hemisphere indicators. Remove any descriptive text, labels, or unrecognised symbols. For example, change 'Lat: 45.123, Lon: -123.456' to '45.123, -123.456'. -
Output coordinates are in a distant or incorrect location.
cause Latitude and longitude values have been accidentally swapped or transposed during input. For example, if parsing a string where longitude is provided first, but the parser expects latitude first.fixDouble-check the order of the parsed coordinates. Latitudes range from -90 to +90, while longitudes range from -180 to +180. If a value greater than 90 appears in the latitude position, it's likely a transposition. Swap the order of the input values or adjust how they are passed to ensure latitude corresponds to latitude and longitude to longitude.
Warnings
- gotcha The parser uses a flexible, 'stupid' algorithm that generally works but might not cover all possible obscure formats. It's designed to accept ambiguous inputs where possible.
- gotcha Common parsing errors stem from extra text, incorrect/missing symbols (e.g., degree signs, hemisphere letters), values outside valid ranges (latitude -90 to +90, longitude -180 to +180), or transposed latitude and longitude (e.g., longitude given where latitude is expected).
Install
-
pip install lat-lon-parser
Imports
- parse
from lat_lon_parser import parse
- to_dec_deg
from lat_lon_parser import to_dec_deg
- to_str
from lat_lon_parser import to_str
Quickstart
from lat_lon_parser import parse, to_str
# Parse a latitude-longitude string
coord_str_dms = "40° 26' 46\" N 79° 58' 56\" W"
decimal_degrees = parse(coord_str_dms)
print(f"Parsed '{coord_str_dms}' to: {decimal_degrees}°")
# Parse a decimal degrees string
coord_str_dec = "23.43 N -45.21 W"
decimal_degrees_2 = parse(coord_str_dec)
print(f"Parsed '{coord_str_dec}' to: {decimal_degrees_2}°")
# Convert decimal degrees to a formatted string
lat_val = 34.1234
lon_val = -118.5678
formatted_str = to_str(lat_val, lon_val)
print(f"Converted {lat_val}, {lon_val} to: {formatted_str}")