Skip to content

qdgc

Encode, decode, navigate, and fill Extended Quarter Degree Grid Cell codes in pure SQL.

qdgc · Encode, decode, navigate, and fill Extended Quarter Degree Grid Cell codes in pure SQL.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1700qdgcqdgc0.1.0GISApache-2.0SQL
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
----dtrNoNoNoYesyesyes
PGXN distribution qdgc also ships qdgc_postgis; the GitHub v0.1.0 tag belongs to qdgc-py and is not this PGXN release.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.1.019 18 17 16 15qdgc-
RPMPIGSTY0.1.019 18 17 16 15qdgc_$v-
DEBPIGSTY0.1.019 18 17 16 15postgresql-$v-qdgc-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el8.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
PackageVersionOSORGSIZEFile URL
qdgc_190.1.0el9.x86_64pigsty24.7 KiBqdgc_19-0.1.0-1PGSTY.el9.noarch.rpm
qdgc_190.1.0el9.aarch64pigsty24.7 KiBqdgc_19-0.1.0-1PGSTY.el9.noarch.rpm
qdgc_190.1.0el10.x86_64pigsty24.8 KiBqdgc_19-0.1.0-1PGSTY.el10.noarch.rpm
qdgc_190.1.0el10.aarch64pigsty24.8 KiBqdgc_19-0.1.0-1PGSTY.el10.noarch.rpm
postgresql-19-qdgc0.1.0d12.x86_64pigsty16.2 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~bookworm_all.deb
postgresql-19-qdgc0.1.0d12.aarch64pigsty16.2 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~bookworm_all.deb
postgresql-19-qdgc0.1.0d13.x86_64pigsty16.2 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~trixie_all.deb
postgresql-19-qdgc0.1.0d13.aarch64pigsty16.2 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~trixie_all.deb
postgresql-19-qdgc0.1.0u22.x86_64pigsty16.6 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~jammy_all.deb
postgresql-19-qdgc0.1.0u22.aarch64pigsty16.6 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~jammy_all.deb
postgresql-19-qdgc0.1.0u24.x86_64pigsty16.6 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~noble_all.deb
postgresql-19-qdgc0.1.0u24.aarch64pigsty16.6 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~noble_all.deb
postgresql-19-qdgc0.1.0u26.x86_64pigsty16.7 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~resolute_all.deb
postgresql-19-qdgc0.1.0u26.aarch64pigsty16.7 KiBpostgresql-19-qdgc_0.1.0-1PGSTY~resolute_all.deb

Source

pig build pkg qdgc;		# 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 qdgc;		# install by package name

pig install qdgc -v 19;   # install for PG 19
pig install qdgc -v 18;   # install for PG 18
pig install qdgc -v 17;   # install for PG 17
pig install qdgc -v 16;   # install for PG 16
pig install qdgc -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION qdgc;

Usage

Sources:

qdgc 0.1.0 is the trusted, relocatable, pure-SQL core of the QDGC extension family. It encodes longitude and latitude as Extended Quarter Degree Grid Cell codes, decodes their bounds, navigates the prefix hierarchy, reports level metrics, and fills longitude/latitude bounding boxes. It has no PostGIS or compiled-library dependency; geometry, geography, and polygon-fill operations belong to the companion qdgc_postgis extension.

Core Workflow

CREATE EXTENSION qdgc;

-- qdgc_encode uses (longitude, latitude, level).
SELECT qdgc_encode(31.4, 2.7, 5);
-- E031N02ADBAC

-- The h3-style alias reverses the coordinate arguments.
SELECT qdgc_latlng_to_cell(2.7, 31.4, 5);

SELECT *
FROM qdgc_cell_to_bounds('E031N02ADBAC');

SELECT qdgc_cell_to_parent('E031N02ADBAC', 3);
SELECT * FROM qdgc_cell_to_children('E031N02AD', 5);

QDGC hierarchy is encoded directly in the text: a child code begins with its parent code. That makes prefix filtering useful for rollups and descendant lookups:

CREATE TABLE observations (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    qdgc_code text NOT NULL
);

CREATE INDEX observations_qdgc_idx ON observations (qdgc_code);

SELECT qdgc_cell_to_parent(qdgc_code, 3) AS level_3_cell,
       count(*)
FROM observations
GROUP BY 1;

SELECT *
FROM observations
WHERE qdgc_code LIKE 'E031N02AB%';

Bounding Boxes and Level Metrics

The core extension can enumerate rectangular coverage without PostGIS. Pass min_lon > max_lon for a box that crosses the antimeridian.

SELECT qdgc_bbox_cell_count(30.0, 1.0, 32.0, 3.0, 7);

SELECT *
FROM qdgc_bbox_to_cells(30.0, 1.0, 32.0, 3.0, 7);

SELECT qdgc_level_degrees(7);
SELECT qdgc_get_num_cells(7);
SELECT qdgc_average_cell_area(7, 2.0, 'km^2');
SELECT qdgc_version();

qdgc_average_cell_area is a spherical estimate. Use qdgc_cell_area_km2 from qdgc_postgis when a cell-specific WGS84 spheroid measurement is required.

Important Objects

  • qdgc_encode(lon, lat, level) and qdgc_latlng_to_cell(lat, lng, level) create codes; the argument order is intentionally different.
  • qdgc_is_valid_cell, qdgc_get_level, qdgc_cell_to_bounds, qdgc_cell_to_lonlat, and qdgc_cell_to_latlng inspect or decode a code.
  • qdgc_cell_to_parent and qdgc_cell_to_children navigate the four-way prefix hierarchy.
  • qdgc_bbox_to_cells enumerates cells meeting a bounding box, while qdgc_bbox_cell_count calculates the count without materializing the set.
  • qdgc_level_degrees, qdgc_get_num_cells, and qdgc_average_cell_area report grid-level metrics.

Operational Notes

  • Upstream requires PostgreSQL 13 or newer and tests PostgreSQL 13 through 17. PostgreSQL 18 is not part of the published 0.1.0 test matrix.
  • The control file sets trusted = true and relocatable = true. No shared_preload_libraries, LOAD, server restart, or native library is required.
  • Relocatable functions call one another by unqualified name. Install qdgc into a schema on the active search_path; the default public schema satisfies this boundary.
  • Coordinates are longitude/latitude degrees. qdgc_encode takes longitude first, while qdgc_latlng_to_cell takes latitude first.
  • Result cardinality grows by four for every additional child level. Count a bounding-box fill before materializing it, and avoid requesting deep descendants without a deliberate result-size bound.

Table reader