| Crates.io | xs_h3 |
| lib.rs | xs_h3 |
| version | 0.1.0 |
| created_at | 2025-05-22 04:00:58.710688+00 |
| updated_at | 2025-05-22 04:00:58.710688+00 |
| description | Excerion Sun's Rust implementation of Uber's H3 geospatial indexing library |
| homepage | |
| repository | https://github.com/excsn/xs_h3 |
| max_upload_size | |
| id | 1684738 |
| size | 721,804 |
XS-H3 is Excerion Sun's pure Rust implementation of Uber's H3, a discrete global grid system. It provides a set of functions for converting between geographic coordinates (latitude/longitude) and H3 cell indexes, as well as for performing grid traversal, hierarchical operations, and region-based analysis.
This library is designed with a primary focus on high performance and memory safety, leveraging the strengths of Rust. It aims to serve as a robust and efficient alternative or complement to existing H3 libraries by closely following the C H3 library's API and behavior where appropriate.
The "xs" in xs-h3 can be thought of as "Cross-Safe" or "Extra Safe," highlighting the emphasis on Rust's safety guarantees.
⚠️ Experimental Status ⚠️
xs-h3 (version 0.1.0) is currently in an early, experimental stage of development.
Use in production environments is NOT recommended at this stage. We encourage experimentation, feedback, and contributions to help stabilize and complete the library.
H3 is a geospatial indexing system that partitions the world into hexagonal cells across multiple resolutions. This hexagonal grid offers several advantages for spatial analysis, including more uniform adjacency and distance calculations compared to traditional square grids.
Key features of the H3 system include:
XS-H3 aims to provide a comprehensive implementation of the H3 specification. Current capabilities include:
lat_lng_to_cell). (Largely implemented)cell_to_lat_lng). (Implemented)cell_to_boundary). (Implemented)is_valid_cell). (Implemented)is_pentagon) or Class III (is_res_class_iii). (Implemented)grid_disk, grid_disk_distances). (Safe BFS version implemented)grid_ring_unsafe). (Implemented, unsafe due to pentagons)grid_path_cells). (Implemented, some path deviations from C noted)grid_distance). (Implemented, some distance deviations from C noted for specific pairs due to underlying local IJK differences)are_neighbor_cells). (Implemented)cell_to_parent, cell_to_children, cell_to_center_child). (Implemented)cell_to_child_pos, child_pos_to_cell). (Implemented)compact_cells). (Basic implementation)uncompact_cells). (Implemented)polygon_to_cells). (Experimental, basic implementation)max_polygon_to_cells_size). (Experimental)cells_to_multi_polygon). (Experimental, hole handling and full normalization are basic)cell_area_km2, etc.). (Implemented)serde feature. (Implemented for core types)Planned / In Progress for future versions:
cells_to_multi_polygon (including full GeoJSON-compliant hole handling and winding order normalization).H3Error variants.The primary engineering goal of XS-H3 is to achieve high performance, while providing Rust's strong memory safety guarantees. This is pursued through:
Benchmarks against reference implementations are planned as a key part of the development and validation process.
H3 (and therefore XS-H3) is suitable for a wide range of geospatial applications, including:
Add xs-h3 to your Cargo.toml:
[dependencies]
xs-h3 = "0.1.0"
If you need Serde support for H3Index, LatLng, etc.:
[dependencies]
xs-h3 = { version = "0.1.0", features = ["serde"] }
Given the experimental nature of version 0.1.0, please lock to this specific version if you decide to try it, as future 0.1.x versions may introduce breaking API changes.
use xs_h3::{lat_lng_to_cell, cell_to_lat_lng, get_resolution, H3Index, LatLng, degs_to_rads, rads_to_degs, H3Error};
fn main() -> Result<(), H3Error> {
let sf_lat_deg = 37.7749;
let sf_lng_deg = -122.4194;
let geo_point = LatLng {
lat: degs_to_rads(sf_lat_deg),
lng: degs_to_rads(sf_lng_deg),
};
let resolution = 9;
let h3_cell: H3Index = lat_lng_to_cell(&geo_point, resolution)?;
println!("H3 Cell for ({}, {}) at res {}: {:x}", sf_lat_deg, sf_lng_deg, resolution, h3_cell.0);
let cell_res = get_resolution(h3_cell);
println!("Resolution of cell {:x}: {}", h3_cell.0, cell_res);
let center_coords_rad: LatLng = cell_to_lat_lng(h3_cell)?;
println!(
"Center of cell {:x}: (lat: {:.6}, lng: {:.6}) degrees",
h3_cell.0,
rads_to_degs(center_coords_rad.lat),
rads_to_degs(center_coords_rad.lng)
);
Ok(())
}
(For more examples, please see the examples/, tests/ and benches/ directories in the repository.)
Contributions are highly welcome, especially given the library's experimental state! Please feel free to open an issue to discuss potential changes, report bugs, or submit a pull request for fixes or new features. When contributing, please try to:
cargo fmt and cargo clippy).XS-H3 is licensed under the Mozilla Public License Version 2.0 (MPL-2.0). See the LICENSE for details.
This library is an implementation of the H3 global grid system, originally developed by Uber Technologies, Inc. We are grateful for their innovative and great pioneering work and for making the H3 specification and reference implementation publicly available for free.