leaf_engine.utils.h3

This module contains various pure functions for manipulating H3 indices.

Attributes

H3_POLYFILL_BUFFER

H3_RESOLUTION

Functions

geometry_to_h3_indices(→ Iterable)

get_contiguous_areas(→ list[set])

Given a set of H3 indices, returns a list of H3 indices representing

get_k_ring(→ set)

Returns the k-ring of a given H3 index (i.e., the k-"disk"). Optionally,

h3_index_to_geometry(→ Union[str, ...)

h3_index_to_lat_lon(→ Tuple[float, float])

h3_indices_to_geometry(→ Union[str, ...)

Converts a set of H3 indices to shapely Polygon or WKT. Raises an exception

is_contiguous_area(→ bool)

Returns whether area defined by a set of H3 indices is contiguous.

lat_lon_to_geometry(→ str)

lat_lon_to_h3_index(→ str)

polygon_to_h3_indices(polygon[, resolution])

Module Contents

leaf_engine.utils.h3.geometry_to_h3_indices(geometry: str | shapely.geometry.Polygon, resolution: int = H3_RESOLUTION) Iterable
Parameters:
  • geometry (Union[str, shapely.geometry.Polygon]) –

  • resolution (int) –

Return type:

Iterable

leaf_engine.utils.h3.get_contiguous_areas(indices: set) list[set]

Given a set of H3 indices, returns a list of H3 indices representing contiguous areas in the provided list of indices.

Parameters:
  • indices (set) – Set of H3 indices at arbitrary resolution. All indices must be at

  • resolution. (same) –

Returns:

List of sets of H3 indices, each set representing a contiguous area in the input. Sets are sorted by number of indices (ascending).

Return type:

list

leaf_engine.utils.h3.get_k_ring(index: str, k: int, exclude_k: int | None = None) set

Returns the k-ring of a given H3 index (i.e., the k-“disk”). Optionally, allows the specification of an internal k-ring to exclude from the return set (which will return a k-“doughnut” instead of a k-“disk”).

Parameters:
  • index (str) – H3 index of arbitrary resolution.

  • k (int) – k-ring to return.

  • exclude_k (int, optional) – Internal k-ring to exclude. Defaults to None.

Returns:

H3 indices comprising the k-ring.

Return type:

set

leaf_engine.utils.h3.h3_index_to_geometry(h3_index: str, as_string=False) str | shapely.geometry.Polygon
Parameters:

h3_index (str) –

Return type:

Union[str, shapely.geometry.Polygon]

leaf_engine.utils.h3.h3_index_to_lat_lon(h3_index: str) Tuple[float, float]
Parameters:

h3_index (str) –

Return type:

Tuple[float, float]

leaf_engine.utils.h3.h3_indices_to_geometry(indices: Iterable, as_string: bool = False) str | shapely.geometry.Polygon

Converts a set of H3 indices to shapely Polygon or WKT. Raises an exception if the conversion produces a MultiPolygon instead of contiguous Polygon.

Parameters:
  • indices (Iterable) – Set of H3 indices.

  • as_string (bool, optional) – Return WKT instead of Polygon object. Defaults to False.

Returns:

Polygon or WKT.

Return type:

Union[str, Polygon]

leaf_engine.utils.h3.is_contiguous_area(indices: set) bool

Returns whether area defined by a set of H3 indices is contiguous.

Parameters:

indices (set) –

Return type:

bool

leaf_engine.utils.h3.lat_lon_to_geometry(lat: float, lon: float) str
Parameters:
Return type:

str

leaf_engine.utils.h3.lat_lon_to_h3_index(lat: float, lon: float, resolution=H3_RESOLUTION) str
Parameters:
Return type:

str

leaf_engine.utils.h3.polygon_to_h3_indices(polygon: shapely.geometry.Polygon, resolution=H3_RESOLUTION)
Parameters:

polygon (shapely.geometry.Polygon) –

leaf_engine.utils.h3.H3_POLYFILL_BUFFER
leaf_engine.utils.h3.H3_RESOLUTION = 5