A robust Python library for parsing geographic coordinates in various formats. This library can handle decimal degrees, degrees/minutes/seconds, and maritime coordinate formats with high precision.
- Multiple format support: Decimal degrees, degrees/minutes/seconds, maritime formats
- Flexible input: Accepts strings, floats, integers, and Decimal types
- Validation: Optional coordinate validation with customizable ranges
- High precision: Uses Python's Decimal type for accurate calculations
- Maritime formats: Special support for nautical coordinate formats
- Internationalization: Supports Cyrillic cardinal directions
- Error handling: Comprehensive error messages for invalid inputs
pip install coordinate-parserfrom coordinate_parser import parse_coordinate
# Basic usage
lat = parse_coordinate("40°41.65'N")
lon = parse_coordinate("74°02.54'W")
print(f"Latitude: {lat}, Longitude: {lon}")
# Decimal degrees
coord = parse_coordinate("23.43")
print(f"Coordinate: {coord}")
# With validation
lat = parse_coordinate("45.5", coord_type="latitude")
lon = parse_coordinate("120.5", coord_type="longitude")parse_coordinate("23.43") # 23.43
parse_coordinate("-45.21") # -45.21
parse_coordinate("23.43 N") # 23.43
parse_coordinate("45.21 W") # -45.21parse_coordinate("23° 25.800'") # 23.43
parse_coordinate("23 25.8 N") # 23.43
parse_coordinate("45°12.6'S") # -45.21parse_coordinate("23° 25' 48.0\" N") # 23.43
parse_coordinate("45° 12' 36.0\" S") # -45.21
parse_coordinate("23d 25m 48s") # 23.43# Pattern 1: degree-dash-minutes with degree symbol
parse_coordinate("40°–41.65'N") # 40.694166666667
parse_coordinate("139°-02.54'E") # 139.042333333333
# Pattern 2: degree-dash-minutes without degree symbol
parse_coordinate("54-05.48N") # 54.091333333333
parse_coordinate("162-29.03W") # -162.483833333333
# Pattern 3: degree-minutes with degree symbol
parse_coordinate("30°34.4'N") # 30.573333333333
parse_coordinate("120°45.5'E") # 120.758333333333
# Pattern 4: degree-minutes-seconds
parse_coordinate("30°34'24.0\"N") # 30.573333333333# Cyrillic cardinal directions
parse_coordinate("23.43 С") # North (С = N)
parse_coordinate("45.21 З") # West (З = W)
parse_coordinate("23.43 В") # East (В = E)
parse_coordinate("45.21 Ю") # South (Ю = S)
# Comma as decimal separator
parse_coordinate("23,43") # 23.43
parse_coordinate("23° 25,8'") # 23.43Parse a coordinate string and return a Decimal value in decimal degrees.
Parameters:
string(str | float | Decimal | None): The coordinate to parsecoord_type(str): Type of coordinate for validation ("latitude", "longitude", or "coordinate")validate(bool): Whether to validate the coordinate is within valid ranges
Returns:
Decimal | None: The parsed coordinate in decimal degrees, or None if input is None/empty
Raises:
ValueError: If the coordinate cannot be parsed or is outside valid range
Convert degrees, minutes, seconds to decimal degrees.
Parameters:
*args: Variable arguments representing degrees, minutes (optional), seconds (optional)
Returns:
float: Decimal degrees
The library supports optional validation for coordinates:
# Latitude validation (-90 to 90)
parse_coordinate("45.5", coord_type="latitude") # Valid
parse_coordinate("95.0", coord_type="latitude") # Raises ValueError
# Longitude validation (-180 to 180)
parse_coordinate("120.5", coord_type="longitude") # Valid
parse_coordinate("185.0", coord_type="longitude") # Raises ValueError
# Disable validation
parse_coordinate("95.0", coord_type="latitude", validate=False) # Returns 95.0The library provides comprehensive error handling:
try:
coord = parse_coordinate("invalid_coordinate")
except ValueError as e:
print(f"Parse error: {e}")
# Common error cases:
# - Invalid format: "'invalid' is not a valid coordinate string"
# - Out of range: "Latitude 95.0 is outside valid range [-90, 90]"
# - Invalid minutes: "Minutes 65 must be less than 60"
# - Invalid seconds: "Seconds 75 must be less than 60"git clone https://github.com/shipster-ai/coordinate-parser.git
cd coordinate-parser
pip install -e ".[dev]"pytestpytest --cov=coordinate_parser --cov-report=html# Format code
black src/ tests/
# Lint code
ruff check src/ tests/
# Type checking
mypy src/Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Originally developed as part of the Shipster project for parsing maritime coordinates.