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_wkt Convert directed edge ID to linestring WKT 🛑

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_h3-0.7.0.tar.gz (3.5 MB view details)

Uploaded Source

Built Distributions

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

polars_h3-0.7.0-cp311-abi3-win_amd64.whl (5.3 MB view details)

Uploaded CPython 3.11+Windows x86-64

polars_h3-0.7.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

polars_h3-0.7.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.1 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

polars_h3-0.7.0-cp311-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (5.9 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.12+ i686

polars_h3-0.7.0-cp311-abi3-macosx_11_0_arm64.whl (4.9 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

polars_h3-0.7.0-cp311-abi3-macosx_10_12_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file polars_h3-0.7.0.tar.gz.

File metadata

  • Download URL: polars_h3-0.7.0.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.15.0

File hashes

Hashes for polars_h3-0.7.0.tar.gz
Algorithm Hash digest
SHA256 4022c0268d7cbc7bf62e567fa7fb54861c697dee6422bc159820577e7e8b68ac
MD5 ce80753fe437880c22cfcd5e2f857f97
BLAKE2b-256 2141200ac696288f67099e525e32a01a6e83c8607c25157512f3502780a899c9

See more details on using hashes here.

File details

Details for the file polars_h3-0.7.0-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_h3-0.7.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8a8f3fe91efba265a45751ad5a7674a86e75607fa42a5f6d43888ce4d792d990
MD5 6f67b520629b1bb6d22bf226d1b3734f
BLAKE2b-256 b94b1eded1b3fe0aaf9e6d6194f76f5e5601eba5238f8bc52d9f20ae19a263dc

See more details on using hashes here.

File details

Details for the file polars_h3-0.7.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_h3-0.7.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 71ac8532373f9253af0f515b569beb2f9c91a40c178b82d1e3fcedb6d4332508
MD5 c3f1908e8da44a4450adafbcced4acc0
BLAKE2b-256 44b35de8a5cc65db4eb910d54a8116d2f8fe4383c9a4f785635b7e774ce7a177

See more details on using hashes here.

File details

Details for the file polars_h3-0.7.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_h3-0.7.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b03a323a810f39893ac2447f6d80ad63b83cb96aa72bee1b2320fdb3500692e8
MD5 cb451c86cdd4c0c79c86a4737f5b0570
BLAKE2b-256 0fe26469c191bb55c0f35dca81170626a3df7d3d43c92b301030b4480357d848

See more details on using hashes here.

File details

Details for the file polars_h3-0.7.0-cp311-abi3-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for polars_h3-0.7.0-cp311-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 eb8998c48d1d9dbee58352910d403e678546b0960f85c7c729837e1add8aeb2c
MD5 73b2c1651b40ec4978b3c9fbe586d99d
BLAKE2b-256 30ff116bfdb8998f3bc1dedb4241ab4dfff676297314cb55da55dc8397490e15

See more details on using hashes here.

File details

Details for the file polars_h3-0.7.0-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_h3-0.7.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 242b77f8110613b5d03c12146cee5d8a28e862c4e3aa7ab352aa1c16dcb1b92b
MD5 248dc930800c5f2e20571450a503a6fa
BLAKE2b-256 762816c91a003eec6bc05f1ad46317d510026b94b5f5107285b0cc59a9408895

See more details on using hashes here.

File details

Details for the file polars_h3-0.7.0-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_h3-0.7.0-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9e287b9bf97c925186a6933ee0c1d15c7e66eb2e5df30385179945325c62dfcf
MD5 99d2dfce0df36532e4946b198c4ddcb8
BLAKE2b-256 b28656dc97b5550f712eefee6255d1e5608f9b8cef0efb86ebf4a9ecdc0c5dfe

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.7.0 This release

7 files

0.6.4

7 files

0.6.3

7 files

0.6.1

7 files

0.5.7

6 files

0.5.6

6 files

0.5.5

6 files

0.5.4

6 files

0.5.3

6 files

0.5.1

6 files

0.4.6

6 files

0.4.5

6 files

0.4.3

6 files

0.4.0

1 file

0.3.0

1 file

0.1.0

1 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