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_usa

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.2.tar.gz (4.3 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.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for coord2loc-0.0.2.tar.gz
Algorithm Hash digest
SHA256 139f8ab75691154d247385639399b30269b162a4a1af22958031e0c0d1e672db
MD5 a9a3730a27f083612610309bc9fa5bcc
BLAKE2b-256 991a62cca93213e48c866cea5ff0fe046f96d083cde9e9a62743daa281e18c9b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for coord2loc-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 39673610aa1c785633c3ba39acd75a102ed93132f5ca313468a85a8440de5081
MD5 421eea18d46bf1d2e8b23ada4f62fb8e
BLAKE2b-256 27b83846c5ef77f0b2d2b18c3ff69ec8fbd485a5c401b7722c78fc471247ee68

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