H3 index functions
Work with Uber’s H3 hierarchical geospatial indexing system for efficient spatial analysis.
ClickHouse function reference
h3IsValid
Verifies whether the number is a valid H3 index.
Syntax
Arguments
h3index
(UInt64
): Hexagon index number.
Returns
- 1 — The number is a valid H3 index. [
UInt8
] - 0 — The number is not a valid H3 index. [
UInt8
]
Example
Result:
This example checks if the given number is a valid H3 index. The result 1
indicates that it is indeed a valid H3 index.
The H3 index system is used for geospatial indexing, where the Earth’s surface is divided into hexagonal cells. This function is useful for validating H3 indexes before using them in other H3-related operations.
h3GetResolution
Returns the resolution of the given H3 index.
Syntax:
Arguments:
h3index
(UInt64
): Hexagon index number.
Returns:
- Index resolution. Range: [0, 15]. Type:
UInt8
. - If the index is not valid, the function returns a random value. Use h3IsValid to verify the index.
Example:
Result:
In this example, we determine the resolution of a H3 index. The result shows that this particular area is represented at resolution 14, which provides a very fine-grained hexagonal grid for precise location analysis.
Here’s the updated markdown with the requested changes:
h3EdgeAngle
Calculates the average length of the H3 hexagon edge in degrees for a given resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- The average length of the H3 hexagon edge in degrees. (
Float64
)
Example:
Result:
This example calculates the average edge length in degrees for H3 hexagons at resolution 10. The result shows that each edge is approximately 0.0006 degrees long.
The edge angle becomes smaller as the resolution increases, reflecting the finer granularity of the H3 grid at higher resolutions.
h3EdgeLengthM
Calculates the average length of the H3 hexagon edge in meters for a given resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- The average length of the H3 hexagon edge in meters. (
Float64
)
Example:
Result:
In this example, we calculate the average edge length in meters for H3 hexagons at resolution 10. This information can be useful for various geospatial applications, such as determining the size of taco delivery zones or planning optimal routes for food trucks.
The edge length decreases as the resolution increases, providing finer granularity for higher resolution indexes.
h3EdgeLengthKm
Calculates the average length of the H3 hexagon edge in kilometers for a given resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- The average length of the H3 hexagon edge in kilometers. (
Float64
)
Example:
Result:
This example calculates the average edge length in kilometers for H3 hexagons at resolution 5. The result shows that at this resolution, each hexagon edge is approximately 8.54 km long.
The edge length decreases as the resolution increases, providing finer granularity for location representation.
geoToH3
Returns the H3 index of the point (lon, lat) at the specified resolution.
Syntax:
Arguments:
lon
(Float64
): Longitude.lat
(Float64
): Latitude.resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- Hexagon index number. (
UInt64
) - 0 in case of error.
Example:
Result:
This example calculates the H3 index for a taco truck location in Mexico City at resolution 15. The resulting index can be used for efficient geospatial operations and analysis of taco truck distribution patterns.
The H3 index is particularly useful for analyzing taco truck density, optimizing delivery routes, or identifying underserved areas in the taco market.
h3ToGeo
Converts an H3 index to geographic coordinates (longitude and latitude).
Syntax:
Arguments:
h3index
(UInt64
): H3 index.
Returns:
- A tuple containing two values:
(lon, lat)
.lon
(Float64
): Longitude of the H3 cell center.lat
(Float64
): Latitude of the H3 cell center.
Example:
Result:
In this example, we convert an H3 index representing a location of a taco truck to its geographic coordinates. The result shows the longitude and latitude of the center of the H3 cell where the taco truck is located.
h3ToGeoBoundary
Returns an array of coordinates representing the boundary of the H3 cell.
Syntax:
Arguments:
h3index
(UInt64
): H3 cell index.
Returns:
- Array of pairs
(lon, lat)
representing the boundary coordinates.Array(Tuple(Float64, Float64))
Example:
Result:
This function is useful for visualizing H3 cells or performing geometric operations on their boundaries. In this example, it returns the coordinates of the vertices that form the boundary of a hexagonal “taco zone” represented by the given H3 index.
h3kRing
Lists all the H3 hexagons in a ring of radius k around the given hexagon in random order.
Syntax:
Arguments:
h3index
(UInt64
): Hexagon index number representing the center.k
(Integer): Radius of the ring.
Returns:
Array of H3 indexes representing the hexagons in the ring. [Array(UInt64)
]
Example:
Result:
In this example, taco_delivery_zone
represents the hexagons surrounding the central taco shop location, which could be used to define a delivery area or analyze nearby competitors.
The order of hexagons in the resulting array is not guaranteed and may change between calls.
This function is useful for creating circular zones around a point of interest, such as determining delivery areas for a taco shop or analyzing the distribution of taco trucks in a city.
h3GetBaseCell
Returns the base cell number of the given H3 index.
Syntax:
Arguments:
index
(UInt64
): Hexagon index number.
Returns:
- Hexagon base cell number. [
UInt8
]
Example:
Result:
In this example, we determine the base cell of a taco truck’s location using its H3 index. The result shows that the taco truck is located in base cell 12, which could represent a specific region or district in the taco delivery service area.
h3HexAreaM2
Returns the average hexagon area in square meters at the given H3 resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- Area in square meters. (
Float64
)
Example:
Result:
In this example, we calculate the average area of a hexagon at resolution 7, which could represent the coverage area of a taco stand. The result shows that each hexagon at this resolution covers approximately 5.16 square kilometers.
The area of hexagons can vary slightly depending on their location on the Earth’s surface due to the projection used in the H3 system.
h3HexAreaKm2
Returns the average hexagon area in square kilometers at the given H3 resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- Area in square kilometers. (
Float64
)
Example:
Result:
In this example, we calculate the average area of a hexagon at resolution 7, which could represent a taco delivery zone. The result shows that each hexagon covers approximately 5.16 square kilometers.
The area of hexagons varies slightly depending on their position on the Earth’s surface. This function returns the average area for the given resolution.
h3IndexesAreNeighbors
Determines if two H3 indexes are neighbors.
Syntax:
Arguments:
h3index1
(UInt64
): First H3 index.h3index2
(UInt64
): Second H3 index.
Returns:
1
if the indexes are neighbors,0
otherwise. [UInt8
]
Example:
Result:
In this example, we check if a Taco Store location and a neighboring taco truck (represented by their H3 indexes) are neighbors. The result 1
indicates that they are indeed neighbors in the H3 grid system.
This function is useful for spatial analysis, such as finding adjacent areas or checking proximity in location-based applications.
h3ToChildren
Returns an array of child indexes for the given H3 index at the specified resolution.
Syntax:
Arguments:
index
(UInt64
): The parent H3 index.resolution
(UInt8
): The desired resolution for the child indexes. Range: [0, 15].
Returns:
- An array of child H3 indexes. [
Array(UInt64)
]
Example:
Result:
In this example, we’re dividing a larger taco delivery area (represented by the parent H3 index) into smaller, more precise delivery zones at resolution 6.
The number of child indexes returned depends on the difference between the resolution of the input index and the requested resolution. A larger difference will result in more child indexes.
h3ToParent
Returns the parent (coarser) index containing the given H3 index.
Syntax:
Arguments:
index
(UInt64
): Hexagon index number.resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- Parent H3 index.
UInt64
.
Example:
Result:
In this example, we find the parent index at resolution 3 for the given H3 index. This can be useful for aggregating data from finer resolutions to coarser ones in geospatial analysis, such as grouping taco shop locations into larger areas.
h3ToString
Converts the H3Index representation of an H3 index to its string representation.
Syntax:
Arguments:
index
(UInt64
): H3 index number.
Returns:
- String representation of the H3 index. [
String
]
Example:
Result:
In this example, we convert the numeric H3 index of a taco shop location to its string representation, which can be useful for human-readable output or when interfacing with systems that expect H3 indexes in string format.
stringToH3
Converts the string representation of an H3 index to its 64-bit integer representation.
Syntax:
Arguments:
index_str
(String
): String representation of the H3 index.
Returns:
- The 64-bit integer representation of the H3 index. [
UInt64
] - Returns 0 if the input is not a valid H3 index string.
Example:
Result:
In this example, we convert a string representation of an H3 index (possibly representing a taco delivery zone) to its 64-bit integer form.
This function is the inverse of h3ToString
. It’s useful when you have H3 indexes stored as strings and need to perform operations that require the numeric representation.
h3GetResolution
Returns the resolution of the given H3 index.
Syntax:
Arguments:
h3index
(UInt64
): Hexagon index number.
Returns:
- Index resolution. Range: [0, 15]. Type:
UInt8
. - If the index is not valid, the function returns a random value. Use h3IsValid to verify the index.
Example:
Result:
In this example, we determine the resolution of a H3 index. The result shows that this particular area is represented at resolution 14, which provides a very fine-grained hexagonal grid for precise location analysis.
h3IsResClassIII
Returns whether the H3 index has a resolution with Class III orientation.
Syntax:
Arguments:
index
(UInt64
): Hexagon index number.
Returns:
1
(UInt8
): Index has a resolution with Class III orientation.0
(UInt8
): Index doesn’t have a resolution with Class III orientation.
Example:
Result:
In this example, we check if the H3 index representing a taco shop location has a Class III orientation. A result of 1 indicates that it does, which might affect how neighboring hexagons are arranged around this location.
h3IsPentagon
Returns whether the given H3 index represents a pentagonal cell.
Syntax:
Parameter:
index
(UInt64
): Hexagon index number.
Returns:
1
— Index represents a pentagonal cell.UInt8
.0
— Index doesn’t represent a pentagonal cell.UInt8
.
Example:
Result:
In this example, we check if the H3 index 599686042433355775
represents a pentagonal cell. The result 0
indicates that it does not.
Pentagonal cells are special cases in the H3 grid system, occurring 12 times per resolution (except for resolution 0, which has 5 pentagons). They are important to handle correctly in certain geometric operations.
h3GetFaces
Returns the icosahedron faces intersected by a given H3 index.
Syntax:
Arguments:
index
(UInt64
): H3 index number.
Returns:
Array containing icosahedron faces intersected by the given H3 index. [Array(UInt64)
]
Example:
Result:
In this example, we determine which icosahedron face(s) are intersected by the H3 index representing a taco truck’s location. The result [7]
indicates that this H3 index intersects with face 7 of the icosahedron.
The icosahedron is the base geometric shape used in the H3 system. Understanding which faces are intersected can be useful for certain geographic analyses or optimizations.
h3CellAreaM2
Returns the exact area of a specific H3 cell in square meters.
Syntax:
Arguments:
index
(UInt64
): H3 index number.
Returns:
- The exact area of the H3 cell in square meters. [
Float64
]
Example:
Result:
In this example, we calculate the area of a hypothetical taco stand location represented by the H3 index. The result shows the exact area of the H3 cell in square meters, which could be useful for analyzing the coverage or distribution of taco stands across different H3 cells.
h3CellAreaRads2
Returns the exact area of a specific H3 cell in square radians.
Syntax:
Arguments:
index
(UInt64
): H3 index of the cell.
Returns:
- The exact area of the cell in square radians. (
Float64
)
Example:
Result:
This example calculates the area in square radians for a specific H3 cell, which could represent a taco delivery zone. The result is a precise measurement that can be used for various geospatial calculations, such as determining the coverage area of a taco truck’s service region.
h3ToCenterChild
Returns the center child (finer) H3 index contained by the given H3 index at the specified resolution.
Syntax:
Arguments:
index
(UInt64
): H3 index of the parent hexagon.resolution
(UInt8
): Desired resolution for the child index. Range: [0, 15].
Returns:
- H3 index of the center child hexagon. (
UInt64
)
Example:
Result:
In this example, we find the center child of a hexagon at resolution 6 (represented by the index 599405990164561919) at the next finer resolution (7). The result is the H3 index of the central hexagon among the seven smaller hexagons that make up the original hexagon.
This function is useful for hierarchical spatial analysis, allowing you to zoom in on the central part of a hexagonal area while maintaining the H3 grid structure.
h3ExactEdgeLengthM
Returns the exact edge length of the unidirectional edge represented by the input H3 index in meters.
Syntax:
Arguments:
index
(UInt64
): Hexagon index number.
Returns:
- Exact edge length in meters. (
Float64
)
Example:
Result:
This function is useful for precise distance calculations in geospatial applications, such as measuring the exact length of taco truck routes between H3 cells.
h3ExactEdgeLengthKm
Returns the exact edge length of the unidirectional edge represented by the input H3 index in kilometers.
Syntax:
Arguments:
index
(UInt64
): Hexagon index number.
Returns:
- Exact edge length in kilometers. (
Float64
)
Example:
Result:
This function is useful for precise distance calculations in geospatial applications, such as determining the exact length of taco delivery routes optimized using H3 indexes.
h3ExactEdgeLengthRads
Returns the exact edge length of the unidirectional edge represented by the input H3 index in radians.
Syntax:
Arguments:
index
(UInt64
): Hexagon index number.
Returns:
- Exact edge length in radians. (
Float64
)
Example:
Result:
In this example, we calculate the exact edge length in radians for the H3 index representing a taco shop location. This can be useful for precise distance calculations in geospatial taco delivery optimization algorithms.
h3NumHexagons
Returns the number of unique H3 indices at the given resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
- Number of H3 indices. (
Int64
)
Example:
Result:
In this example, we calculate the number of unique H3 indices (potential taco delivery zones) at resolution 3. This could be useful for estimating the coverage of a taco delivery service at different granularities.
The number of hexagons increases significantly with each resolution level. Be cautious when using high resolutions, as it may result in a very large number of hexagons.
h3PointDistM
Calculates the “great circle” or “haversine” distance between two geographic points in meters.
Syntax:
Arguments:
lat1
,lon1
(Float64
): Latitude and longitude of the first point in degrees.lat2
,lon2
(Float64
): Latitude and longitude of the second point in degrees.
Returns:
- The great circle distance between the two points in meters. (
Float64
)
Example:
Result:
This example calculates the distance in meters between San Francisco (Taco Town) and Los Angeles (Burrito City).
This function is particularly useful for calculating distances between taco trucks or restaurants in different cities.
h3PointDistKm
Calculates the “great circle” or “haversine” distance between two geographic points on Earth in kilometers.
Syntax:
Arguments:
lat1
,lon1
(Float64
): Latitude and longitude of the first point in degrees.lat2
,lon2
(Float64
): Latitude and longitude of the second point in degrees.
Returns:
The great circle distance between the two points in kilometers. (Float64
)
Example:
Result:
This example calculates the distance between San Francisco (Taco Town) and Los Angeles (Burrito City) in kilometers.
The function uses the Earth’s mean radius for calculations. For very precise measurements or for locations near the poles, consider using more specialized geodesic functions.
h3PointDistRads
Returns the “great circle” or “haversine” distance between pairs of geographic coordinates in radians.
Syntax:
Arguments:
lat1
,lon1
(Float64
): Latitude and longitude of the first point in degrees.lat2
,lon2
(Float64
): Latitude and longitude of the second point in degrees.
Returns:
- The great circle distance between the two points in radians. (
Float64
)
Example:
Result:
In this example, we calculate the distance in radians between San Francisco (the taco capital of the Bay Area) and Los Angeles (home of the famous Korean taco trucks).
This function is useful for calculating distances on a spherical surface, such as Earth, without the need for unit conversion. It’s particularly handy when working with H3 indexes or other geospatial applications where radian measurements are preferred.
h3GetRes0Indexes
Returns an array of all the resolution 0 H3 indexes.
Syntax:
Returns:
Array of all the resolution 0 H3 indexes. [Array(UInt64)
]
Example:
Result:
In this example, h3GetRes0Indexes()
returns an array of 122 UInt64
values representing all the base (resolution 0) H3 cells. These could be thought of as the largest possible “taco delivery zones” in a global H3-based delivery system.
The resolution 0 indexes represent the coarsest division of the Earth’s surface in the H3 system, with each cell covering approximately 4,250,546.8 km² (about 7.81% of the Earth’s surface).
h3GetPentagonIndexes
Returns all the pentagon H3 indexes at the specified resolution.
Syntax:
Arguments:
resolution
(UInt8
): Index resolution. Range: [0, 15].
Returns:
Array of all pentagon H3 indexes. [Array(UInt64)
]
Example:
Result:
In this example, we retrieve all pentagon H3 indexes at resolution 3. The result is an array of UInt64 values representing the pentagon indexes.
Pentagon indexes are special cases in the H3 grid system, occurring 12 times per resolution (once per face of the icosahedron). They are important for certain geospatial calculations and analyses.
Here’s the updated markdown with the requested changes:
h3Line
Returns the line of H3 indices between two given H3 indices.
Syntax:
Arguments:
start
(UInt64
): The H3 index of the starting point.end
(UInt64
): The H3 index of the ending point.
Returns:
An array of H3 indices representing the line between the start and end points. [Array(UInt64)
]
Example:
Result:
This example calculates a route for taco delivery between two locations in San Francisco, represented by their H3 indices.
The function returns the shortest path between two H3 indices. It’s useful for routing and distance calculations in geospatial applications.
h3Distance
Calculates the distance in grid cells between two H3 indexes.
Syntax:
Arguments:
start
(UInt64
): The starting H3 index.end
(UInt64
): The ending H3 index.
Returns:
- The number of grid cells between the two indexes. [
Int64
] - Returns a negative number if finding the distance fails.
Example:
Result:
In this example, we calculate the distance between a Taco Stand location and a nearby taco truck using their H3 indexes at resolution 9. The result shows they are 3 grid cells apart.
The h3Distance
function is useful for quickly estimating distances between locations without the need for precise geographic calculations. It’s particularly efficient for proximity searches and clustering analysis in location-based applications.
Here’s the updated markdown with the requested changes:
h3HexRing
Returns the indexes of the hexagonal ring centered at the provided origin H3 index and with the specified length k.
Syntax:
Arguments:
index
(UInt64
): H3 index representing the origin.k
(UInt64
): Distance from the origin.
Returns:
- Array of H3 indexes representing the hexagonal ring. [
Array(UInt64)
]
Example:
Result:
In this example, we define a taco delivery zone as a hexagonal ring around a central location in San Francisco. The result is an array of H3 indexes representing the perimeter of the delivery area, which could be used to determine eligible delivery addresses or calculate delivery fees.
If a pentagonal distortion is encountered, the function may return fewer than 6 * k indexes.
h3GetUnidirectionalEdge
Returns a unidirectional edge H3 index based on the provided origin and destination hexagons.
Syntax:
Arguments:
originIndex
(UInt64
): Origin hexagon index.destinationIndex
(UInt64
): Destination hexagon index.
Returns:
- Unidirectional edge H3 index (
UInt64
). - Returns 0 if the hexagons are not neighbors or if the input is invalid.
Example:
Result:
In this example, we calculate the unidirectional edge between two neighboring H3 indexes, which could represent a taco delivery route from one hexagon to another.
This function is useful for creating directed graphs of H3 cells, which can be used for routing or connectivity analysis in geospatial applications.
h3UnidirectionalEdgeIsValid
Determines if the provided H3 index is a valid unidirectional edge index.
Syntax:
Arguments:
index
(UInt64
): H3 index number.
Returns:
- 1 — The H3 index is a valid unidirectional edge. [
UInt8
] - 0 — The H3 index is not a valid unidirectional edge. [
UInt8
]
Example:
Result:
In this example, we check if the given H3 index represents a valid unidirectional edge. The result 1
indicates that it is indeed a valid unidirectional edge.
This function is useful for validating H3 indexes when working with unidirectional edges in geospatial applications, such as routing or boundary analysis in taco delivery service areas.
h3GetOriginIndexFromUnidirectionalEdge
Returns the origin hexagon index from a given unidirectional edge H3 index.
Syntax
Arguments
edge
(UInt64
): Hexagon index number representing a unidirectional edge.
Returns
- Origin hexagon index number. (
UInt64
)
Example
Result:
This example demonstrates how to get the origin hexagon index from a unidirectional edge in a taco delivery route optimization system. The origin index could represent the starting point of a delivery path between two adjacent hexagonal areas.
h3GetDestinationIndexFromUnidirectionalEdge
Returns the destination hexagon index from a unidirectional edge H3 index.
Syntax:
Arguments:
edge
(UInt64
): Hexagon index number representing a unidirectional edge.
Returns:
- Destination hexagon index number. [
UInt64
]
Example:
Result:
This function is useful when working with H3 unidirectional edges, particularly in geospatial applications or routing algorithms. It allows you to extract the destination hexagon from an edge representation.
Ensure that the input is a valid unidirectional edge index. You can use h3UnidirectionalEdgeIsValid
to check the validity of an edge index before using this function.
h3GetIndexesFromUnidirectionalEdge
Returns the origin and destination hexagon indexes from the given unidirectional edge H3 index.
Syntax:
Arguments:
edge
(UInt64
): Hexagon index number that represents a unidirectional edge.
Returns:
A tuple consisting of two values tuple(origin, destination)
:
origin
(UInt64
): Origin hexagon index number.destination
(UInt64
): Destination hexagon index number.
Returns (0, 0)
if the provided input is not a valid unidirectional edge.
Example:
Result:
In this example, we get the origin and destination hexagon indexes for a taco delivery route represented by a unidirectional edge. The result shows the starting point (origin) and ending point (destination) of the delivery route.
h3GetUnidirectionalEdgesFromHexagon
Returns all of the unidirectional edges originating from the provided H3 index.
Syntax:
Arguments:
index
(UInt64
): H3 index of the origin hexagon.
Returns:
Array of H3 indexes representing each unidirectional edge originating from the given hexagon. [Array(UInt64)
]
Example:
Result:
In this example, we get all the unidirectional edges (potential taco delivery routes) from a specific H3 hexagon. Each edge represents a path to a neighboring hexagon, which could be used to plan efficient taco deliveries to adjacent areas.
This function is useful for analyzing connectivity and creating path-finding algorithms in hexagonal grid systems, such as optimizing delivery routes or planning service coverage areas.
h3GetUnidirectionalEdgeBoundary
Returns the coordinates defining the boundary of a unidirectional edge in the H3 grid system.
Syntax:
Arguments:
edge
(UInt64
): A unidirectional edge H3 index.
Returns:
- An array of pairs
(lon, lat)
representing the boundary coordinates of the edge. [Array(Tuple(Float64, Float64))
]
Example:
Result:
In this example, the function returns the coordinates of the start and end points of the unidirectional edge represented by the H3 index 1248204388774707199
. These coordinates could be used for visualizing the edge on a map or for further geographical calculations.
This function is particularly useful when working with H3 edges in geospatial applications, allowing you to obtain the precise geographical boundaries of H3 grid edges.