pub struct CellIndex(/* private fields */);
Expand description
Represents a cell (hexagon or pentagon) in the H3 grid system at a particular resolution.
The index is encoded on 64-bit with the following bit layout:
┏━┳━━━┳━━━━┳━━━━┳━━━━━━━┳━━━┳━━━┳━┈┈┈┈┈┈┈┈━┳━━━┳━━━┓
┃U┃ M ┃ U ┃ R ┃ B ┃C₀ ┃C₁ ┃ ┃C₁₄┃C₁₅┃
┗━┻━━━┻━━━━┻━━━━┻━━━━━━━┻━━━┻━━━┻━┈┈┈┈┈┈┈┈━┻━━━┻━━━┛
64 63 59 56 52 45 42 39 6 3 0
Where:
U
are unused reserved bit, always set to 0 (bit 63 and bits 56-58).M
is the index mode, always set to 1, coded on 4 bits (59-62).R
is the cell resolution, in [0; 15], coded on 4 bits (52-55).B
is the base cell, in [0; 121], coded on 7 bits (45-51)C
are cells, coded on 3 bits each, with either a value in [0; 6] or the pattern0b111
if unused.
References:
Implementations§
source§impl CellIndex
impl CellIndex
sourcepub const fn resolution(self) -> Resolution
pub const fn resolution(self) -> Resolution
Returns the resolution of the index.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.resolution(), h3o::Resolution::Ten);
sourcepub const fn base_cell(self) -> BaseCell
pub const fn base_cell(self) -> BaseCell
Returns the base cell of the index.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.base_cell(), h3o::BaseCell::try_from(15)?);
sourcepub fn area_rads2(self) -> f64
pub fn area_rads2(self) -> f64
Computes the area of this H3 cell, in radians².
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.area_rads2(), 3.3032558516982826e-10);
sourcepub fn area_km2(self) -> f64
pub fn area_km2(self) -> f64
Computes the area of this H3 cell, in km².
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.area_km2(), 0.013407827139722947);
sourcepub fn area_m2(self) -> f64
pub fn area_m2(self) -> f64
Computes the area of this H3 cell, in m².
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.area_m2(), 13407.827139722947);
sourcepub fn icosahedron_faces(self) -> FaceSet
pub fn icosahedron_faces(self) -> FaceSet
Finds all icosahedron faces intersected this cell index
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let faces = index.icosahedron_faces().iter().collect::<Vec<_>>();
sourcepub fn is_pentagon(self) -> bool
pub fn is_pentagon(self) -> bool
Returns true if this index represents a pentagonal cell.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert!(!index.is_pentagon());
sourcepub fn max_face_count(self) -> usize
pub fn max_face_count(self) -> usize
Returns the maximum number of icosahedron faces the index may intersect.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.max_face_count(), 2);
sourcepub fn direction_at(self, resolution: Resolution) -> Option<Direction>
pub fn direction_at(self, resolution: Resolution) -> Option<Direction>
Returns the cell at the given resolution in the index, if any.
§Example
use h3o::{CellIndex, Direction, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.direction_at(Resolution::Five), Some(Direction::K));
sourcepub fn parent(self, resolution: Resolution) -> Option<Self>
pub fn parent(self, resolution: Resolution) -> Option<Self>
Returns the parent, at the specified resolution, of the cell.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(
index.parent(Resolution::Five),
CellIndex::try_from(0x851fb467fffffff).ok()
);
sourcepub fn center_child(self, resolution: Resolution) -> Option<Self>
pub fn center_child(self, resolution: Resolution) -> Option<Self>
Returns the center child index at the specified resolution.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(
index.center_child(Resolution::Fifteen),
CellIndex::try_from(0x8f1fb46622d8000).ok()
);
sourcepub fn children_count(self, resolution: Resolution) -> u64
pub fn children_count(self, resolution: Resolution) -> u64
Returns the exact number of children for a cell at a given resolution.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.children_count(Resolution::Fifteen), 16_807);
sourcepub fn child_position(self, resolution: Resolution) -> Option<u64>
pub fn child_position(self, resolution: Resolution) -> Option<u64>
Returns the position of the cell within an ordered list of all children of the cell’s parent at the specified resolution.
Returns None
if the cell’s resolution is coarser than the given
resolution.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(index.child_position(Resolution::Eight), Some(24));
assert_eq!(index.child_position(Resolution::Twelve), None);
sourcepub fn child_at(self, position: u64, resolution: Resolution) -> Option<Self>
pub fn child_at(self, position: u64, resolution: Resolution) -> Option<Self>
Returns the child cell at a given position within an ordered list of all children at the specified resolution.
Returns None
if no child can be found (coarser resolution, index out
of bound, …).
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x881fb46623fffff)?;
assert_eq!(
index.child_at(24, Resolution::Ten),
CellIndex::try_from(0x8a1fb46622dffff).ok(),
);
assert_eq!(index.child_at(24, Resolution::Five), None);
sourcepub fn children(self, resolution: Resolution) -> impl Iterator<Item = Self>
pub fn children(self, resolution: Resolution) -> impl Iterator<Item = Self>
Return the children, at the specified resolution, of the cell index.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
let children = index.children(Resolution::Eleven).collect::<Vec<_>>();
sourcepub fn compact(
indexes: impl IntoIterator<Item = Self>
) -> Result<impl Iterator<Item = Self>, CompactionError>
pub fn compact( indexes: impl IntoIterator<Item = Self> ) -> Result<impl Iterator<Item = Self>, CompactionError>
Compresses a set of unique cell indexes all at the same resolution.
The indexes are compressed by pruning full child branches to the parent level. This is also done for all parents recursively to get the minimum number of hex addresses that perfectly cover the defined space.
§Errors
All cell indexes must be unique and have the same resolution, otherwise
an CompactionError
is returned.
§Example
use h3o::CellIndex;
let cells = [
0x081003ffffffffff,
0x081023ffffffffff,
0x081043ffffffffff,
0x081063ffffffffff,
0x081083ffffffffff,
0x0810a3ffffffffff,
0x0810c3ffffffffff,
]
.into_iter()
.map(|hex| CellIndex::try_from(hex))
.collect::<Result<Vec<_>, _>>()?;
let compacted_cells = CellIndex::compact(cells)?.collect::<Vec<_>>();
sourcepub fn uncompact_size(
compacted: impl IntoIterator<Item = Self>,
resolution: Resolution
) -> u64
pub fn uncompact_size( compacted: impl IntoIterator<Item = Self>, resolution: Resolution ) -> u64
Computes the exact size of the uncompacted set of cells.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
let size = CellIndex::uncompact_size(std::iter::once(index), Resolution::Eleven);
sourcepub fn uncompact(
compacted: impl IntoIterator<Item = Self>,
resolution: Resolution
) -> impl Iterator<Item = Self>
pub fn uncompact( compacted: impl IntoIterator<Item = Self>, resolution: Resolution ) -> impl Iterator<Item = Self>
Expands a compressed set of cells into a set of cells of the specified resolution.
§Example
use h3o::{CellIndex, Resolution};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
let cells = CellIndex::uncompact(
std::iter::once(index), Resolution::Eleven
).collect::<Vec<_>>();
sourcepub fn boundary(self) -> Boundary
pub fn boundary(self) -> Boundary
Computes the cell boundary, in spherical coordinates, of this index.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let boundary = index.boundary().iter().collect::<Vec<_>>();
sourcepub fn base_cells() -> impl Iterator<Item = Self>
pub fn base_cells() -> impl Iterator<Item = Self>
Returns all the base cell indexes.
§Example
let cells = h3o::CellIndex::base_cells().collect::<Vec<_>>();
sourcepub fn edge(self, destination: Self) -> Option<DirectedEdgeIndex>
pub fn edge(self, destination: Self) -> Option<DirectedEdgeIndex>
Returns the edge between the current cell and the specified destination.
§Example
use h3o::{CellIndex, Direction, DirectedEdgeIndex};
let src = CellIndex::try_from(0x8a1fb46622dffff)?;
let dst = CellIndex::try_from(0x8a1fb46622d7fff)?;
assert_eq!(src.edge(dst), DirectedEdgeIndex::try_from(0x16a1fb46622dffff).ok());
// Not a neighbor, thus no shared edge.
let dst = CellIndex::try_from(0x8a1fb4644937fff)?;
assert!(src.edge(dst).is_none());
sourcepub fn edges(self) -> impl Iterator<Item = DirectedEdgeIndex>
pub fn edges(self) -> impl Iterator<Item = DirectedEdgeIndex>
Returns all of the directed edges from the current index.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let edges = index.edges().collect::<Vec<_>>();
sourcepub fn vertex(self, vertex: Vertex) -> Option<VertexIndex>
pub fn vertex(self, vertex: Vertex) -> Option<VertexIndex>
Get the specified vertex of this cell.
§Example
use h3o::{CellIndex, Vertex, VertexIndex};
let index = CellIndex::try_from(0x8a1fb46622dffff)?;
assert_eq!(
index.vertex(Vertex::try_from(3)?),
VertexIndex::try_from(0x25a1fb464492ffff).ok()
);
sourcepub fn vertexes(self) -> impl Iterator<Item = VertexIndex>
pub fn vertexes(self) -> impl Iterator<Item = VertexIndex>
Returns all vertexes for the current cell.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let vertexes = index.vertexes().collect::<Vec<_>>();
sourcepub fn grid_disk<T>(self, k: u32) -> Twhere
T: FromIterator<Self>,
pub fn grid_disk<T>(self, k: u32) -> Twhere
T: FromIterator<Self>,
Produce cells within grid distance k
of the cell.
This function is a convenience helper that tries
Self::grid_disk_fast
first and then fallback on
Self::grid_disk_safe
if the former fails.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells = index.grid_disk::<Vec<_>>(2);
sourcepub fn grid_disk_safe(self, k: u32) -> impl Iterator<Item = Self>
pub fn grid_disk_safe(self, k: u32) -> impl Iterator<Item = Self>
Safe but slow version of Self::grid_disk_fast
.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells = index.grid_disk_safe(2).collect::<Vec<_>>();
sourcepub fn grid_disk_fast(self, k: u32) -> impl Iterator<Item = Option<Self>>
pub fn grid_disk_fast(self, k: u32) -> impl Iterator<Item = Option<Self>>
Produces indexes within grid distance k
of the cell.
0-ring
is defined as the current cell, 1-ring
is defined as
0-ring
and all neighboring indexes, and so on.
This function fails (i.e. returns a None item) when a pentagon (or a pentagon distortion) is encountered. When this happen, the previously returned cells should be treated as invalid and discarded.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells = index.grid_disk_fast(2).collect::<Option<Vec<_>>>()
.unwrap_or_default();
sourcepub fn grid_disk_distances<T>(self, k: u32) -> Twhere
T: FromIterator<(Self, u32)>,
pub fn grid_disk_distances<T>(self, k: u32) -> Twhere
T: FromIterator<(Self, u32)>,
Produce cells and their distances from the current cell, up to distance
k
.
This function is a convenience helper that tries
Self::grid_disk_distances_fast
first and then fallback on
Self::grid_disk_distances_safe
if the former fails.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells_and_dists = index.grid_disk_distances::<Vec<_>>(2);
sourcepub fn grid_disk_distances_safe(
self,
k: u32
) -> impl Iterator<Item = (Self, u32)>
pub fn grid_disk_distances_safe( self, k: u32 ) -> impl Iterator<Item = (Self, u32)>
Safe but slow version of Self::grid_disk_distances_fast
.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells_and_dists = index.grid_disk_distances_safe(2).collect::<Vec<_>>();
sourcepub fn grid_disk_distances_fast(
self,
k: u32
) -> impl Iterator<Item = Option<(Self, u32)>>
pub fn grid_disk_distances_fast( self, k: u32 ) -> impl Iterator<Item = Option<(Self, u32)>>
Produce cells and their distances from the current cell, up to distance
k
.
0-ring
is defined as the current cell, 1-ring
is defined as 0-ring
and all neighboring indexes, and so on.
This function fails (i.e. returns a None item) when a pentagon (or a pentagon distortion) is encountered. When this happen, the previously returned items should be treated as invalid.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells_and_dists = index.grid_disk_distances_fast(2)
.collect::<Option<Vec<_>>>()
.unwrap_or_default();
sourcepub fn grid_disks_fast(
indexes: impl IntoIterator<Item = Self>,
k: u32
) -> impl Iterator<Item = Option<Self>>
pub fn grid_disks_fast( indexes: impl IntoIterator<Item = Self>, k: u32 ) -> impl Iterator<Item = Option<Self>>
Takes an list of cell indexes and a max k-ring
and returns a stream of
cell indexes sorted first by the original cell index and then by the
grid k-ring
(0 to max).
This function fails (i.e. returns a None item) when a pentagon (or a pentagon distortion) is encountered. When this happen, the previously returned cells should be treated as invalid and discarded.
§Example
use h3o::CellIndex;
let indexes = vec![
CellIndex::try_from(0x8a1fb46622dffff)?,
CellIndex::try_from(0x8a1fb46622d7fff)?,
];
let cells = CellIndex::grid_disks_fast(indexes, 2)
.collect::<Option<Vec<_>>>()
.unwrap_or_default();
sourcepub fn grid_ring_fast(self, k: u32) -> impl Iterator<Item = Option<Self>>
pub fn grid_ring_fast(self, k: u32) -> impl Iterator<Item = Option<Self>>
Returns the “hollow” ring of hexagons at exactly grid distance k
from
the current cell.
In particular, k=0 returns just the current hexagon.
This function fails (i.e. returns a None item) when a pentagon (or a pentagon distortion) is encountered. When this happen, the previously returned cells should be treated as invalid and discarded.
Failure cases may be fixed in future versions.
§Example
let index = h3o::CellIndex::try_from(0x8a1fb46622dffff)?;
let cells = index.grid_ring_fast(2).collect::<Option<Vec<_>>>()
.unwrap_or_default();
sourcepub fn grid_distance(self, to: Self) -> Result<i32, LocalIjError>
pub fn grid_distance(self, to: Self) -> Result<i32, LocalIjError>
Produces the grid distance between the two indexes.
§Errors
This function may fail to find the distance between two indexes, for
example if they are very far apart. It may also fail when finding
distances for indexes on opposite sides of a pentagon.
In such case, LocalIjError::Pentagon
or LocalIjError::HexGrid
is
returned.
LocalIjError::ResolutionMismatch
if the source and destination
indexes don’t have the same resolution.
§Example
use h3o::CellIndex;
let src = CellIndex::try_from(0x8a1fb46622dffff)?;
let dst = CellIndex::try_from(0x8a1fb46622d7fff)?;
assert_eq!(src.grid_distance(dst)?, 1);
sourcepub fn grid_path_cells_size(self, to: Self) -> Result<i32, LocalIjError>
pub fn grid_path_cells_size(self, to: Self) -> Result<i32, LocalIjError>
Computes the number of indexes in a line from the current index to the end one.
To be used for pre-allocating memory for CellIndex::grid_path_cells
.
§Errors
See Self::grid_distance
.
§Example
use h3o::CellIndex;
let src = CellIndex::try_from(0x8a1fb46622dffff)?;
let dst = CellIndex::try_from(0x8a1fb46622d7fff)?;
assert_eq!(src.grid_path_cells_size(dst)?, 2);
sourcepub fn grid_path_cells(
self,
to: Self
) -> Result<impl Iterator<Item = Result<Self, LocalIjError>>, LocalIjError>
pub fn grid_path_cells( self, to: Self ) -> Result<impl Iterator<Item = Result<Self, LocalIjError>>, LocalIjError>
Given two H3 indexes, return the line of indexes between them (inclusive).
§Notes
- The specific output of this function should not be considered stable
across library versions. The only guarantees the library provides are
that the line length will be
start.grid_distance(end) + 1
and that every index in the line will be a neighbor of the preceding index. - Lines are drawn in grid space, and may not correspond exactly to either Cartesian lines or great arcs.
§Errors
This function may fail to find the distance between two indexes, for
example if they are very far apart. It may also fail when finding
distances for indexes on opposite sides of a pentagon.
In such case, LocalIjError::Pentagon
or LocalIjError::HexGrid
is
returned.
LocalIjError::ResolutionMismatch
if the source and destination
indexes don’t have the same resolution.
§Example
use h3o::CellIndex;
let src = CellIndex::try_from(0x8a1fb46622dffff)?;
let dst = CellIndex::try_from(0x8a1fb46622d7fff)?;
let cells = src.grid_path_cells(dst)?.collect::<Result<Vec<_>, _>>()?;
sourcepub fn is_neighbor_with(self, index: Self) -> Result<bool, ResolutionMismatch>
pub fn is_neighbor_with(self, index: Self) -> Result<bool, ResolutionMismatch>
Returns whether or not the provided cell index is a neighbor of the current one.
§Errors
ResolutionMismatch
if the two indexes don’t have the
same resolution.
§Example
use h3o::CellIndex;
let src = CellIndex::try_from(0x8a1fb46622dffff)?;
let dst = CellIndex::try_from(0x8a1fb46622d7fff)?;
assert!(src.is_neighbor_with(dst)?);
let dst = CellIndex::try_from(0x8a1fb4644937fff)?;
assert!(!src.is_neighbor_with(dst)?);
sourcepub fn to_local_ij(self, origin: Self) -> Result<LocalIJ, LocalIjError>
pub fn to_local_ij(self, origin: Self) -> Result<LocalIJ, LocalIjError>
Produces IJ
coordinates for an index anchored by an origin.
The coordinate space used by this function may have deleted regions or warping due to pentagonal distortion.
Coordinates are only comparable if they come from the same origin index.
This function’s output is not guaranteed to be compatible across different versions of H3.
§Arguments
origin
- An anchoring index for theIJ
coordinate system.index
- Index to find the coordinates of.
§Errors
LocalIjError::ResolutionMismatch
if the index and the origin don’t
have the same resolution.
Failure may occur if the index is too far away from the origin or if the
index is on the other side of a pentagon.
In such case, LocalIjError::Pentagon
or LocalIjError::HexGrid
is
returned.
§Example
use h3o::CellIndex;
let anchor = CellIndex::try_from(0x823147fffffffff)?;
let index = CellIndex::try_from(0x8230e7fffffffff)?;
let localij = index.to_local_ij(anchor)?;
sourcepub fn succ(self) -> Option<Self>
pub fn succ(self) -> Option<Self>
Returns the next cell, in term of ordering.
Returns None
if self
is the last cell at this resolution.
§Example
use h3o::CellIndex;
let start = CellIndex::try_from(0x823147fffffffff)?;
let after = start.succ().expect("next cell index");
sourcepub fn pred(self) -> Option<Self>
pub fn pred(self) -> Option<Self>
Returns the previous cell, in term of ordering.
Returns None
if self
is the frist cell at this resolution.
§Example
use h3o::CellIndex;
let start = CellIndex::try_from(0x823147fffffffff)?;
let before = start.pred().expect("next cell index");
sourcepub fn first(resolution: Resolution) -> Self
pub fn first(resolution: Resolution) -> Self
The first cell index at the given resolution.
§Example
use h3o::{CellIndex, Resolution};
let first = CellIndex::first(Resolution::Nine);
sourcepub fn last(resolution: Resolution) -> Self
pub fn last(resolution: Resolution) -> Self
The last cell index at the given resolution.
§Example
use h3o::{CellIndex, Resolution};
let last = CellIndex::last(Resolution::Nine);
Trait Implementations§
source§impl<'a> Arbitrary<'a> for CellIndex
impl<'a> Arbitrary<'a> for CellIndex
source§fn arbitrary(data: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(data: &mut Unstructured<'a>) -> Result<Self>
Self
from the given unstructured data. Read moresource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self
from the entirety of the given
unstructured data. Read moresource§impl<'de> Deserialize<'de> for CellIndex
impl<'de> Deserialize<'de> for CellIndex
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Ord for CellIndex
impl Ord for CellIndex
source§impl PartialEq for CellIndex
impl PartialEq for CellIndex
source§impl PartialOrd for CellIndex
impl PartialOrd for CellIndex
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more