Skip to main content

This is a Polars extension that adds support for the H3 discrete global grid system, so you can index points and geometries to hexagons directly in Polars. All credits goes to the h3o for doing the heavy lifting.

Highlights

  • 🚀 Blazing Fast: Built entirely in Rust, offering vectorized, multi-core H3 operations within Polars. Ideal for high-performance data processing.

  • 🌍 H3 Feature Parity: Comprehensive support for H3 functions, including WKT/WKB polygon coverage and cell-set dissolution.

  • 📋 Fully Tested & Used in Production: Thoroughly tested against the standard H3 library.

  • 🔍 Data Type Agnostic: Supports string and integer H3 indexes natively, eliminating format conversion hassles.

Get started

You can get started by installing it with pip (or uv):

pip install polars-h3

You can use the extension as a drop-in replacement for the standard H3 functions.

import polars_h3 as plh3
import polars as pl

df = (
    pl.DataFrame(
        {
            "lat": [37.7749],
            "long": [-122.4194],
        }
    )
    .with_columns(
        plh3.latlng_to_cell(
            "lat",
            "long",
            resolution=7,
            return_dtype=pl.Utf8,
        ).alias("h3_cell")
    )
)

print(df)
shape: (1, 3)
┌─────────┬───────────┬─────────────────┐
│ lat     ┆ long      ┆ h3_cell         │
│ ---     ┆ ---       ┆ ---             │
│ f64     ┆ f64       ┆ str             │
╞═════════╪═══════════╪═════════════════╡
│ 37.7749 ┆ -122.4194 ┆ 872830828ffffff │
└─────────┴───────────┴─────────────────┘

Check out the quickstart notebook for more examples. The polygon-to-H3 notebook starts with raw census-tract GeoParquet, compares H3 resolutions, builds a validated tract-to-cell crosswalk, and closes the cell-set geometry round trip without GeoPandas. The telematics notebook turns timestamped GPS points into trips, traces the H3 cells traveled, and estimates time spent in each cell.

🌟 You can also find the advanced notebooks here.

Implemented functions

This extension implements most of the H3 API. The full list of functions is below - you can find full docs here.

⚠️ Performance Note: When possible, prefer using pl.UInt64 for H3 indices instead of the pl.Utf8 representation. String representations require casting operations which impact performance. Working directly with the native 64-bit integer format provides better computational efficiency.

Geometry conversions use WKT String or WKB Binary columns and do not require a Polars geometry dtype.

Full list of functions

✅ = Supported 🚧 = Pending 🛑 = Not supported

Function Description Supported
latlng_to_cell Convert latitude/longitude coordinate to cell ID ✅
cell_to_lat Convert cell ID to latitude ✅
cell_to_lng Convert cell ID to longitude ✅
cell_to_latlng Convert cell ID to latitude/longitude ✅
get_resolution Get resolution number of cell ID ✅
str_to_int Convert pl.Utf8 cell ID to pl.UInt64 ✅
int_to_str Convert pl.UInt64 or pl.Int64 cell ID to pl.Utf8 ✅
is_valid_cell True if this is a valid cell ID ✅
is_res_class_iii True if the cell's resolution is class III ✅
is_pentagon True if the cell is a pentagon ✅
get_icosahedron_faces List of icosahedron face IDs the cell is on ✅
cell_to_parent Get coarser cell for a cell ✅
cell_to_children Get finer cells for a cell ✅
cell_to_center_child Provides the center child (finer) cell contained by cell at resolution childRes. ✅
cell_to_child_pos Position of the child cell within the ordered list of all children of its parent at the specified resolution ✅
child_pos_to_cell Child cell at a given position within the ordered list of children for a specified parent/resolution ✅
compact_cells Compacts a collection of H3 cells (all same resolution) by replacing child cells with their parent if all children exist ✅
uncompact_cells Uncompacts a set of H3 cells to the resolution res ✅
grid_ring Produces the "hollow ring" of cells which are exactly grid distance k from the origin cell ✅
grid_disk Produces the "filled-in disk" of cells at most grid distance k from the origin cell ✅
grid_path_cells Find a grid path to connect two cells ✅
grid_distance Find the grid distance between two cells ✅
cell_to_local_ij Convert a cell ID to a local I,J coordinate space ✅
local_ij_to_cell Convert a local I,J coordinate to a cell ID ✅
cell_to_boundary Convert cell ID to its boundary lat/lng coordinates ✅
cell_to_vertex Get the vertex ID for a cell ID and vertex number ✅
cell_to_vertexes Get all vertex IDs for a cell ID (5 for pentagon, 6 for hex) ✅
vertex_to_latlng Convert a vertex ID to latitude/longitude coordinates ✅
is_valid_vertex True if passed a valid vertex ID ✅
is_valid_directed_edge True if passed a valid directed edge ID ✅
origin_to_directed_edges Get all directed edge IDs for a cell ID ✅
directed_edge_to_cells Convert a directed edge ID to origin/destination cell IDs ✅
get_directed_edge_origin Convert a directed edge ID to origin cell ID ✅
get_directed_edge_destination Convert a directed edge ID to destination cell ID ✅
cells_to_directed_edge Convert an origin/destination pair to directed edge ID ✅
are_neighbor_cells True if the two cell IDs share an edge ✅
average_hexagon_area Get average area of a hexagon cell at resolution ✅
cell_area Get the area of a cell ID ✅
average_hexagon_edge_length Average hexagon edge length at resolution ✅
edge_length Get the length of a directed edge ID ✅
get_num_cells Get the number of cells at a resolution ✅
get_pentagons Get all pentagons at a resolution ✅
great_circle_distance Compute the great circle distance between two points (haversine) ✅
cells_to_multi_polygon_wkt Convert a set of cells to multipolygon WKT ✅
polygon_to_cells Convert polygon WKT or WKB to a set of cells ✅
directed_edge_to_boundary Convert a directed edge ID to its boundary coordinates ✅

Plotting

The library also comes with helper functions to plot hexes on a Folium map.

import polars_h3 as plh3
import polars as pl

hex_map = plh3.graphing.plot_hex_outlines(df, hex_id_col="h3_cell")
display(hex_map)

# overlay a polygon coverage with its source WKT/WKB geometry column
coverage_map = plh3.graphing.plot_polygon_coverage(
    covered,
    geometry_col="geometry",
    cells_col="h3_cells",
)
display(coverage_map)

# or if you have a metric to plot

hex_map = plh3.graphing.plot_hex_fills(
    df,
    hex_id_col="h3_cell",
    metric_col="metric_col",
)
display(hex_map)

CleanShot 2024-12-08 at 00 26 22

Development

It's recommended to use uv to manage the extension's python dependencies. If you modify rust code, you will need to run uv run maturin develop --uv to see changes.

You can run test suite with uv run pytest. You can also run the docs locally with uv run --group docs zensical serve.

Benchmarking

If you're looking to benchmark the performance of the extension, build the release version with maturin develop --release --uv and then run uv run -m benchmarks.engine (assuming you have the benchmark dependencies installed). Benchmarking with the development version will lead to misleading results.

# 1 – (build) compile the optimized Rust extension
uv run maturin develop --release --uv

# 2 – (run) execute the benchmark CLI
uv run h3-bench \
  --libraries plh3 duckdb h3_py \   # which back-ends to test (or “all”)
  --functions latlng_to_cell cell_to_parent \  # which functions to time (or “all”)
  --iterations 3 \                  # repetitions per test
  --fast-factor 4 \                 # divide default row-counts to speed things up
  --output results.json             # optional: dump raw results

Metadata

Release files for polars-h3 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for polars-h3 0.7.1
File Size Uploaded
polars_h3-0.7.1.tar.gz 3.5 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for polars-h3 0.7.1
File
polars_h3-0.7.1-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
polars_h3-0.7.1-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details
polars_h3-0.7.1-cp311-abi3-musllinux_1_2_i686.whl CPython 3.11 abi3 Linux musl 1.2+ x86-32 Details
polars_h3-0.7.1-cp311-abi3-musllinux_1_2_aarch64.whl CPython 3.11 abi3 Linux musl 1.2+ ARM64 Details
polars_h3-0.7.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details
polars_h3-0.7.1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details
polars_h3-0.7.1-cp311-abi3-manylinux_2_12_i686.manylinux2010_i686.whl CPython 3.11 abi3 Linux glibc 2.12+ x86-32 Details
polars_h3-0.7.1-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details
polars_h3-0.7.1-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details

Total release size: 52.0 MB

Release files / polars_h3-0.7.1.tar.gz

Download URL polars_h3-0.7.1.tar.gz
Size 3.5 MB
Tags Source
SHA-256 checksum
How to use checksums
dab7be2e40aa4d4569240bb7435d1b4a13aeb9923d837f1cd97269d058d5ab91
BLAKE2b-256 checksum
How to use checksums
1e2a9f658d4190692c63a846a7f5f7ca2713ada424806feaabbeadd2326df922
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-win_amd64.whl

Download URL polars_h3-0.7.1-cp311-abi3-win_amd64.whl
Size 5.3 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6e58cc2af492df786c5a441884b24ef32f2ea2c826fdc616b48c0fbc3088ffeb
BLAKE2b-256 checksum
How to use checksums
63de14c460fb1b96e30715b0074857d5e97a47bdfa6f398256339152e6c1c518
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL polars_h3-0.7.1-cp311-abi3-musllinux_1_2_x86_64.whl
Size 5.6 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
c1c9d1947ca079d863765f778af0e5ff07167036ef9bb4a53ec30ccb707aff6b
BLAKE2b-256 checksum
How to use checksums
b0bad75c8ea7ab3ab3b291ac10fc0cef5290ec67d54b81a03c4f57d7648c91a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-musllinux_1_2_i686.whl

Download URL polars_h3-0.7.1-cp311-abi3-musllinux_1_2_i686.whl
Size 5.9 MB
Tags CPython 3.11 Linux musl 1.2+ x86-32 abi3
SHA-256 checksum
How to use checksums
0c6fabe96a479b96d5221580a68b17091640cdccb96555f240bf9c157eba9b78
BLAKE2b-256 checksum
How to use checksums
c57f85356a588f3c0365d407686b279df52e04cf4520184929c374a45352e28c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-musllinux_1_2_aarch64.whl

Download URL polars_h3-0.7.1-cp311-abi3-musllinux_1_2_aarch64.whl
Size 5.2 MB
Tags CPython 3.11 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
e64a30996edc9a62fc938cabf9657d4307fb049a8295f59016386067686d1251
BLAKE2b-256 checksum
How to use checksums
393398084b922e8cfe271692e396c8849fbe7a28a86068a8db657e9d1c524107
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL polars_h3-0.7.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 5.4 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
72edc59e10987a475636f67cf549c894eaa8042a06b9a742ff32a0b51c9fb372
BLAKE2b-256 checksum
How to use checksums
0f87a99f32be4f630988902604020305cbae8e6b0d628a575183833ce40c7dc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL polars_h3-0.7.1-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 5.1 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
bcf04394c1d7453c36f5621ea491d40db93fd36e8ead66eed485759ed9336ad3
BLAKE2b-256 checksum
How to use checksums
19635f63b6bfe83deeccc81988c4974ace548a40b75941f610dd5e99e0bf02b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-manylinux_2_12_i686.manylinux2010_i686.whl

Download URL polars_h3-0.7.1-cp311-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Size 5.9 MB
Tags CPython 3.11 Linux glibc 2.12+ x86-32 abi3
SHA-256 checksum
How to use checksums
b11b7121d81a28cce8cf496388268555f981db5df9e07dfdd20423a735d30084
BLAKE2b-256 checksum
How to use checksums
c83c179f092224907b3cc5c9a8126d70e3163c08860e139255f42afe0b0555bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-macosx_11_0_arm64.whl

Download URL polars_h3-0.7.1-cp311-abi3-macosx_11_0_arm64.whl
Size 4.9 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a5937aab522ef99b0929389d2af49bcc4968fb14cc6f6bfd92d9304b30e40386
BLAKE2b-256 checksum
How to use checksums
b8d046f3edf436b427aaf1b7e66847a39d653631f9e888dce04ac0f827ade893
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / polars_h3-0.7.1-cp311-abi3-macosx_10_12_x86_64.whl

Download URL polars_h3-0.7.1-cp311-abi3-macosx_10_12_x86_64.whl
Size 5.2 MB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
f35863f01e4815262d7bd42a5df281d3f6115cf513eb4caed4c3e565ba5f233d
BLAKE2b-256 checksum
How to use checksums
8f36e718e6d427c91f2ab1313fe1cb1fab3d46b2e319775701f4ac4f10f6d620
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release history Release notifications | RSS feed

This release

0.7.1 This release

10 release files

0.7.0

7 release files

0.6.4

7 release files

0.6.3

7 release files

0.6.1

7 release files

0.5.7

6 release files

0.5.6

6 release files

0.5.5

6 release files

0.5.4

6 release files

0.5.3

6 release files

0.5.1

6 release files

0.4.6

6 release files

0.4.5

6 release files

0.4.3

6 release files

0.4.0

1 release file

0.3.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page