Skip to main content

Convert latitude and longitude into localities like countries and states

Project description

coord2loc

Convert latitude and longitude coordinates into localities like countries and states.

Python 3.9+ PyPI version

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

pip install coord2loc

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 name
  • administrative_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 coordinates
  • raise_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 locate
  • options (Options, optional): Configuration options
  • Returns: A Location object if found, otherwise None (unless raise_on_not_found is True)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coord2loc-0.0.1-py3-none-any.whl (103.3 MB view details)

Uploaded Python 3

File details

Details for the file coord2loc-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: coord2loc-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 103.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.12

File hashes

Hashes for coord2loc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 147a307649219c2c4aa47b4794cedf99cb20cd19665ecf4af3e038fdac10a6ad
MD5 7d622dcdd85e0e088ef5b4bde570bce7
BLAKE2b-256 9a2ba12c93796b6cc14cd2d759da9b4f40ecb4272438d4e5b7a1279360c06cbd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page