Skip to main content

Geo2Zip is a Python package that provides a fast and efficient way to find the closest US ZIP code for a given latitude and longitude.

Project description

Geo2Zip

Build and Test

Geo2Zip is a Python package that provides a fast and efficient way to find the closest ZIP/Postal code for a given latitude and longitude. It uses a small built-in KDTree for nearest-neighbor lookup, avoiding heavyweight spatial dependencies.

The package includes Canada and US ZIP/postal codes and their geolocation as out-of-the-boxdatasets.

Current version: 0.1.7

Features

  • Quickly find the closest ZIP code for a given latitude and longitude.
  • Efficient spatial indexing using a built-in KDTree.
  • Easy-to-use command-line interface.

Installation

Prerequisites

  • Python 3.7 or higher
  • pip

Install the package

  1. Clone the repository:

    git clone https://github.com/jlopex/geo2zip.git
    cd geo2zip
    
  2. Install the package and its dependencies:

    pip install -e .
    
  3. Install development dependencies (for testing):

    pip install -e .[dev]
    

Usage

Command Line Interface

You can use the geo2zip command to find the closest ZIP code for a given latitude and longitude.

geo2zip <latitude> <longitude>

Example:

geo2zip 37.7749 -122.4194

This command will output the closest ZIP code to the provided coordinates.

As a Python Library

You can also use the Geo2Zip class directly with your own dataset in your Python code doing:

from geo2zip import Geo2Zip

# Initialize with the path to your data file
geo2zip = Geo2Zip('path/to/geo_zip/data/your.csv')

# Find the closest ZIP code
latitude = 37.7749
longitude = -122.4194
closest_zip = geo2zip.find_closest_zip(latitude, longitude)
print(f"The closest ZIP code to ({latitude}, {longitude}) is {closest_zip}")

Data

The dataset used for US ZIP codes and their coordinates is extracted from the 2023 US Gazetteer Files. The CSV file is included in the package at geo2zip/data/us.csv.

The dataset used for Canada postal codes and their coordinates is taken from here. The CSV file is included in the package at geo2zip/data/ca.csv.

Testing

Simple and stupid Tests are written using pytest. To run the tests, execute the following command:

pytest tests/

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

geo2zip-0.1.7.tar.gz (8.5 MB view details)

Uploaded Source

Built Distribution

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

geo2zip-0.1.7-py3-none-any.whl (8.6 MB view details)

Uploaded Python 3

File details

Details for the file geo2zip-0.1.7.tar.gz.

File metadata

  • Download URL: geo2zip-0.1.7.tar.gz
  • Upload date:
  • Size: 8.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for geo2zip-0.1.7.tar.gz
Algorithm Hash digest
SHA256 b8019730f47f1a8b341128fbeab1c7f06d2fea39c1796f141104281f0c69a9b0
MD5 d605a08210cde172a6a3037c20c2f2e6
BLAKE2b-256 e5dac444a088d5f9fe59d8ec130174b567a9a2001798170053fbacdd6a1c2070

See more details on using hashes here.

File details

Details for the file geo2zip-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: geo2zip-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 8.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for geo2zip-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 70daa1949755f2782acc057c278400c4cccf50ff21386500da048434e3efddc9
MD5 ec9e98e2eb0ed874a69f1881884ae5a4
BLAKE2b-256 009f09d462ef5f1602603b0aa50d2ed2a824b9b403e7e2d761c2d7454fd8f44d

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