Coordinate Parser
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.
Features
- 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
Installation
pip install coordinate-parser
Quick Start
from 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")
Supported Formats
Decimal Degrees
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.21
Degrees and Minutes
parse_coordinate("23° 25.800'") # 23.43
parse_coordinate("23 25.8 N") # 23.43
parse_coordinate("45°12.6'S") # -45.21
Degrees, Minutes, and Seconds
parse_coordinate("23° 25' 48.0\" N") # 23.43
parse_coordinate("45° 12' 36.0\" S") # -45.21
parse_coordinate("23d 25m 48s") # 23.43
Maritime Formats
# 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
International Support
# 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.43
API Reference
parse_coordinate(string, coord_type="coordinate", validate=True)
Parse 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
to_dec_deg(*args)
Convert degrees, minutes, seconds to decimal degrees.
Parameters:
*args: Variable arguments representing degrees, minutes (optional), seconds (optional)
Returns:
float: Decimal degrees
Validation
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.0
Error Handling
The 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"
Development
Setup Development Environment
git clone https://github.com/shipster-ai/coordinate-parser.git
cd coordinate-parser
pip install -e ".[dev]"
Running Tests
pytest
Running Tests with Coverage
pytest --cov=coordinate_parser --cov-report=html
Code Quality
# Format code
black src/ tests/
# Lint code
ruff check src/ tests/
# Type checking
mypy src/
Contributing
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
License
This project is licensed under the MIT License - see the LICENSE file for details.
Credits
Originally developed as part of the Shipster project for parsing maritime coordinates.
Metadata
Release files for coordinate-parser 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coordinate_parser-0.1.1.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coordinate_parser-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 kB
Release files / coordinate_parser-0.1.1.tar.gz
| Download URL | coordinate_parser-0.1.1.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f94b90dc656fa571c046ac3364cda450474df836525cefc40b1fa421e14e24e6
|
|
BLAKE2b-256 checksum How to use checksums |
da6ca0fa0d0a8714aaa8a63e3bb0ae6a4e1f7ca8b5f27a619e353fba37d38285
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 17, 2025.
Transparency logRelease files / coordinate_parser-0.1.1-py3-none-any.whl
| Download URL | coordinate_parser-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
864c2304a059fc32af7e47733412aa0a7e2fba24aeec7cde3ad58d6efca60fbe
|
|
BLAKE2b-256 checksum How to use checksums |
c9579ed1c25333314059b4ac201199f2364253cb362a999fd3613a295f0979f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 17, 2025.
Transparency log