Skip to main content

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

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.

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 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 Provides the position of the child cell within an ordered list of all children of the cell's parent at the specified resolution parentRes. โœ…
child_pos_to_cell Provides the child cell at a given position within an ordered list of all children of parent at the specified resolution childRes. โœ…
compact_cells Compacts a collection of H3 cells by recursively replacing children cells with their parents if all children are present. Input cells must all share the same resolution. โœ…
uncompact_cells Uncompacts the set compactedSet of indexes to the resolution res. h3Set must be at least of size uncompactCellsSize(compactedSet, numHexes, 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 which are at most grid distance k from the origin cell. Output order is not guaranteed. โœ…
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 cell boundary lat / longs โœ…
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 โœ…
vertex_to_latlng Convert a vertex ID to latitude/longitude coordinate โœ…
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 are directly adjacent โœ…
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 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)

CleanShot 2024-12-08 at 00 26 22

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


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

Uploaded CPython 3.8+Windows x86-64

polars_h3-0.4.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

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

polars_h3-0.4.3-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (4.4 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.12+ i686

polars_h3-0.4.3-cp38-abi3-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

polars_h3-0.4.3-cp38-abi3-macosx_10_12_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for polars_h3-0.4.3.tar.gz
Algorithm Hash digest
SHA256 c71ea58eb6c2f1b80bb4692f5b773634383a97203c8ab3c4c6c4c92759ad3721
MD5 99aa0a3e02637c346fc01773334a9cfb
BLAKE2b-256 960969f44afe25f29aab45b4248648b8238378b71f79bc9c8cfa25709368a62f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: polars_h3-0.4.3-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.7.8

File hashes

Hashes for polars_h3-0.4.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7ff36e2a5c9ce3750c456a9fa9552962ddd9113c483967480e5b4e677d6e159c
MD5 01b54f8e734e8cf33bac82bdfa5c79d4
BLAKE2b-256 7c86eef08c03778cb8a8d3a9eb687c885735b97c85069d44260b8388aa79dda0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polars_h3-0.4.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 52b0b36bb4cea7cc661bbc69f97c090abee6426adecb523bce01510a49a3bb59
MD5 1d707b6728b0364d4f59c6df6a316208
BLAKE2b-256 18b352901afbd26c3ad001d5e28c290f6921e4e059a23567f799da0a1458a58a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polars_h3-0.4.3-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 1112174760d2045ff3ba8b2835d493e346eea6e8cb6b7e51432aabe55f64f426
MD5 628cda49368ae73b2a09d682a74e77a6
BLAKE2b-256 c5eb7019a0f4e65f0399a69e41e1804cb4e955d937df87cd7c3db579fa989cb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polars_h3-0.4.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6ccbf2cc57cb44c8f7e9bb818477cd087dc6e13437c6297302c069d5d071201c
MD5 ddb0ee5c43d348acd3164a0aa729043d
BLAKE2b-256 57e7730eb45e37e5572d55241628c4d7c6fc8479bcc4782f1acbccb172ddf8e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polars_h3-0.4.3-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 09301d1253b4bf413f2b65615ffbc4daea47e99edf2a36fc34e65e24804ea2fc
MD5 f2550f29db94e2128d5158afda4dc44f
BLAKE2b-256 cb5188029a56d3733122a5c849c9142c40bd24e9659e2623e14a87af13a5968b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page