Skip to content

Instantly share code, notes, and snippets.

@bnaul
Created January 8, 2026 15:49
Show Gist options
  • Select an option

  • Save bnaul/d4d3e651ffd9a81f7e42bc623860a9fb to your computer and use it in GitHub Desktop.

Select an option

Save bnaul/d4d3e651ffd9a81f7e42bc623860a9fb to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION bqutils.geo.H3_FROMLONGLAT(longitude FLOAT64, latitude FLOAT64, resolution INT64)
RETURNS STRING
LANGUAGE js
OPTIONS (
library = ["https://storage.googleapis.com/bqutils.replicahq.com/h3-js.min.js"],
description = """Converts a longitude/latitude pair to an H3 cell index at the specified resolution.
Example usage:
SELECT bqutils.geo.H3_FROMLONGLAT(-122.4194, 37.7749, 9) as h3_index;
Output:
"8928308280fffff"
Parameters:
- longitude: The longitude coordinate (FLOAT64)
- latitude: The latitude coordinate (FLOAT64)
- resolution: The H3 resolution (0-15, where 0 is coarsest and 15 is finest)
Returns:
The H3 cell index as a hexadecimal string, or null if inputs are invalid."""
)
AS r"""
try {
return h3.latLngToCell(latitude, longitude, Number(resolution));
} catch (error) {
return null;
}
""";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment