Functions for Working with H3 Indexes

    The level of the hierarchy is called and can receive a value from 0 till 15, where 0 is the base level with the largest and coarsest cells.

    A latitude and longitude pair can be transformed to a 64-bit H3 index, identifying a grid cell.

    The H3 index is used primarily for bucketing locations and other geospatial manipulations.

    The full description of the H3 system is available at the Uber Engeneering site.

    Verifies whether the number is a valid index.

    Syntax

    Parameter

    • h3index — Hexagon index number. Type: UInt64.

    Returned values

    • 1 — The number is a valid H3 index.
    • 0 — The number is not a valid H3 index.

    Type: .

    Example

    Query:

    1. SELECT h3IsValid(630814730351855103) as h3IsValid;

    Result:

    1. ┌─h3IsValid─┐
    2. 1
    3. └───────────┘

    h3GetResolution

    Defines the resolution of the given index.

    Syntax

    1. h3GetResolution(h3index)

    Parameter

    • h3index — Hexagon index number. Type: UInt64.

    Returned values

    • Index resolution. Range: [0, 15].
    • If the index is not valid, the function returns a random value. Use to verify the index.

    Type: UInt8.

    Example

    Query:

    1. SELECT h3GetResolution(639821929606596015) as resolution;

    Result:

    1. ┌─resolution─┐
    2. 14
    3. └────────────┘

    h3EdgeAngle

    Calculates the average length of the H3 hexagon edge in grades.

    Syntax

    1. h3EdgeAngle(resolution)

    Parameter

    • resolution — Index resolution. Type: . Range: [0, 15].

    Returned values

    • The average length of the H3 hexagon edge in grades. Type: .

    Example

    Query:

    1. SELECT h3EdgeAngle(10) as edgeAngle;

    Result:

    1. ┌───────h3EdgeAngle(10)─┐
    2. 0.0005927224846720883
    3. └───────────────────────┘

    h3EdgeLengthM

    Calculates the average length of the hexagon edge in meters.

    Syntax

    1. h3EdgeLengthM(resolution)

    Parameter

    • resolution — Index resolution. Type: UInt8. Range: [0, 15].

    Returned values

    • The average length of the hexagon edge in meters. Type: Float64.

    Example

    Query:

    1. SELECT h3EdgeLengthM(15) as edgeLengthM;

    Result:

    1. ┌─edgeLengthM─┐
    2. 0.509713273
    3. └─────────────┘

    geoToH3

    Returns H3 point index (lon, lat) with specified resolution.

    Syntax

    1. geoToH3(lon, lat, resolution)

    Arguments

    • lon — Longitude. Type: .
    • lat — Latitude. Type: Float64.
    • resolution — Index resolution. Range: [0, 15]. Type: .

    Returned values

    • Hexagon index number.
    • 0 in case of error.

    Type: UInt64.

    Example

    Query:

      Result:

      1. ┌────────────h3Index─┐
      2. 644325524701193974
      3. └────────────────────┘

      h3ToGeo

      Returns the geographical coordinates of longitude and latitude corresponding to the provided H3 index.

      Syntax

      1. h3ToGeo(h3Index)

      Arguments

      • h3Index — H3 Index. .

      Returned values

      • A tuple consisting of two values: tuple(lon,lat). lon — Longitude. Float64. lat — Latitude. .

      Example

      Query:

      1. SELECT h3ToGeo(644325524701193974) AS coordinates;

      Result:

      1. ┌─coordinates───────────────────────────┐
      2. (37.79506616830252,55.71290243145668)

      Syntax

      1. h3ToGeoBoundary(h3Index)

      Arguments

      • h3Index — H3 Index. Type: UInt64.

      Returned values

      • Array of pairs ‘(lon, lat)’.
        Type: (Float64, ).

      Example

      Query:

      Result:

      1. ┌─h3ToGeoBoundary(599686042433355775)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
      2. [(37.2713558667319,-121.91508032705622),(37.353926450852256,-121.8622232890249),(37.42834118609435,-121.92354999630156),(37.42012867767779,-122.03773496427027),(37.33755608435299,-122.090428929044),(37.26319797461824,-122.02910130919001)]
      3. └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

      h3kRing

      Lists all the hexagons in the raduis of k from the given hexagon in random order.

      Syntax

      1. h3kRing(h3index, k)

      Arguments

      • h3index — Hexagon index number. Type: UInt64.
      • k — Raduis. Type:

      Returned values

      • Array of H3 indexes.

      Type: Array().

      Example

      Query:

      1. SELECT arrayJoin(h3kRing(644325529233966508, 1)) AS h3index;

      Result:

      1. ┌────────────h3index─┐
      2. 644325529233966508
      3. 644325529233966497
      4. 644325529233966510
      5. 644325529233966504
      6. 644325529233966509
      7. 644325529233966355
      8. 644325529233966354
      9. └────────────────────┘

      h3GetBaseCell

      Returns the base cell number of the index.

      Syntax

      1. h3GetBaseCell(index)

      Parameter

      • index — Hexagon index number. Type: UInt64.

      Returned value

      • Hexagon base cell number.

      Type: .

      Example

      Query:

      1. SELECT h3GetBaseCell(612916788725809151) as basecell;

      Result:

      1. ┌─basecell─┐
      2. 12
      3. └──────────┘

      h3HexAreaM2

      Returns average hexagon area in square meters at the given resolution.

      Syntax

      1. h3HexAreaM2(resolution)

      Parameter

      • resolution — Index resolution. Range: [0, 15]. Type: .

      Returned value

      • Area in square meters.

      Type: Float64.

      Example

      Query:

      1. SELECT h3HexAreaM2(13) as area;

      Result:

      1. ┌─area─┐
      2. 43.9
      3. └──────┘

      h3IndexesAreNeighbors

      Returns whether or not the provided H3 indexes are neighbors.

      Syntax

      1. h3IndexesAreNeighbors(index1, index2)

      Arguments

      • index1 — Hexagon index number. Type: .
      • index2 — Hexagon index number. Type: UInt64.

      Returned value

      • 1 — Indexes are neighbours.
      • 0 — Indexes are not neighbours.

      Type: .

      Example

      Query:

      1. SELECT h3IndexesAreNeighbors(617420388351344639, 617420388352655359) AS n;

      Result:

      1. ┌─n─┐
      2. 1
      3. └───┘

      h3ToChildren

      Returns an array of child indexes for the given index.

      Syntax

      1. h3ToChildren(index, resolution)

      Arguments

      • resolution — Index resolution. Range: [0, 15]. Type: UInt8.

      Returned values

      • Array of the child H3-indexes.

      Type: (UInt64).

      Example

      Query:

      1. SELECT h3ToChildren(599405990164561919, 6) AS children;

      Result:

      1. ┌─children───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
      2. └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

      Returns the parent (coarser) index containing the given index.

      Syntax

      1. h3ToParent(index, resolution)

      Arguments

      • index — Hexagon index number. Type: UInt64.
      • resolution — Index resolution. Range: [0, 15]. Type: .
      • Parent H3 index.

      Type: UInt64.

      Example

      Query:

      1. SELECT h3ToParent(599405990164561919, 3) as parent;

      Result:

      h3ToString

      Converts the H3Index representation of the index to the string representation.

      1. h3ToString(index)

      Parameter

      • index — Hexagon index number. Type: UInt64.

      Returned value

      • String representation of the H3 index.

      Type: .

      Example

      Query:

      1. SELECT h3ToString(617420388352917503) as h3_string;

      Result:

      1. ┌─h3_string───────┐
      2. 89184926cdbffff
      3. └─────────────────┘

      stringToH3

      Converts the string representation to the H3Index (UInt64) representation.

      Syntax

      1. stringToH3(index_str)

      Parameter

      • index_str — String representation of the H3 index. Type: .

      Returned value

      • Hexagon index number. Returns 0 on error. Type: UInt64.

      Example

      Query:

      1. SELECT stringToH3('89184926cc3ffff') as index;

      Result:

      1. ┌──────────────index─┐
      2. 617420388351344639
      3. └────────────────────┘

      h3GetResolution

      Returns the resolution of the H3 index.

      Syntax

      1. h3GetResolution(index)

      Parameter

      • index — Hexagon index number. Type: .

      Returned value

      • Index resolution. Range: [0, 15]. Type: UInt8.

      Example

      Query:

      1. SELECT h3GetResolution(617420388352917503) as res;

      Result:

      1. ┌─res─┐
      2. 9
      3. └─────┘

      h3IsResClassIII

      Returns whether H3 index has a resolution with Class III orientation.

      Syntax

      1. h3IsResClassIII(index)

      Parameter

      • index — Hexagon index number. Type: .

      Returned value

      • 1 — Index has a resolution with Class III orientation.
      • 0 — Index doesn’t have a resolution with Class III orientation.

      Type: UInt8.

      Example

      Query:

      1. SELECT h3IsResClassIII(617420388352917503) as res;

      Result:

      1. ┌─res─┐
      2. 1
      3. └─────┘

      h3IsPentagon

      Returns whether this H3 index represents a pentagonal cell.

      Syntax

      1. h3IsPentagon(index)

      Parameter

      • index — Hexagon index number. Type: .

      Returned value

      • 1 — Index represents a pentagonal cell.
      • 0 — Index doesn’t represent a pentagonal cell.

      Type: UInt8.

      Example

      Query:

      1. SELECT SELECT h3IsPentagon(644721767722457330) as pentagon;

      Result:

      1. ┌─pentagon─┐
      2. 0
      3. └──────────┘

      Returns icosahedron faces intersected by a given index.

      Syntax

      1. h3GetFaces(index)

      Parameter

      • index — Hexagon index number. Type: UInt64.

      Returned values

      • Array containing icosahedron faces intersected by a given H3 index.

      Type: (UInt64).

      Example

      Query:

      1. SELECT SELECT h3GetFaces(599686042433355775) as faces;

      Result:

      1. ┌─faces─┐
      2. [7]