coord2loc
Convert latitude and longitude coordinates into localities like countries and states.
Overview
coord2loc is a Python library that allows you to convert geographic coordinates (latitude and longitude) into human-readable locations, such as countries and administrative regions (states, provinces, etc.). It uses polygon data from geoBoundaries.org to accurately map coordinates to their corresponding locations.
Installation
You can install all locations by selecting the all extra:
pip install coord2loc[all]
Alternatively, you can install specific regions with their respective extras:
pip install coord2loc[can] # Canada
pip install coord2loc[usa] # United States
Usage
Basic Usage
from coord2loc import Coordinate, locate
# Create a coordinate (New York City)
coordinate = Coordinate(latitude=40.7128, longitude=-74.0060)
# Locate the coordinate
location = locate(coordinate)
# Print the result
print(f"Country: {location.country}")
print(f"Administrative Region: {location.administrative_region}")
# Output:
# Country: USA
# Administrative Region: New York
Handling Unknown Locations
By default, the locate function returns None for coordinates that cannot be found (e.g., in oceans):
from coord2loc import Coordinate, locate
# Coordinate in the Atlantic Ocean
coordinate = Coordinate(latitude=30.0, longitude=-40.0)
# Locate the coordinate
location = locate(coordinate)
# Check if location was found
if location is None:
print("Location not found")
else:
print(f"Country: {location.country}")
print(f"Administrative Region: {location.administrative_region}")
# Output:
# Location not found
Configuring Behavior with Options
You can customize the behavior of the locate function using the Options class:
from coord2loc import Coordinate, Options, locate, CoordinateNotFound
# Create a coordinate in the ocean
coordinate = Coordinate(latitude=30.0, longitude=-40.0)
# Configure to raise an exception when location is not found
options = Options(raise_on_not_found=True)
try:
location = locate(coordinate, options)
print(f"Country: {location.country}")
print(f"Administrative Region: {location.administrative_region}")
except CoordinateNotFound as e:
print(f"Error: {e}")
# Output:
# Error: Coordinate (30.0, -40.0) could not be located
API Reference
Classes
Coordinate
A named tuple representing a geographic coordinate.
latitude(float): The latitude value (-90 to 90)longitude(float): The longitude value (-180 to 180)
Location
A named tuple representing a location.
country(str): The country nameadministrative_region(str): The administrative region (state, province, etc.)
Options
A named tuple for configuring the behavior of the locate function.
raise_on_invalid(bool, default=True): Whether to raise an exception for invalid coordinatesraise_on_not_found(bool, default=False): Whether to raise an exception when a location cannot be found
Functions
locate(coordinate, options=Options())
Locates a coordinate and returns the corresponding location.
coordinate(Coordinate): The coordinate to locateoptions(Options, optional): Configuration options- Returns: A
Locationobject if found, otherwiseNone(unlessraise_on_not_foundisTrue)
Exceptions
InvalidCoordinate
Raised when a coordinate is invalid (outside the valid range).
CoordinateNotFound
Raised when a coordinate cannot be located (when raise_on_not_found is True).
Attribution
- geoBoundaries.org (CC BY 4.0): This library embeds polygons derived from the data made available by geoBoundaries.
- coord2state
(MIT): This library is essentially a python port
Austin Henley's
coord2statejavascript library. Thank you Austin!
Release files for coord2loc 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coord2loc-0.0.3.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coord2loc-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.5 kB
Release files / coord2loc-0.0.3.tar.gz
| Download URL | coord2loc-0.0.3.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e390685a706553cb16736dbff3ab9be9ac40cb6ac2b5f1cf7674cbd57560d515
|
|
BLAKE2b-256 checksum How to use checksums |
9c9ad11301c7745caee0937abe99f1aaa38ded3338c8eb1db4871b5d99086449
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.12
|
Release files / coord2loc-0.0.3-py3-none-any.whl
| Download URL | coord2loc-0.0.3-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8477e92282d5eccedbef10f73b0ef5e69f10e94a876a520d49e2a6f2687ab7a5
|
|
BLAKE2b-256 checksum How to use checksums |
e35695e08f4112f0613ca8ec98f8661591449e70933edc1fd74a001faa690732
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.12
|