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, covering almost everything the standard H3 library provides, excluding geometric functions.

  • 📋 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.

🌟 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.

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
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_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)

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.6.4.tar.gz (833.6 kB 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.6.4-cp38-abi3-win_amd64.whl (4.2 MB view details)

Uploaded CPython 3.8+Windows x86-64

polars_h3-0.6.4-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

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

polars_h3-0.6.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

polars_h3-0.6.4-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (4.5 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.12+ i686

polars_h3-0.6.4-cp38-abi3-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

polars_h3-0.6.4-cp38-abi3-macosx_10_12_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for polars_h3-0.6.4.tar.gz
Algorithm Hash digest
SHA256 3d7462fc94cfd011536391fd65036a967445c50398e2d24168183ba42586dc8a
MD5 6d59356173595b99f65dd96c72403e0b
BLAKE2b-256 1f24bca55d5053a50cb7d7e930602b4fbfcafde5a4c79d3d30e9e52497dd70f5

See more details on using hashes here.

File details

Details for the file polars_h3-0.6.4-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: polars_h3-0.6.4-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.15.0

File hashes

Hashes for polars_h3-0.6.4-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 220fe3e45501a024d1592af7d757486d746d2ec1043daf4c5da495ee8816a06e
MD5 ed559ade39b2d3bf4fae885e897af53a
BLAKE2b-256 9775566c9359f8f4ca6f77fadbd48be2572e03677a63511641664a2ad7a4d19e

See more details on using hashes here.

File details

Details for the file polars_h3-0.6.4-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_h3-0.6.4-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f708e1d3c883d87d5e09dd76a2f82a8f08f6dc08c381b5bf0a467739c7a1df93
MD5 e8a6ccac79a8915a896725408869fdfe
BLAKE2b-256 b59db587c5e3cc70b223a2492f91f933b728d6dfac5ec8635d787855bf8fd287

See more details on using hashes here.

File details

Details for the file polars_h3-0.6.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_h3-0.6.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a0cf9ca3e5128f1d302a3169bf0c1f376b7af8b290c314bb6df7452dee8f1292
MD5 1da0d6bc7946103b9438cba2ef9e1d10
BLAKE2b-256 4c9f16716427c4dd94f99eb5934da073b6bb1a7fcc09d129c786215c30d2a1a7

See more details on using hashes here.

File details

Details for the file polars_h3-0.6.4-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for polars_h3-0.6.4-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 774cafdde4a850f7afe266a000afd776f12d535b94088952bdb7bca79c47873b
MD5 c34e0f03422ba2057494fb380d8bb07b
BLAKE2b-256 b991b2840b83248ecd7c54fa54c79f6cee4e334f5d84bf753b2fda8cded845d4

See more details on using hashes here.

File details

Details for the file polars_h3-0.6.4-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_h3-0.6.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8c77164dd8d7ba13ddf3ffb6c064b9caabab551c075d62567c145c08780dd3ea
MD5 4c426136a8aed948ab75708d81abd906
BLAKE2b-256 4520d2030af2d5e0c10e3c29f931cf646ac8058dbeb92aa47b57f76be5fb8db3

See more details on using hashes here.

File details

Details for the file polars_h3-0.6.4-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_h3-0.6.4-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e77ef02f77b3e70619db7bf6c6b55aa2a0b79d8463264199e20339264a7f3849
MD5 51d460692512ed3f4b5b21633a12592c
BLAKE2b-256 cbf7c23a7c557db75618bce8da842d1afe47814aba35b022a93ccda5fbe81cd7

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.0

7 files

This release

0.6.4 This release

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