A Python library for locating area codes from latitude and longitude coordinates
Project description
Area Code Locator
A Python library for locating area codes (NPAs) from latitude and longitude coordinates using geospatial data.
Features
- Fast area code lookup from geographic coordinates
- Supports Parquet format for efficient data storage
- Handles boundary cases with buffer searches
- Uses spatial indexing for performance
- Automatic coordinate system handling
Installation
Install from source:
git clone https://github.com/Eat-A-Fish/area-code-locator.git
cd area-code-locator
pip install -e .
Or install dependencies directly:
pip install -r requirements.txt
Requirements
- Python 3.8+
- GeoPandas
- Shapely
- PyProj
- PyArrow (for Parquet file support)
Data Setup
The library comes with pre-processed area code boundary data, so no setup is required! The area-codes.parquet file (29MB) is included in the repository and contains comprehensive North American area code boundaries.
Data Source
The area code data is derived from the nanp-boundaries project by Elijah Verdoorn, which provides authoritative GeoJSON boundaries for North American Numbering Plan (NANP) area codes.
Using Custom Data
If you prefer to use your own area code data source:
- Obtain a Parquet file containing area code polygons
- Ensure it has:
- A geometry column with polygon data
- An area code column (named
area_code,areacode,npa, orcode) - Coordinate Reference System (CRS) set to EPSG:4326 (WGS84)
- Replace the
area-codes.parquetfile or specify a different path
Usage
from area_code_locator import AreaCodeLocator
# Use included data (recommended)
locator = AreaCodeLocator()
# Lookup area code for a location
area_code = locator.lookup(40.7128, -74.0060) # New York City
print(area_code) # ['212']
# Get all matching area codes
all_codes = locator.lookup(34.0522, -118.2437, return_all=True) # Los Angeles
print(all_codes) # ['213', '310', ...]
# Or use custom data
locator_custom = AreaCodeLocator("path/to/your/area-codes.parquet")
API Reference
AreaCodeLocator
__init__(path: Optional[str] = None, projected_epsg: int = 5070)
Initialize the locator with area code data.
path: Path to a Parquet file containing area code polygons. If None, uses the included data.projected_epsg: EPSG code for projected coordinate system (default: 5070 - NAD83/Conus Albers)
lookup(lat: float, lon: float, return_all: bool = True) -> Union[str, List[str]]
Find area codes for the given latitude and longitude.
lat: Latitude in decimal degreeslon: Longitude in decimal degreesreturn_all: If True, return all matching codes; if False, return only the first match
Returns a list of area codes or a single area code string.
Algorithm
The lookup uses a hierarchical approach:
- Exact match: Check if the point is exactly within any area code polygon
- Buffer search: If no exact match, search within a 50-meter buffer around the point
- Expanding search: If still no match, search in expanding circles (25km, 100km, 300km) and return the nearest area code
Development
For development, install with dev dependencies:
pip install -e ".[dev]"
Run tests:
pytest
Acknowledgments
This project uses area code boundary data derived from nanp-boundaries by Elijah Verdoorn, which provides comprehensive GeoJSON boundaries for North American Numbering Plan (NANP) area codes.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file area_code_locator-0.1.0.tar.gz.
File metadata
- Download URL: area_code_locator-0.1.0.tar.gz
- Upload date:
- Size: 23.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b3a97aeeea6243ea40a37581abe1aaeb62bed293a33351192e388fcbbd8b793
|
|
| MD5 |
9a42b31333509ac214fb5b5ba31d991f
|
|
| BLAKE2b-256 |
f0c3525753ea525feb550ae0b7f38c2bf46734bbb6b55a843f84fd9d9896ddbf
|
File details
Details for the file area_code_locator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: area_code_locator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.1 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03daed86eab2f59b790c6d4b1982a2f0852f1003f75c414102cd878404cd15a2
|
|
| MD5 |
7baa402c952daf394df3d0991b3c23af
|
|
| BLAKE2b-256 |
34bfb0e00673ad9094a3b429ae4c0f74d96d2bf5ea6ede8e1a98f21b318e6c92
|