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. It uses a KDTree for quick nearest-neighbor lookup, making it suitable for geospatial queries.

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 KDTree for quick nearest-neighbor lookup, making it suitable for geospatial queries.

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

Current version: 0.1.3

Features

  • Quickly find the closest ZIP code for a given latitude and longitude.
  • Efficient spatial indexing using 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.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

geo2zip-0.1.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: geo2zip-0.1.3.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for geo2zip-0.1.3.tar.gz
Algorithm Hash digest
SHA256 dcc17623fa7f8266c4c31004b94f7f9a42ce896d4f718842d0c78af617c6bb63
MD5 4e4abf5203aa778cedbb41efec12c5eb
BLAKE2b-256 87e9f5ad6ec038bb5000baeb9a91c55f4aa787190f37e18849e1ffe12af08533

See more details on using hashes here.

File details

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

File metadata

  • Download URL: geo2zip-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for geo2zip-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dfaca48e606cd3054e33f1d74a4f5b3d0458fb47f018edb0599214a112c8c7a6
MD5 58cefd74756cddf5026129184a63aa53
BLAKE2b-256 82521af3aaeff411cb002f0537067189100d8109806e68e33b0372271f341956

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page