H3.API
H3.API.H3ErrorCodeH3.API.areNeighborCellsH3.API.cellAreaKm2H3.API.cellAreaM2H3.API.cellAreaRads2H3.API.cellToBoundaryH3.API.cellToChildrenH3.API.cellToChildrenSizeH3.API.cellToLatLngH3.API.cellToLocalIjH3.API.cellToLocalIjkH3.API.cellToParentH3.API.cellToVertexH3.API.cellToVertexesH3.API.cellsToDirectedEdgeH3.API.compactCellsH3.API.describeH3ErrorH3.API.directedEdgeToBoundaryH3.API.directedEdgeToCellsH3.API.edgeLengthKmH3.API.edgeLengthMH3.API.edgeLengthRadsH3.API.faceIjkToH3H3.API.geoToFaceIjkH3.API.geoToVec3dH3.API.getBaseCellNumberH3.API.getDirectedEdgeDestinationH3.API.getDirectedEdgeOriginH3.API.getNumCellsH3.API.getNumVertexesH3.API.getRes0CellsH3.API.getResolutionH3.API.gridDiskH3.API.gridDiskDistancesH3.API.gridDiskDistancesUnsafeH3.API.gridDiskUnsafeH3.API.gridDisksUnsafeH3.API.gridDistanceH3.API.gridPathCellsH3.API.gridPathCellsSizeH3.API.gridRingUnsafeH3.API.h3ToFaceIjkH3.API.h3ToStringH3.API.hex2dToCoordIJKH3.API.ijToIjkH3.API.ijkDistanceH3.API.ijkNormalizeH3.API.ijkToHex2dH3.API.ijkToIjH3.API.isPentagonH3.API.isResClassIIIH3.API.isValidCellH3.API.isValidDirectedEdgeH3.API.isValidVertexH3.API.latLngToCellH3.API.localIjToCellH3.API.localIjkToCellH3.API.maxGridDiskSizeH3.API.originToDirectedEdgesH3.API.res0CellCountH3.API.stringToH3H3.API.uncompactCellsH3.API.uncompactCellsSizeH3.API.vertexToLatLng
Error handling
H3.API.H3ErrorCode — Type
struct H3ErrorCode
value::H3Error
endThe type returned by most H3 functions is H3Error, a 32 bit integer type with the following properties:
H3Errorwill be an integer type of 32 bits, i.e.uint32_t.H3Errorwith value 0 indicates success (no error).- No
H3Errorvalue will set the most significant bit. - As a result of these properties, no
H3Errorvalue will set the bits that correspond with the Mode bit field in anH3Index.
Table of error codes https://h3geo.org/docs/library/errors/#table-of-error-codes
H3.API.describeH3Error — Function
describeH3Error(err::H3Error)::String
describeH3Error(code::H3ErrorCode)::String
describeH3Error(enum::Lib.H3ErrorCodes)::Stringconverts the provided H3Error value into a description string
Indexing functions
H3.API.latLngToCell — Function
latLngToCell(g::LatLng, res::Int)::Union{H3ErrorCode, H3Index}find the H3 index of the resolution res cell containing the lat/lng
H3.API.cellToLatLng — Function
cellToLatLng(h::H3Index)::Union{H3ErrorCode, LatLng}find the lat/lng center point g of the cell h3
H3.API.cellToBoundary — Function
cellToBoundary(h::H3Index)::Union{H3ErrorCode, Vector{LatLng}}Determines the cell boundary in spherical coordinates for an H3 index.
@param h3 The H3 index. @param cb The boundary of the H3 cell in spherical coordinates.
Index inspection functions
H3.API.getResolution — Function
getResolution(h::H3Index)::Cintreturns the resolution of the provided H3 index Works on both cells and directed edges.
H3.API.getBaseCellNumber — Function
getBaseCellNumber(h::H3Index)::Cintreturns the base cell "number" (0 to 121) of the provided H3 cell
Note: Technically works on H3 edges, but will return base cell of the origin cell.
H3.API.stringToH3 — Function
stringToH3(str::String)::Union{H3ErrorCode, H3Index}Converts the string representation to H3Index (UInt64) representation.
H3.API.h3ToString — Function
h3ToString(h::H3Index)::StringConverts the H3Index representation of the index to the string representation.
H3.API.isValidCell — Function
isValidCell(h::H3Index)::Boolconfirms if an H3Index is a valid cell (hexagon or pentagon) In particular, returns 0 (False) for H3 directed edges or invalid data
H3.API.isResClassIII — Function
isResClassIII(h::H3Index)::Booldetermines if a hexagon is Class III (or Class II)
H3.API.isPentagon — Function
isPentagon(h::H3Index)::Booldetermines if an H3 cell is a pentagon
Grid traversal functions
H3.API.gridDisk — Function
gridDisk(origin::H3Index, k::Int)::Union{H3ErrorCode, Vector{H3Index}}Produce cells within grid distance k of the origin cell.
k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and all neighboring cells, and so on.
Output is placed in the provided array in no particular order. Elements of the output array may be left zero, as can happen when crossing a pentagon.
@param origin origin cell @param k k >= 0 @param out zero-filled array which must be of size maxGridDiskSize(k)
H3.API.maxGridDiskSize — Function
maxGridDiskSize(k::Int)::Union{H3ErrorCode, Int64}Maximum number of cells that result from the gridDisk algorithm with the given k. Formula source and proof: https://oeis.org/A003215
@param k k value, k >= 0. @param out size in indexes
H3.API.gridDiskDistances — Function
gridDiskDistances(origin::H3Index, k::Int)::Union{H3ErrorCode, NamedTuple{(:out, :distances)}}k-rings produces indices within k distance of the origin index.
H3.API.gridDiskUnsafe — Function
gridDiskUnsafe(origin::H3Index, k::Int)::Union{H3ErrorCode, Vector{H3Index}}gridDiskUnsafe produces indexes within k distance of the origin index. Output behavior is undefined when one of the indexes returned by this function is a pentagon or is in the pentagon distortion area.
k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indexes, and so on.
Output is placed in the provided array in order of increasing distance from the origin.
@param origin Origin location. @param k k >= 0 @param out Array which must be of size maxGridDiskSize(k). @return 0 if no pentagon or pentagonal distortion area was encountered.
H3.API.gridDiskDistancesUnsafe — Function
gridDiskDistancesUnsafe(origin::H3Index, k::Int)::Union{H3ErrorCode, NamedTuple{(:out, :distances)}}hexRange produces indexes within k distance of the origin index.
H3.API.gridDisksUnsafe — Function
gridDisksUnsafe(h3Set::Vector{H3Index}, k::Int)::Union{H3ErrorCode, Vector{H3Index}}gridDisksUnsafe takes an array of input hex IDs and a max k-ring and returns an array of hexagon IDs sorted first by the original hex IDs and then by the k-ring (0 to max), with no guaranteed sorting within each k-ring group.
@param h3Set A pointer to an array of H3Indexes @param length The total number of H3Indexes in h3Set @param k The number of rings to generate @param out A pointer to the output memory to dump the new set of H3Indexes to The memory block should be equal to maxGridDiskSize(k) * length @return 0 if no pentagon is encountered. Cannot trust output otherwise
H3.API.gridRingUnsafe — Function
gridRingUnsafe(origin::H3Index, k::Int)::Vector{H3Index}Returns the "hollow" ring of hexagons at exactly grid distance k from the origin hexagon. In particular, k=0 returns just the origin hexagon.
A nonzero failure code may be returned in some cases, for example, if a pentagon is encountered. Failure cases may be fixed in future versions.
@param origin Origin location. @param k k >= 0 @param out Array which must be of size 6 * k (or 1 if k == 0) @return 0 if successful; nonzero otherwise.
H3.API.gridPathCells — Function
gridPathCells(origin::H3Index, destination::H3Index)::Union{H3ErrorCode, Vector{H3Index}}Given two H3 indexes, return the line of indexes between them (inclusive).
This function may fail to find the line 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.
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
gridDistance(start, end) + 1and 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.
@param start Start index of the line @param end End index of the line @param out Output array, which must be of size gridPathCellsSize(start, end) @return 0 on success, or another value on failure.
H3.API.gridPathCellsSize — Function
gridPathCellsSize(origin::H3Index, destination::H3Index)::Union{H3ErrorCode, Int64}Number of indexes in a line from the start index to the end index, to be used for allocating memory. Returns a negative number if the line cannot be computed.
@param start Start index of the line @param end End index of the line @param size Size of the line @returns 0 on success, or another value on error
H3.API.gridDistance — Function
gridDistance(origin::H3Index, h::H3Index)::Union{H3ErrorCode, Int64}Produces the grid distance between the two indexes.
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.
@param origin Index to find the distance from. @param index Index to find the distance to. @return The distance, or a H3ErrorCode if the library could not compute the distance.
H3.API.cellToLocalIj — Function
cellToLocalIj(origin::H3Index, h::H3Index)::Union{H3ErrorCode, CoordIJ}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.
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.
This function's output is not guaranteed to be compatible across different versions of H3.
@param origin An anchoring index for the ij coordinate system. @param index Index to find the coordinates of @param mode Mode, must be 0 @param out ij coordinates of the index will be placed here on success @return 0 on success, or another value on failure.
H3.API.localIjToCell — Function
localIjToCell(origin::H3Index, ij::CoordIJ)::Union{H3ErrorCode, H3Index}Produces an index for ij coordinates anchored by an origin.
The coordinate space used by this function may have deleted regions or warping due to pentagonal distortion.
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.
This function's output is not guaranteed to be compatible across different versions of H3.
@param origin An anchoring index for the ij coordinate system. @param out ij coordinates to index. @param mode Mode, must be 0 @param index Index will be placed here on success. @return 0 on success, or another value on failure.
Hierarchical grid functions
H3.API.cellToParent — Function
cellToParent(h::H3Index, parentRes::Int)::Union{H3ErrorCode, H3Index}cellToParent produces the parent index for a given H3 index
@param h H3Index to find parent of @param parentRes The resolution to switch to (parent, grandparent, etc)
@return H3Index of the parent, or H3_NULL if you actually asked for a child
H3.API.cellToChildren — Function
cellToChildren(h::H3Index, childRes::Int)::Union{H3ErrorCode, Vector{H3Index}}provides the children (or grandchildren, etc) of the given cell
H3.API.cellToChildrenSize — Function
cellToChildrenSize(h::H3Index, childRes::Int)::Union{H3ErrorCode, Int64}determines the exact number of children (or grandchildren, etc) that would be returned for the given cell
H3.API.compactCells — Function
compactCells(h3Set::Vector{H3Index})::Union{H3ErrorCode, Vector{H3Index}}compacts the given set of hexagons as best as possible
H3.API.uncompactCells — Function
uncompactCells(compactedSet::Vector{H3Index}, res::Int)::Union{H3ErrorCode, Vector{H3Index}}uncompacts the compacted hexagon set
H3.API.uncompactCellsSize — Function
uncompactCellsSize(compactedSet::Vector{H3Index}, res::Int)::Union{H3ErrorCode, Int64}determines the exact number of hexagons that will be uncompacted from the compacted set
Unidirectional edge functions
H3.API.areNeighborCells — Function
areNeighborCells(origin::H3Index, destination::H3Index)::Union{H3ErrorCode, Bool}Returns whether or not the provided H3Indexes are neighbors. @param origin The origin H3 index. @param destination The destination H3 index. @param out Set to 1 if the indexes are neighbors, 0 otherwise @return Error code if the origin or destination are invalid or incomparable.
H3.API.cellsToDirectedEdge — Function
cellsToDirectedEdge(origin::H3Index, destination::H3Index)::Union{H3ErrorCode, H3Index}Returns a directed edge H3 index based on the provided origin and destination @param origin The origin H3 hexagon index @param destination The destination H3 hexagon index @return The directed edge H3Index, or H3_NULL on failure.
H3.API.isValidDirectedEdge — Function
isValidDirectedEdge(edge::H3Index)::Boolreturns whether the H3Index is a valid directed edge
H3.API.getDirectedEdgeOrigin — Function
getDirectedEdgeOrigin(edge::H3Index)::Union{H3ErrorCode, H3Index}Returns the origin hexagon from the directed edge H3Index @param edge The edge H3 index @return The origin H3 hexagon index, or H3_NULL on failure
H3.API.getDirectedEdgeDestination — Function
getDirectedEdgeDestination(edge::H3Index)::Union{H3ErrorCode, H3Index}Returns the destination hexagon from the directed edge H3Index @param edge The edge H3 index @return The destination H3 hexagon index, or H3_NULL on failure
H3.API.directedEdgeToCells — Function
directedEdgeToCells(edge::H3Index)::Union{H3ErrorCode, Tuple{H3Index, H3Index}}Returns the origin, destination pair of hexagon IDs for the given edge ID @param edge The directed edge H3Index @param originDestination Pointer to memory to store origin and destination IDs
H3.API.originToDirectedEdges — Function
originToDirectedEdges(origin::H3Index)::Union{H3ErrorCode, Vector{H3Index}}Provides all of the directed edges from the current H3Index. @param origin The origin hexagon H3Index to find edges for. @param edges The memory to store all of the edges inside.
H3.API.directedEdgeToBoundary — Function
directedEdgeToBoundary(edge::H3Index)::Union{H3ErrorCode, Vector{LatLng}}Provides the coordinates defining the directed edge. @param edge The directed edge H3Index @param cb The cellboundary object to store the edge coordinates.
Vertex functions
H3.API.getNumVertexes — Function
getNumVertexes(cell::H3Index)::IntProvides the number of vertexes for the cell. @param cell H3Index of cell
H3.API.cellToVertex — Function
cellToVertex(cell::H3Index,vertex_num::Integer)::H3IndexProvides the H3Index for the specified cell and vertex_num. vertex_num is 1-6 for hexagonal cells, 1-5 for pentagonal cells. @param cell H3Index of cell @param vertex_num vertex number of cell, starting at 1
H3.API.cellToVertexes — Function
cellToVertexes(cell::H3Index)::Vector{H3Index}Provides the vertexes for cell. @param cell H3Index of cell to get vertexes of
H3.API.vertexToLatLng — Function
vertexToLatLng(vertex::H3Index)::LatLngProvides the LatLng for vertex. @param vertex H3Index of vertex
H3.API.isValidVertex — Function
isValidVertex(vertex::H3Index)::BoolDetermines whether vertex is a valid vertex (e.g., as opposed to a cell). @param vertex H3Index of candidate vertex
Miscellaneous H3 functions
H3.API.cellAreaRads2 — Function
cellAreaRads2(cell::H3Index)::Union{H3ErrorCode, Cdouble}Exact area of specific cell in square radiants.
H3.API.cellAreaKm2 — Function
cellAreaKm2(cell::H3Index)::Union{H3ErrorCode, Cdouble}Exact area of specific cell in square kilometers.
H3.API.cellAreaM2 — Function
cellAreaM2(res::Int)::Union{H3ErrorCode, Cdouble}Exact area of specific cell in square meters.
H3.API.edgeLengthRads — Function
edgeLengthRads(edge::H3Index)::Union{H3ErrorCode, Float64}Length of a directed edge in radians.
H3.API.edgeLengthKm — Function
edgeLengthKm(edge::H3Index)::Union{H3ErrorCode, Float64}Length of a directed edge in kilometers.
H3.API.edgeLengthM — Function
edgeLengthM(edge::H3Index)::Union{H3ErrorCode, Float64}Length of a directed edge in meters.
H3.API.getNumCells — Function
getNumCells(res::Int)::Union{H3ErrorCode, Int64}number of cells (hexagons and pentagons) for a given resolution
It works out to be 2 + 120*7^r for resolution r.
Mathematical notes
Let h(n) be the number of children n levels below a single hexagon.
Then h(n) = 7^n.
Let p(n) be the number of children n levels below a single pentagon.
Then p(0) = 1, and p(1) = 6, since each pentagon has 5 hexagonal immediate children and 1 pentagonal immediate child.
In general, we have the recurrence relation
p(n) = 5h(n-1) + p(n-1) = 57^(n-1) + p(n-1).
Working through the recurrence, we get that
p(n) = 1 + 5\sum_{k=1}^n 7^{k-1} = 1 + 5(7^n - 1)/6,
using the closed form for a geometric series.
Using the closed forms for h(n) and p(n), we can get a closed form for the total number of cells at resolution r:
c(r) = 12p(r) + 110h(r) = 2 + 120*7^r.
@param res H3 cell resolution
@return number of cells at resolution res
H3.API.getRes0Cells — Function
getRes0Cells()::Union{H3ErrorCode, Vector{H3Index}}All the resolution 0 H3 indexes.
H3.API.res0CellCount — Function
res0CellCount()::Cintreturns the number of resolution 0 cells (hexagons and pentagons)
Coordinate Systems
H3.API.ijToIjk — Function
ijToIjk(c::CoordIJ)::Union{H3ErrorCode, CoordIJK}Transforms coordinates from the IJ coordinate system to the IJK+ coordinate system.
H3.API.ijkToHex2d — Function
ijkToHex2d(c::CoordIJK)::Vec2dFind the center point in 2D cartesian coordinates of a hex.
H3.API.ijkToIj — Function
ijkToIj(c::CoordIJK)::CoordIJTransforms coordinates from the IJK+ coordinate system to the IJ coordinate system.
H3.API.ijkDistance — Function
ijkDistance(c1::CoordIJK, c2::CoordIJK)::IntFinds the distance between the two coordinates. Returns result.
H3.API.ijkNormalize — Function
ijkNormalize(c::CoordIJK)::CoordIJKNormalizes ijk coordinates by setting the components to the smallest possible values. Works in place.
H3.API.cellToLocalIjk — Function
cellToLocalIjk(origin::H3Index, h3::H3Index)::Union{H3ErrorCode, CoordIJK}Produces ijk+ 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.
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.
@param origin An anchoring index for the ijk+ coordinate system. @param index Index to find the coordinates of @param out ijk+ coordinates of the index will be placed here on success @return 0 on success, or another value on failure.
H3.API.h3ToFaceIjk — Function
h3ToFaceIjk(h::H3Index)::Union{H3ErrorCode, FaceIJK}Convert an H3Index to a FaceIJK address.
H3.API.localIjkToCell — Function
localIjkToCell(origin::H3Index, ijk::CoordIJK)::Union{H3ErrorCode, H3Index}Produces an index for ijk+ coordinates anchored by an origin.
The coordinate space used by this function may have deleted regions or warping due to pentagonal distortion.
Failure may occur if the coordinates are too far away from the origin or if the index is on the other side of a pentagon.
@param origin An anchoring index for the ijk+ coordinate system. @param ijk IJK+ Coordinates to find the index of @param out The index will be placed here on success @return 0 on success, or another value on failure.
H3.API.faceIjkToH3 — Function
faceIjkToH3(faceijk::FaceIJK, res::Int)::H3IndexConvert an FaceIJK address to the corresponding H3Index.
H3.API.hex2dToCoordIJK — Function
hex2dToCoordIJK(v::Vec2d)::CoordIJKDetermine the containing hex in ijk+ coordinates for a 2D cartesian coordinate vector (from DGGRID).
H3.API.geoToVec3d — Function
geoToVec3d(geo::LatLng)::Vec3dCalculate the 3D coordinate on unit sphere from the latitude and longitude.
H3.API.geoToFaceIjk — Function
geoToFaceIjk(geo::LatLng, res::Int)::FaceIJKEncodes a coordinate on the sphere to the FaceIJK address of the containing cell at the specified resolution.