Purpose
GeoHash is a geocode system that converts latitude and longitude coordinates into a string format (a combination of letters and numbers), providing a textual representation for a geographic location. A GeoHash value does not point to a precise point but represents a rectangular area within which all points receive the same value. This characteristic enables GeoHash to support approximate location descriptions and fast processing while protecting individual privacy. The core principle of GeoHash is interval bisection: it treats the Earth's surface as a two-dimensional plane, recursively splits the longitude and latitude into smaller sub-blocks, and encodes each sub-block. Longer GeoHash values result from more divisions of longitude and latitude. More divisions mean smaller sub-blocks and higher positional accuracy.
In this topic, the _ST_GeoHash function is used to convert a geometry object into a geohash value represented as a string.
Syntax
_ST_GeoHash(geometry, [precision])
Parameters
geometry: the geometry object to be converted into a GeoHash value.precision: Optional. The precision of the GeoHash calculation, which is the length of the returned GeoHash string. It accepts an integer (int) value. A higher value indicates a more precise geographic range.- If
precisionis not specified or is set to 0, the precision of the returned GeoHash will be calculated based on the input geometry. It is the minimum precision of a sub-block that can contain the input geometry. A Point will return a GeoHash with a precision of 20 characters. - If
precisionis specified, the returned GeoHash will have the specifiedprecision. For non-points, the calculation starts from the center of the geometry's bounding box.
- If
Examples
Converts a point into a short string of letters and digits, the GeoHash value, with a precision of 20.
obclient [test]> SELECT _ST_GeoHash(Point(-126.002,48.348), 20) AS geohash;The return result is as follows:
+----------------------+ | geohash | +----------------------+ | c0w7hc2ps87jfvwhesrb | +----------------------+ 1 row in setConverts a polygon into a short string of letters and digits, called a GeoHash value, with a precision of 10.
- The
ST_GEOMFROMTEXTfunction takes a Well-Known Text (WKT) string and returns a geometric object. In this example,POLYGON((10 10, 20 20, 10 30, 0 20, 10 10))is used as the input parameter. This string defines the coordinates of five vertices for a polygon; the last coordinate is the same as the first, making it a closed polygon. - The 10 is the precision parameter for GeoHash, indicating how long the returned GeoHash string should be.
- It names the query result column
geohash.
obclient [test]> SELECT _ST_GeoHash(ST_GEOMFROMTEXT('POLYGON((10 10, 20 20, 10 30, 0 20, 10 10))'), 10) AS geohash;The return result is as follows:
+------------+ | geohash | +------------+ | s5x1g8cu2y | +------------+ 1 row in set- The
References
For more information about
ST_GEOMFROMTEXT, see Create a geometric value function.For more information about
POLYGON, see Spatial data formats.