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.
-
25X faster than h3-py
-
π H3 Feature Parity: Comprehensive support for H3 functions, covering almost everything the standard H3 library provides, excluding geometric functions.
-
π Fully Tested: Accurately 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):
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"),
... )
>>> df
shape: (1, 3)
βββββββββββ¬ββββββββββββ¬ββββββββββββββββββ
β lat β long β h3_cell β
β --- β --- β --- β
β f64 β f64 β str β
βββββββββββͺββββββββββββͺββββββββββββββββββ‘
β 37.7749 β -122.4194 β 872830828ffffff β
βββββββββββ΄ββββββββββββ΄ββββββββββββββββββ
Check out the quickstart notebook for more examples.
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.
β οΈ Performance Note: When possible, prefer using
pl.UInt64
for H3 indices instead of thepl.Utf8
representation. String representations require casting operations which impact performance. Working directly with the native 64-bit integer format provides better computational efficiency.
We are unable to support the functions that work with geometries.
Full list of functions¶
β = Supported π§ = Pending π = Not supported
Full list of functions¶
Function | Description | Supported |
---|---|---|
latlng_to_cell | Convert latitude/longitude coordinates to an H3 cell index. | β |
cell_to_lat | Extract the latitude coordinate from H3 cell indices. | β |
cell_to_lng | Extract the longitude coordinate from H3 cell indices. | β |
cell_to_latlng | Convert H3 cells into a list of [latitude, longitude] . | β |
cell_to_local_ij | Convert an H3 cell index into its local IJ coordinates, relative to a given origin. | β |
local_ij_to_cell | Convert local IJ coordinates back into an H3 cell index. | β |
cell_to_boundary | Retrieve the polygon boundary coordinates of the given H3 cell. | β |
are_neighbor_cells | Check if two H3 cells share a common edge. | β |
cells_to_directed_edge | Create a directed H3 edge from two neighboring cells. | β |
is_valid_directed_edge | Check if an H3 index is a valid directed edge. | β |
directed_edge_to_cells | Retrieve the origin/destination cells from a directed edge. | β |
get_directed_edge_origin | Extract the origin cell from a directed H3 edge. | β |
get_directed_edge_destination | Extract the destination cell from a directed H3 edge. | β |
origin_to_directed_edges | List all directed edges originating from a given cell. | β |
directed_edge_to_boundary | Retrieve the geographic boundary (list of lat/lng pairs) for a directed edge. | β |
get_resolution | Retrieve the resolution of H3 indices (cells, edges, or vertices). | β |
str_to_int | Convert string-based H3 indices into UInt64 representation. | β |
int_to_str | Convert integer-based H3 indices into string form. | β |
is_valid_cell | Check if H3 cell indices are valid. | β |
is_pentagon | Determine if an H3 cell is a pentagon. | β |
is_res_class_III | Check if H3 cells belong to Class III resolution. | β |
get_icosahedron_faces | Retrieve the icosahedron faces intersected by an H3 cell. | β |
cell_to_parent | Retrieve the parent cell of a given H3 cell at a specified resolution. | β |
cell_to_center_child | Retrieve the βcenter childβ of an H3 cell at a finer resolution. | β |
cell_to_children_size | Get the number of child cells at a given resolution. | β |
cell_to_children | Retrieve all child cells at a specified resolution. | β |
cell_to_child_pos | Get the position index of a child cell within its parent hierarchy. | β |
child_pos_to_cell | Get the child cell at a given position index for a specified parent/resolution. | β |
compact_cells | Compact a set of H3 cells into a minimal covering set. | β |
uncompact_cells | Uncompact a set of H3 cells to the specified resolution. | β |
great_circle_distance | Compute the Haversine distance between two sets of lat/lng coordinates. | β |
average_hexagon_area | Get the average area of an H3 hexagon at a given resolution. | β |
cell_area | Get the area of a specific H3 cell. | β |
edge_length | Get the length of an H3 edge cell (currently raises NotImplementedError ). | π§ |
average_hexagon_edge_length | Get the average edge length for hexagons at a given resolution. | β |
get_num_cells | Get the total number of H3 cells at a given resolution. | β |
get_pentagons | Get the number of pentagons at a given resolution (currently raises NotImplementedError ). | π§ |
grid_distance | Compute the grid distance (minimum steps) between two H3 cells. | β |
grid_ring | Produce a βhollow ringβ of cells at distance k from the origin cell. | β |
grid_disk | Produce a βfilled diskβ of cells within distance k of an origin cell. | β |
grid_path_cells | Return the minimal path of cells connecting an origin and destination. | β |
cell_to_vertex | Retrieve the H3 vertex index for a specific vertex of a given cell. | β |
cell_to_vertexes | Retrieve all vertex indices for a given H3 cell (5 for pentagon, 6 for hex). | β |
vertex_to_latlng | Convert an H3 vertex index into its latitude/longitude coordinates. | β |
is_valid_vertex | Check whether an H3 index represents a valid vertex. | β |
cells_to_multi_polygon_wkt | Convert a set of cells to multipolygon WKT. | π (Not supported) |
polygon_wkt_to_cells | Convert polygon WKT to a set of cells. | π (Not supported) |
directed_edge_to_boundary_wkt | Convert directed edge ID to linestring WKT. | π (Not supported) |
Plotting¶
The library also comes with helper functions to plot hexes on a Folium map.
import polars_h3 as pl_h3
import polars as pl
hex_map = pl_h3.graphing.plot_hex_outlines(df, "h3_cell")
display(hex_map)
# or if you have a metric to plot
hex_map = pl_h3.graphing.plot_hex_fills(df, "h3_cell", "metric_col")
display(hex_map)
Development¶
It's recommended to use uv to manage the extension dependencies. If you modify rust code, you will need to run uv run maturin develop --uv
to see changes. 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.