H3 Python Bindings

4.4.2 · active · verified Thu Apr 09

h3 is a set of Python bindings for Uber's H3 C library, a hierarchical hexagonal geospatial indexing system. It allows for efficient spatial indexing, querying, and analysis using hexagonal grids. The current version is 4.4.2, and it typically sees multiple releases per year, often in sync with updates to the underlying H3 C library.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to convert geographic coordinates to an H3 index, retrieve its center and boundary, find its neighbors, and validate an H3 index.

import h3

# Example coordinates: San Francisco
lat, lon = 37.7749, -122.4194

# 1. Get an H3 index for a given lat/lon at a specific resolution
resolution = 9
h3_index = h3.geo_to_h3(lat, lon, resolution)
print(f"H3 Index for ({lat}, {lon}) at resolution {resolution}: {h3_index}")

# 2. Get the geographic coordinates of the center of an H3 index
center_coords = h3.h3_to_geo(h3_index)
print(f"Center coordinates of {h3_index}: {center_coords}")

# 3. Get the boundary (polygon) of an H3 index
boundary = h3.h3_to_geo_boundary(h3_index)
print(f"Boundary of {h3_index} (first 2 points): {boundary[:2]}...")

# 4. Find immediate neighbors of an H3 index (k-ring with distance 1)
neighbors = h3.h3_k_ring(h3_index, 1)
print(f"Neighbors of {h3_index}: {list(neighbors)}")

# 5. Check if an H3 index is valid
is_valid = h3.h3_is_valid(h3_index)
print(f"Is {h3_index} valid? {is_valid}")

view raw JSON →