跳转到主要内容

h3_postgis

H3 PostGIS integration

pg_h3 : H3 PostGIS integration

Overview

ID Extension Package Version Category License Language
1531 h3_postgis pg_h3 4.5.0 GIS Apache-2.0 C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d-r No Yes No Yes yes no
RPM and DEB 4.5.0 require h3, postgis and postgis_raster; point coordinates use longitude, latitude.

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT PIGSTY 4.5.0 18 17 16 15 14 pg_h3 h3, postgis, postgis_raster
RPM PIGSTY 4.5.0 18 17 16 15 14 h3-pg_$v -
DEB PIGSTY 4.5.0 18 17 16 15 14 postgresql-$v-h3 -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
el8.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
el9.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
el9.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
el10.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
el10.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
d12.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
d12.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
d13.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
d13.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
u22.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
u22.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
u24.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
u24.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
u26.x86_64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0
u26.aarch64 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0 PIGSTY 4.5.0

Source

pig build pkg pg_h3;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_h3;		# install via package name, for the active PG version
pig install h3_postgis;		# install by extension name, for the current active PG version

pig install h3_postgis -v 18;   # install for PG 18
pig install h3_postgis -v 17;   # install for PG 17
pig install h3_postgis -v 16;   # install for PG 16
pig install h3_postgis -v 15;   # install for PG 15
pig install h3_postgis -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION h3_postgis CASCADE; -- requires h3, postgis, postgis_raster

Usage

Sources:

h3_postgis bridges H3 cells with PostGIS geometry, geography, and raster. It requires h3, postgis, and postgis_raster, including for geometry-only use. Input geometries must use SRID 4326 with longitude, latitude coordinates; the functions do not reproject inputs.

Convert Points and Cells

CREATE EXTENSION h3_postgis CASCADE;
SET h3.strict = true;

SELECT h3_latlng_to_cell(
    ST_SetSRID(ST_MakePoint(-122.0553238, 37.3615593), 4326), 9
);
SELECT h3_cell_to_geometry('85283473fffffff'::h3index);
SELECT h3_cell_to_boundary_geometry('85283473fffffff'::h3index);

Transform other coordinate systems to SRID 4326 with PostGIS before calling the H3 functions. Setting an SRID label alone does not transform coordinates.

Core API

Task Functions
Point to cell h3_latlng_to_cell(geometry, integer), h3_latlng_to_cell(geography, integer)
Cell center h3_cell_to_geometry, h3_cell_to_geography
Cell boundary h3_cell_to_boundary_geometry, h3_cell_to_boundary_geography
Polygon coverage h3_polygon_to_cells, h3_cells_to_multi_polygon_geometry, h3_cells_to_multi_polygon_geography
Continuous raster summaries h3_raster_summary, h3_raster_summary_stats_agg
Categorical raster summaries h3_raster_class_summary, h3_raster_class_summary_item_agg

The geometry-to-resolution @ operator also maps a location to an H3 cell. Validate polygon inputs with ST_IsValid(); repairs through ST_MakeValid() can change topology and produce geometry collections, so retain and review polygonal components before coverage calculations. Invalid polygons have undefined behavior.

Summarize Raster Data

SELECT (summary).h3,
       (h3_raster_summary_stats_agg((summary).stats)).*
FROM (
    SELECT h3_raster_summary(rast, 8) AS summary
    FROM rasters
) AS r
GROUP BY (summary).h3;

The default summary chooses a method; explicit clip, centroid, and subpixel variants let you control how raster pixels are assigned to cells. Review the choice against raster resolution and the requested H3 resolution.

Upgrade and Boundaries

ALTER EXTENSION h3 UPDATE TO '4.5.0';
ALTER EXTENSION h3_postgis UPDATE TO '4.5.0';

The base extension update performs affected btree rebuilds and distance-dependent refreshes, so plan its maintenance window before updating the companion. Version 4.5.0 fixes restricted-search-path maintenance on PostgreSQL 17+, expression-index dump/restore, and several geometry and polygonization errors. Both extension versions should match. Install the matching 4.5.0 package files before updating either extension.

h3.extend_antimeridian should normally remain false for planar overlays. Both extensions are relocatable; ensure the schemas containing H3 and PostGIS objects are visible when issuing unqualified SQL. Neither extension requires shared preloading.