Skip to main content

Given a coordinate point, find a very close point given a set of coordinate points.

Project description

GeoKDTree

PyPI version License: MIT PyPI Downloads

GeoKDTree

Ultra-fast nearest-neighbor lookup for latitude/longitude data

GeoKDTree is a lightweight, high-performance spatial indexing library for Python designed to find the nearest geographic coordinate from massive datasets in nanoseconds.

It wraps a highly optimized KD-Tree with a geographic interface, allowing you to work directly with (latitude, longitude) pairs. No projections, no external dependencies, and no heavy GIS stacks.

geokdtree

Documentation

Installation

pip install geokdtree

Getting Started

from geokdtree import GeoKDTree

example_points = [
    (34.0522, -118.2437),  # Los Angeles
    (40.7128, -74.0060),   # New York
    (37.7749, -122.4194),  # San Francisco
    (51.5074, -0.1278),    # London
    (48.8566, 2.3522),     # Paris
]

geo_kd_tree = GeoKDTree(points=example_points)

test_point = (47.6062, -122.3321)  # Seattle
# Find the index of the closest point in the original dataset
closest_idx = geo_kd_tree.closest_idx(test_point)
# Find the closest point itself
closest_point = geo_kd_tree.closest_point(test_point)

print(f"Closest index (from original data) is {closest_idx}")
print(f"Closest point (from original data) is {closest_point}")

Why Use GeoKDTree?

GeoKDTree is designed to solve one focused problem extremely well:

Fast nearest-neighbor lookup for latitude/longitude data at scale.

It is worth noting that the closest point found may not be the true closest point, but should be very close for most practical applications. See KD-Tree limitations for more details.

Extremely Fast Lookups

Once constructed, nearest-neighbor queries consistently complete in tens of nanoseconds, even with very large datasets.

Typical benchmark results from the included tests:

Number of Points Build Time Query Time
1,000 ~1.7 ms ~0.02 ms
10,000 ~25 ms ~0.05 ms
100,000 ~350 ms ~0.05 ms
1,000,000 ~6.8 s ~0.07 ms

This makes GeoKDTree well-suited for:

  • Real-time proximity queries
  • Matching incoming coordinates against large reference datasets
  • High-throughput geospatial APIs
  • Pre-filtering before more expensive geospatial calculations

Exact timings depend on hardware, Python version, and data distribution. These values reflect typical results from the repository’s benchmarks.

Built for Geographic Coordinates

GeoKDTree works directly with (latitude, longitude) pairs.

You do not need to:

  • Project coordinates into planar space
  • Use heavyweight GIS libraries
  • Maintain custom spatial indexing code

Just pass geographic coordinates and query.

Simple API, Minimal Overhead

GeoKDTree intentionally keeps the API small and focused.

  • Build once from a list of coordinates
  • Query nearest neighbors with a single method call
  • Retrieve indices or points directly from your original dataset

There are no external C extensions or heavy dependencies, keeping installation and deployment simple.

Deterministic and Predictable Performance

  • Tree construction scales at approximately O(n log n)
  • Query performance scales at approximately O(log n)
  • No probabilistic approximations
  • No background indexing or caching

This predictability is valuable for production systems where latency and reproducibility matter.

Supported Features

See: https://connor-makowski.github.io/geokdtree/geokdtree.html

Contributing

Issues, feature requests, and pull requests are welcome. Please open an issue to discuss changes or enhancements.

Development

Running Tests, Prettifying Code, and Updating Docs

Make sure Docker is installed and running on a Unix system (Linux, MacOS, WSL2).

  • Create a docker container and drop into a shell

    • ./run.sh
  • Run all tests (see ./utils/test.sh)

    • ./run.sh test
  • Prettify the code (see ./utils/prettify.sh)

    • ./run.sh prettify
  • Update the docs (see ./utils/docs.sh)

    • ./run.sh docs
  • Note: You can and should modify the Dockerfile to test different python versions.

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

geokdtree-1.0.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

geokdtree-1.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file geokdtree-1.0.1.tar.gz.

File metadata

  • Download URL: geokdtree-1.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for geokdtree-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b37847170412f9a253292010eb6afcd997ff43ecbdc1c404a59f7bf4fc2b86d4
MD5 e84c35e92f05daf2be5477b566406bd3
BLAKE2b-256 b1b57c120322ddf287d05648fe295721560f11b753058da86da31bd02b037cbc

See more details on using hashes here.

File details

Details for the file geokdtree-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: geokdtree-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for geokdtree-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f17814872758453c4de3e612d93355e6b3f79d3352a4a71bd2ace34db0c542d5
MD5 6aa739379f52a080071a3e2f3edc596c
BLAKE2b-256 d3d3fbbe281d470c6aad6951c8b8e9b042f6b5b32bb26afb9adfed22ba366f0d

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