ST_GeoHash

更新时间:
复制 MD 格式

ST_GeoHash converts a geometry object into a Geohash string. The length of the returned string controls precision — more characters mean a smaller, more precise area.

Syntax

text ST_GeoHash(geometry geom, integer maxchars)

Parameters

ParameterDescription
geomThe geometry object to encode as a Geohash string.
maxcharsThe number of characters in the returned Geohash string. Omit this parameter to return a string at full precision.

Usage notes

Non-point geometry

For geometry objects other than points, ST_GeoHash calculates the Geohash from the center of the bounding box of the geometry.

Supported types

ST_GeoHash supports circular strings and curves.

ST_GeoHash only works with geometry objects in a longitude- and latitude-based coordinate system. Geometry objects that are not in longitude- and latitude-based geographic systems are not supported.

Examples

Return a Geohash string at full precision

SELECT ST_GeoHash(ST_GeomFromText('POINT(116 40)', 4326));

Output:

      st_geohash
----------------------
 wx47x9u8gumnhzp791zb
(1 row)

Return a Geohash string at a custom precision

Specify maxchars to truncate the Geohash to a fixed length. Shorter strings represent larger areas.

SELECT ST_GeoHash(ST_GeomFromText('POINT(116 40)', 4326), 3);

Output:

 st_geohash
------------
 wx4
(1 row)

What's next

  • ST_GeomFromGeoHash — convert a Geohash string back to a geometry object