pig install pg_h3;# install via package name, for the active PG versionpig install h3;# install by extension name, for the current active PG versionpig install h3 -v 18;# install for PG 18pig install h3 -v 17;# install for PG 17pig install h3 -v 16;# install for PG 16pig install h3 -v 15;# install for PG 15pig install h3 -v 14;# install for PG 14
This extension provides PostgreSQL bindings for the H3 Core Library, Uber’s hexagonal hierarchical geospatial indexing system. For the full API reference, see the H3 Documentation.
Generally, all functions have been renamed from camelCase in H3 to snake_case in SQL, prefixed with h3_.
The h3index type is an unsigned 64-bit integer representing any H3 object (hexagon, pentagon, directed edge, etc.), displayed as a 16-character hexadecimal string like '8928308280fffff'.
Indexing Functions
These functions find the H3 index containing coordinates, and the center and boundary of H3 indexes.
-- Index a location at a specified resolution (0-15)
SELECTh3_latlng_to_cell(POINT('37.3615593,-122.0553238'),5);-- Find the centroid of an index
SELECTh3_cell_to_latlng('85283473fffffff'::h3index);-- Find the boundary polygon of an index
SELECTh3_cell_to_boundary('85283473fffffff'::h3index);
Use SET h3.extend_antimeridian TO true to extend coordinates when crossing the 180th meridian.
Index Inspection Functions
-- Get resolution of an index (0-15)
SELECTh3_get_resolution('85283473fffffff'::h3index);-- Get the base cell number
SELECTh3_get_base_cell_number('85283473fffffff'::h3index);-- Validate an H3 index
SELECTh3_is_valid_cell('85283473fffffff'::h3index);-- Check if index is a pentagon
SELECTh3_is_pentagon('85283473fffffff'::h3index);-- Check if resolution has Class III orientation
SELECTh3_is_res_class_iii('85283473fffffff'::h3index);-- Find all icosahedron faces intersected by an index
SELECTh3_get_icosahedron_faces('85283473fffffff'::h3index);
Grid Traversal Functions
Grid traversal allows finding cells in the vicinity of an origin cell and determining how to traverse the grid from one cell to another.
-- Produce all indices within k distance of origin
SELECTh3_grid_disk('85283473fffffff'::h3index,2);-- Indices with distances from origin
SELECT*FROMh3_grid_disk_distances('85283473fffffff'::h3index,2);-- Hollow hexagonal ring at distance k
SELECTh3_grid_ring_unsafe('85283473fffffff'::h3index,1);-- Line of indexes between two cells (inclusive)
SELECTh3_grid_path_cells('85283473fffffff'::h3index,'8528342bfffffff'::h3index);-- Grid distance between two indices
SELECTh3_grid_distance('85283473fffffff'::h3index,'8528342bfffffff'::h3index);-- Local IJ coordinates
SELECTh3_cell_to_local_ij('85283473fffffff'::h3index,'8528342bfffffff'::h3index);SELECTh3_local_ij_to_cell('85283473fffffff'::h3index,POINT(0,0));
Hierarchical Grid Functions
Move between resolutions in the H3 grid system to produce parent (coarser) or children (finer) cells.
-- Get parent cell at a coarser resolution
SELECTh3_cell_to_parent('85283473fffffff'::h3index,3);-- Get all children at a finer resolution
SELECTh3_cell_to_children('85283473fffffff'::h3index,7);-- Get center child at a finer resolution
SELECTh3_cell_to_center_child('85283473fffffff'::h3index,7);-- Compact an array of cells
SELECTh3_compact_cells(ARRAY['85283473fffffff'::h3index,'85283477fffffff'::h3index]);-- Uncompact to a target resolution
SELECTh3_uncompact_cells(ARRAY['85283473fffffff'::h3index],7);-- Get position of child within parent's children list
SELECTh3_cell_to_child_pos('872834700ffffff'::h3index,5);-- Get child at a specific position
SELECTh3_child_pos_to_cell(0,'85283473fffffff'::h3index,7);
Region Functions
Convert H3 indexes to and from polygonal areas.
-- Fill a polygon with hexagons at a given resolution
SELECTh3_polygon_to_cells('((37.7,-122.5),(37.8,-122.5),(37.8,-122.4),(37.7,-122.4))'::polygon,NULL::polygon[],5);-- Get the outline polygon(s) of a set of hexagons
SELECT*FROMh3_cells_to_multi_polygon(ARRAY['85283473fffffff'::h3index,'85283477fffffff'::h3index]);
Unidirectional Edge Functions
Encode directed edges from one cell to a neighboring cell.
-- Check if two cells are neighbors
SELECTh3_are_neighbor_cells('85283473fffffff'::h3index,'85283477fffffff'::h3index);-- Get directed edge between neighbors
SELECTh3_cells_to_directed_edge('85283473fffffff'::h3index,'85283477fffffff'::h3index);-- Validate an edge
SELECTh3_is_valid_directed_edge(edge)FROM...;-- Get origin and destination from edge
SELECTh3_get_directed_edge_origin(edge);SELECTh3_get_directed_edge_destination(edge);-- Get both as a record
SELECT*FROMh3_directed_edge_to_cells(edge);-- All edges originating from a cell
SELECTh3_origin_to_directed_edges('85283473fffffff'::h3index);-- Edge boundary coordinates
SELECTh3_directed_edge_to_boundary(edge);
Vertex Functions
-- Get a single vertex of a cell
SELECTh3_cell_to_vertex('85283473fffffff'::h3index,0);-- Get all vertexes of a cell
SELECTh3_cell_to_vertexes('85283473fffffff'::h3index);-- Get geocoordinates of a vertex
SELECTh3_vertex_to_latlng(vertex);-- Validate a vertex
SELECTh3_is_valid_vertex(vertex);
Miscellaneous Functions
-- Great circle distance between two points (km, m, or rads)
SELECTh3_great_circle_distance(POINT(37.7,-122.5),POINT(40.7,-74.0),'km');-- Average hexagon area at a resolution
SELECTh3_get_hexagon_area_avg(5,'km^2');-- Exact area of a specific cell
SELECTh3_cell_area('85283473fffffff'::h3index,'km^2');-- Average edge length at a resolution
SELECTh3_get_hexagon_edge_length_avg(5,'km');-- Exact edge length
SELECTh3_edge_length(edge,'km');-- Number of unique cells at a resolution
SELECTh3_get_num_cells(5);-- All 122 resolution-0 cells
SELECTh3_get_res_0_cells();-- All pentagons at a resolution
SELECTh3_get_pentagons(5);
-- H3 index to/from bigint
SELECT'85283473fffffff'::h3index::bigint;SELECT599686042433355775::bigint::h3index;-- H3 index to point
SELECT'85283473fffffff'::h3index::point;
PostGIS Integration
The GEOMETRY data passed to h3-pg PostGIS functions should be in SRID 4326. Using other SRIDs (e.g. 3857) can result in errors or invalid data.
PostGIS integration requires the companion h3_postgis extension:
CREATEEXTENSIONh3_postgisCASCADE;-- Index a PostGIS geometry at a resolution
SELECTh3_latlng_to_cell(geom,9)FROMpoints;-- Convert H3 cell to PostGIS geometry/geography
SELECTh3_cell_to_geometry('85283473fffffff'::h3index);SELECTh3_cell_to_geography('85283473fffffff'::h3index);-- Cell boundary as PostGIS geometry (splits at antimeridian)
SELECTh3_cell_to_boundary_geometry('85283473fffffff'::h3index);-- Fill PostGIS polygon with H3 cells
SELECTh3_polygon_to_cells(geom,7)FROMpolygons;-- Convert H3 cells back to PostGIS multipolygon
SELECTh3_cells_to_multi_polygon_geometry(ARRAY['85283473fffffff'::h3index]);-- PostGIS indexing operators
SELECTgeom@9FROMpoints;-- geometry @ resolution
Raster Processing
For continuous raster data (temperature, elevation, etc.), summarize pixel values within H3 cells:
Raster summary methods: h3_raster_summary (auto-selects), h3_raster_summary_clip (clips by cell geometry), h3_raster_summary_centroids (groups by pixel centroid), h3_raster_summary_subpixel (for sub-pixel H3 cells). The same variants exist for class summaries.