Purpose
GeoHash is a geocoding system that converts the latitude and longitude coordinates of a geographic location into a string of letters and digits. In other words, GeoHash provides a text representation of geographic locations. GeoHash does not point to a precise point location but defines a rectangular area, where all points are assigned the same GeoHash value. GeoHash supports approximate description and quick processing of geographic locations while protecting personal location privacy. The core idea of GeoHash is dichotomy. GeoHash considers the Earth as a two-dimensional surface, performs recursive dichotomy for the latitudes and longitudes to obtain smaller subblocks, and encodes each sublock. A longer GeoHash string means that the surface of the Earth is subdivided more times, resulting in a smaller rectangular subblock, which means higher location precision.
In this topic, _ST_GeoHash() converts a geometry object into a GeoHash string value.
Syntax
_ST_GeoHash(geometry, [precision])
Parameters
geometry: the geometry object to be converted into a GeoHash value.precision: the calculation precision of GeoHash, namely, the length of the returned GeoHash string. The value can be an integer. This parameter is optional. A greater value indicates a more precise geographic range.- If you do not specify
precisionor setprecisionto0, the precision of the returned GeoHash value is calculated based on the input geometry object, which is the smallest precision that can contain the subblocks of the input geometry object. For a point, a GeoHash value with 20 characters is returned. - If you specify
precision, the returned GeoHash value is of the specified precision. For a non-point object, the start point for calculation is the central point in the framework of the geometry object.
- If you do not specify
Examples
Convert a point into a short GeoHash string that consists of letters and digits, 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 setConvert a polygon into a short GeoHash string that consists of letters and digits, with a precision of 10.
ST_GEOMFROMTEXT()accepts a Well-Known Text (WKT) string and returns a geometry object. In this example, the input argument isPOLYGON((10 10, 20 20, 10 30, 0 20, 10 10)), which defines the coordinates of five vertexes of a polygon. The coordinates of the last vertex are the same as those of the first vertex, because the polygon is a closed shape.10is the precision, indicating the length of the returned GeoHash string.- The query result column is named
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
References
For information about
ST_GEOMFROMTEXT, see Functions that create geometry values.For information about
POLYGON, see Spatial data formats.