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

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 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 Distribution

coord2loc-0.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

coord2loc-0.0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file coord2loc-0.0.3.tar.gz.

File metadata

  • Download URL: coord2loc-0.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.12

File hashes

Hashes for coord2loc-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e390685a706553cb16736dbff3ab9be9ac40cb6ac2b5f1cf7674cbd57560d515
MD5 439d7d7593832fbc97b34950b3b9cd3a
BLAKE2b-256 9c9ad11301c7745caee0937abe99f1aaa38ded3338c8eb1db4871b5d99086449

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for coord2loc-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8477e92282d5eccedbef10f73b0ef5e69f10e94a876a520d49e2a6f2687ab7a5
MD5 3784a4d3e4ed522a5c4cee563f83f694
BLAKE2b-256 e35695e08f4112f0613ca8ec98f8661591449e70933edc1fd74a001faa690732

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