Skip to main content

Convert latitude and longitude to U.S. state using geospatial lookup.

Project description

latlng-to-state

Unit Tests Linter PyPi Prod Deploy License: MIT Ruff

Quickly find U.S. states or regions from latitude/longitude coordinates using built-in geospatial data. latlng-to-state is a lightweight Python package for geospatial point-in-geometry analysis. It enables you to determine whether a given latitude/longitude point falls within or intersects any polygon geometry — such as U.S. states or other regions.

The package includes built-in datasets for:

  • 🗺️ U.S. state geometries (from U.S. Census)
  • 📍 U.S. ZIP code centroids (from U.S. Census)

It works seamlessly with GeoPandas and shapely, making it easy to integrate spatial logic into data pipelines, geocoding workflows, and location-based applications.

Disclaimer: This repository is primarily for tutorial and example purposes, demonstrating PyPI package development and GitHub Actions workflows.


🔧 Features

  • 🗺️ Spatial Lookup: Determine if a lat/lng point is contained within or intersects with a polygon (e.g., a U.S. state).
  • 📦 Built-in Data: Includes preloaded U.S. state geometries and U.S zip codes (as a .parquet file).
  • 🧪 Clean API: Simple utility functions for integrating geospatial logic into your own workflows.
  • 🧱 Modular Design: Organized into utilities and core logic for easy extension.

📦 Installation & Dependencies

pip install latlng-to-state

Requires Python ≥ 3.9

This will automatically install the necessary dependencies, including:

  • geopandas
  • shapely
  • pandas
  • pyarrow

🚀 Quickstart

import geopandas as gpd
from latlng_to_state.datasets import load_us_state_geometry
from latlng_to_state.core.lookup import lookup_geometry_by_point

# Load built-in U.S. state geometries
gdf = load_us_state_geometry()

# Sample lat/lng in Texas
lat, lng = 31.9686, -99.9018

# Lookup state name
state_name = lookup_geometry_by_point(lat, lng, gdf, check_type="contain", return_col="state")

print(state_name)  # Output: Texas

lookup_geometry_by_point

lookup_geometry_by_point(
    lat: float,
    lng: float,
    gdf: geopandas.GeoDataFrame,
    check_type: Optional[str] = None,
    return_col: str = "NAME"
) -> Optional[Union[str, int, float]]

Check whether a given lat/lng point falls inside or intersects with any geometry in a GeoDataFrame.

  • lat, lng: Coordinates of the point.
  • gdf: A GeoDataFrame containing geometries (e.g., U.S. states).
  • check_type: "contain", "intersect", or None (tries contain then intersect).
  • return_col: Column to return (e.g., "NAME" for state name).

Returns the matched value from the return_col column, or None if no match.


load_us_state_geometry

from latlng_to_state.datasets import (
    load_us_state_geometry
    , load_us_zipcode_centroid
)

gdf = load_us_state_geometry()

Loads the built-in U.S. state geometry data as a GeoDataFrame.


📁 Included Data

The package includes:

  • A U.S. state geometry and U.S. zipcode centroid dataset (in parquet format) bundled under latlng_to_state/datasets/data.

💡 Data Source Disclosure

The U.S. state geometries and zipcode centroid data included in this package are sourced from the U.S. Census Bureau and are in the public domain. While efforts have been made to ensure data accuracy, this package is provided "as is" without any warranties. Users should verify the data suitability for their specific applications.


📄 License

This project is licensed under the MIT License.


🧑‍💻 Contributing

Contributions welcome! Feel free to open issues or submit PRs on GitHub.

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

latlng_to_state-0.0.1.tar.gz (5.1 MB view details)

Uploaded Source

Built Distribution

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

latlng_to_state-0.0.1-py3-none-any.whl (5.1 MB view details)

Uploaded Python 3

File details

Details for the file latlng_to_state-0.0.1.tar.gz.

File metadata

  • Download URL: latlng_to_state-0.0.1.tar.gz
  • Upload date:
  • Size: 5.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for latlng_to_state-0.0.1.tar.gz
Algorithm Hash digest
SHA256 dd501958dfb3d6c4c217b9c1212696b718c8d77e3c3f247592f8330bd4ae09fe
MD5 87b1367f12607c366bef686a2ef569d2
BLAKE2b-256 dfd477c692ac4f6ba68ae3cd6291711c1310196e888bbce57f0d2725819275b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for latlng_to_state-0.0.1.tar.gz:

Publisher: prod_deploy.yml on AdamIH1/latlng-to-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for latlng_to_state-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c15655ed0bd7e01f39698ebb24fa3abda2c0b73357347ac98de000ad4a4129e1
MD5 747e23597fbc32c93bedd7726c81fd2e
BLAKE2b-256 80c070b88d76c8eacbd7b15f5b352057354bcb94e8aa751487c81d4946f921e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for latlng_to_state-0.0.1-py3-none-any.whl:

Publisher: prod_deploy.yml on AdamIH1/latlng-to-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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