pg_geohash

pg_geohash

pg_geohash : Handle geohash based functionality for spatial coordinates

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1590
pg_geohash
pg_geohash
1.0
GIS
MIT
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
See Also
postgis
h3
q3c
pg_polyline
postgis_topology
postgis_raster
postgis_sfcgal
postgis_tiger_geocoder

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
1.0
18
17
16
15
14
pg_geohash-
RPM
PIGSTY
1.0
18
17
16
15
14
pg_geohash_$v-
DEB
PIGSTY
1.0
18
17
16
15
14
postgresql-$v-pg-geohash-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el8.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el10.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el10.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0

Source

pig build pkg pg_geohash;		# 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_geohash;		# install via package name, for the active PG version

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

Create this extension with:

CREATE EXTENSION pg_geohash;

Usage

pg_geohash: Geohash functions for PostgreSQL

C-based geohash functions for PostgreSQL (also supports HAWQ and Greenplum). Based on the libgeohash C library.

Background on geohash: Wikipedia: Geohash

Functions

Encode latitude/longitude to a geohash string with specified precision:

SELECT LAT_LON_TO_GEOHASH_WITH_LEN(latitude, longitude, 5) AS geohash;

Encode latitude/longitude to a full-precision geohash:

SELECT LAT_LON_TO_GEOHASH(latitude, longitude) AS geohash;

Decode a geohash back to latitude/longitude:

SELECT GEOHASH_TO_LAT_LON('dp3w7') AS lat_lon;

Example

Compute geohash-based aggregates using 5-character precision (~2.4km x 4.9km cells):

SELECT LAT_LON_TO_GEOHASH_WITH_LEN(latitude, longitude, 5) AS geohash,
       COUNT(*)
FROM crimes
GROUP BY 1
ORDER BY 2 DESC
LIMIT 10;
 geohash | count
---------+-------
 dp3w7   | 72404
 dp3tt   | 70713
 dp3tw   | 63642
 dp3wm   | 62332
 dp3wk   | 56467

Recover coordinates from a geohash:

SELECT location,
       GEOHASH_TO_LAT_LON(LAT_LON_TO_GEOHASH(latitude, longitude))
FROM crimes
LIMIT 5;
Last updated on