H3 bindings for Polars
Project description
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, covering almost everything the standard H3 library provides, excluding geometric functions.
-
๐ Fully Tested: 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"),
... )
>>> 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 - you can find full docs here.
โ ๏ธ Performance Note: When possible, prefer using
pl.UInt64for H3 indices instead of thepl.Utf8representation. 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
| 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 (currently raises NotImplementedError) |
โ |
get_num_cells |
Get the number of cells at a resolution | โ |
get_pentagons |
Get all pentagons at a resolution (currently raises NotImplementedError) |
โ |
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_wkt_to_cells |
Convert polygon WKT 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, "h3_cell")
display(hex_map)
# or if you have a metric to plot
hex_map = plh3.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_h3-0.5.5.tar.gz.
File metadata
- Download URL: polars_h3-0.5.5.tar.gz
- Upload date:
- Size: 422.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfe93d8f0b78413c00af11a87bc1343b06eb4be036de8d21975ed18402981f43
|
|
| MD5 |
15b815e392871fd6f59b9cce0c7e0edb
|
|
| BLAKE2b-256 |
efe5967a997cd02a6142bb02a679cb56a0d084e68a1dc9ca2c9262841a9fa2b4
|
File details
Details for the file polars_h3-0.5.5-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: polars_h3-0.5.5-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
718c059f6f8f7dbec49b82440bf342545fab9d992bf1c0017e65c0fb2f019a73
|
|
| MD5 |
cfac4b2090d31a410856bcc5b2209b64
|
|
| BLAKE2b-256 |
7ec1d9cb82fc0ea491c03e14c401f86cbb61b8df64da35f0e83eb3ed5344af40
|
File details
Details for the file polars_h3-0.5.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: polars_h3-0.5.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75ce25f04c281050aceba4d0c5372ec1f8adbc35e411c0d783f5232b676f7347
|
|
| MD5 |
8243601d28e4a580933bfa8f405b39f5
|
|
| BLAKE2b-256 |
03eaaccb3125bbd94ce25918d5923190133ce548ca50435560c6deff61d798e2
|
File details
Details for the file polars_h3-0.5.5-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl.
File metadata
- Download URL: polars_h3-0.5.5-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.8+, manylinux: glibc 2.12+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8654a8049589b93ad9913b2695ceff440a9179e6ad9c9b4472b5dd3deb4b005e
|
|
| MD5 |
c05fb9e290e94223098be6d92bb50b08
|
|
| BLAKE2b-256 |
78e87d9e5e845b5e18ccecc43d1d04648d2d477922cc43b246036c58a548f2f4
|
File details
Details for the file polars_h3-0.5.5-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: polars_h3-0.5.5-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.8 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d37076568fbdd72c8d0c601a61090ceb3eb2670b49704da1cee6dfe178ea8122
|
|
| MD5 |
9ed3199446d75bd4f44d0741a0d6b3f0
|
|
| BLAKE2b-256 |
17e0acb6d4ab174ad58a298a49a2ce82037626a489dc227574b7ac92e688e284
|
File details
Details for the file polars_h3-0.5.5-cp38-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: polars_h3-0.5.5-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0697fd858f5655fb6ec6d65ae4593f6a96d7732c5042b8200a3f1776fe963ad2
|
|
| MD5 |
0dd0059206a040448f04011f67e62846
|
|
| BLAKE2b-256 |
b9588090f0e50a1450fe85175cc0266a961036e6d79fa55f068391349670dcd1
|