website/versioned_docs/version-3.x/api/traversal.mdx
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Grid traversal allows finding cells in the vicinity of an origin cell, and determining how to traverse the grid from one cell to another.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
void kRing(H3Index origin, int k, H3Index* out);
h3.k_ring(origin, k)
List<Long> kRing(long origin, int k);
List<String> kRing(String origin, int k);
h3.kRing(origin, k)
function example() {
const h = '85283473fffffff';
const k = 5;
return h3v3.kRing(h, k);
}
k-rings produces indices within k distance of the origin index.
k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indices, 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.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int maxKringSize(int k);
:::note
This function exists for memory management and is not exposed.
:::
</TabItem> <TabItem value="java">:::note
This function exists for memory management and is not exposed.
:::
</TabItem> <TabItem value="javascript">:::note
This function exists for memory management and is not exposed.
:::
</TabItem> </Tabs>Maximum number of indices that result from the kRing algorithm with the given k.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
void kRingDistances(H3Index origin, int k, H3Index* out, int* distances);
h3.k_ring_distances(origin, k)
List<List<Long>> kRingDistances(long origin, int k);
List<List<String>> kRingDistances(String origin, int k);
h3.kRingDistances(origin, k)
function example() {
const h = '85283473fffffff';
const k = 5;
return h3v3.kRingDistances(h, k);
}
k-rings produces indices within k distance of the origin index.
k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indices, 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.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int hexRange(H3Index origin, int k, H3Index* out);
h3.hex_range(h, k)
List<List<Long>> hexRange(Long h3, int k) throws PentagonEncounteredException;
List<List<String>> hexRange(String h3Address, int k) throws PentagonEncounteredException;
:::note
This function is not exposed.
:::
</TabItem> </Tabs>hexRange 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.
Returns 0 if no pentagonal distortion is encountered.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int hexRangeDistances(H3Index origin, int k, H3Index* out, int* distances);
h3.hex_range_distances(h, k)
:::note
This function is not exposed because the same functionality is exposed by hexRange
:::
</TabItem> <TabItem value="javascript">:::note
This function is not exposed.
:::
</TabItem> </Tabs>hexRange 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. The distances in hexagons is placed in the distances array at the same offset.
Returns 0 if no pentagonal distortion is encountered.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int hexRanges(H3Index* h3Set, int length, int k, H3Index* out);
h3.hex_ranges(h, k)
:::note
This function is not exposed because the same functionality is exposed by hexRange
:::
</TabItem> <TabItem value="javascript">:::note
This function is not exposed.
:::
</TabItem> </Tabs>hexRanges 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.
Returns 0 if no pentagonal distortion was encountered. Otherwise, output is undefined
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int hexRing(H3Index origin, int k, H3Index* out);
h3.hex_ring(h, k)
List<Long> hexRing(long h3, int k) throws PentagonEncounteredException;
List<String> hexRing(String h3Address, int k) throws PentagonEncounteredException;
h3.hexRing(h3Index, k)
function example() {
const h = '85283473fffffff';
const k = 1;
return h3v3.hexRing(h, k);
}
Produces the hollow hexagonal ring centered at origin with sides of length k.
Returns 0 if no pentagonal distortion was encountered.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int h3Line(H3Index start, H3Index end, H3Index* out);
h3.h3_line(start, end)
List<Long> h3Line(long start, long end) throws LineUndefinedException
List<String> h3Line(String startAddress, String endAddress) throws LineUndefinedException
h3.h3Line(start, end)
function example() {
const start = '85283473fffffff';
const end = '8528342bfffffff';
return h3v3.h3Line(start, end);
}
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 h3Distance(start, 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.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int h3LineSize(H3Index start, H3Index end);
:::note
This function exists for memory management and is not exposed.
:::
</TabItem> <TabItem value="java">:::note
This function exists for memory management and is not exposed.
:::
</TabItem> <TabItem value="javascript">:::note
This function exists for memory management and is not exposed.
:::
</TabItem> </Tabs>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.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int h3Distance(H3Index origin, H3Index h3);
h3.h3_distance(h1, h2)
int h3Distance(long a, long b) throws DistanceUndefinedException;
int h3Distance(String a, String b) throws DistanceUndefinedException;
h3.h3Distance(a, b)
function example() {
const start = '85283473fffffff';
const end = '8528342bfffffff';
return h3v3.h3Distance(start, end);
}
Returns the distance in grid cells between the two indexes.
Returns a negative number if finding the distance failed. Finding the distance can fail because the two indexes are not comparable (different resolutions), too far apart, or are separated by pentagonal distortion. This is the same set of limitations as the local IJ coordinate space functions.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out);
h3.experimental_h3_to_local_ij(origin, h)
CoordIJ experimentalH3ToLocalIj(long origin, long h3) throws PentagonEncounteredException, LocalIjUndefinedException;
CoordIJ experimentalH3ToLocalIj(String originAddress, String h3Address) throws PentagonEncounteredException, LocalIjUndefinedException;
h3.experimentalH3ToLocalIj(origin, h3)
function example() {
const origin = '85283473fffffff';
const h = '8528342bfffffff';
const {i, j} = h3v3.experimentalH3ToLocalIj(origin, h);
return [i, j];
}
Produces local IJ coordinates for an H3 index anchored by an origin.
This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.
<Tabs groupId="language" defaultValue="c" values={[ {label: 'C', value: 'c'}, {label: 'Python', value: 'python'}, {label: 'Java', value: 'java'}, {label: 'JavaScript (Live)', value: 'javascript'}, ] }> <TabItem value="c">
int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out);
h3.experimental_local_ij_to_h3(origin, i, j)
long experimentalLocalIjToH3(long origin, CoordIJ ij) throws LocalIjUndefinedException;
String experimentalLocalIjToH3(String originAddress, CoordIJ ij) throws LocalIjUndefinedException;
h3.experimentalLocalIjToH3(origin, coords)
function example() {
const h = '85283473fffffff';
const coords = {i: 0, j: 0};
return h3v3.experimentalLocalIjToH3(h, coords);
}
Produces an H3 index from local IJ coordinates anchored by an origin.
This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.