A Python library for parsing geographic coordinates in various formats
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file coordinate_parser-0.1.1.tar.gz.
File metadata
- Download URL: coordinate_parser-0.1.1.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f94b90dc656fa571c046ac3364cda450474df836525cefc40b1fa421e14e24e6
|
|
| MD5 |
100f966189c95a56897a8184b2a7ed24
|
|
| BLAKE2b-256 |
da6ca0fa0d0a8714aaa8a63e3bb0ae6a4e1f7ca8b5f27a619e353fba37d38285
|
Provenance
The following attestation bundles were made for coordinate_parser-0.1.1.tar.gz:
Publisher:
release.yml on 17swifts/coordinate-parser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coordinate_parser-0.1.1.tar.gz -
Subject digest:
f94b90dc656fa571c046ac3364cda450474df836525cefc40b1fa421e14e24e6 - Sigstore transparency entry: 529086626
- Sigstore integration time:
-
Permalink:
17swifts/coordinate-parser@5beb98f325dd19edd4decdfc556216b8d36cc8a0 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/17swifts
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5beb98f325dd19edd4decdfc556216b8d36cc8a0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file coordinate_parser-0.1.1-py3-none-any.whl.
File metadata
- Download URL: coordinate_parser-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
864c2304a059fc32af7e47733412aa0a7e2fba24aeec7cde3ad58d6efca60fbe
|
|
| MD5 |
78087e4a2b7c02a485434cb35dec0ef4
|
|
| BLAKE2b-256 |
c9579ed1c25333314059b4ac201199f2364253cb362a999fd3613a295f0979f2
|
Provenance
The following attestation bundles were made for coordinate_parser-0.1.1-py3-none-any.whl:
Publisher:
release.yml on 17swifts/coordinate-parser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coordinate_parser-0.1.1-py3-none-any.whl -
Subject digest:
864c2304a059fc32af7e47733412aa0a7e2fba24aeec7cde3ad58d6efca60fbe - Sigstore transparency entry: 529086628
- Sigstore integration time:
-
Permalink:
17swifts/coordinate-parser@5beb98f325dd19edd4decdfc556216b8d36cc8a0 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/17swifts
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5beb98f325dd19edd4decdfc556216b8d36cc8a0 -
Trigger Event:
release
-
Statement type: