Geohash 是一种将任意精度的纬度和经度坐标编码为文本字符串的系统。Geohash 值是字符串,仅包含从 "0123456789bcdefghjkmnpqrstuvwxyz" 中选择的字符。Geohash is a system for encoding latitude and longitude
coordinates of arbitrary precision into a text string. Geohash
values are strings that contain only characters chosen from
"0123456789bcdefghjkmnpqrstuvwxyz".
本节中的函数用于操作地理哈希值,这为应用程序提供了导入和导出地理哈希数据以及对地理哈希值进行索引和搜索的能力。The functions in this section enable manipulation of geohash values, which provides applications the capabilities of importing and exporting geohash data, and of indexing and searching geohash values.
除非另有说明,本节中的函数对其几何参数的处理方式如下:Unless otherwise specified, functions in this section handle their geometry arguments as follows:
如果任何参数为
NULL,则返回值为NULL。If any argument isNULL, the return value isNULL.如果任何参数无效,将发生错误。If any argument is invalid, an error occurs.
如果任何参数的经度或纬度超出范围,将发生错误:If any argument has a longitude or latitude that is out of range, an error occurs:
如果经度值不在范围(−180, 180]内,将发生
ER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGE错误。If a longitude value is not in the range (−180, 180], anER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGEerror occurs.如果纬度值不在范围[−90, 90]内,将发生
ER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGE错误。If a latitude value is not in the range [−90, 90], anER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGEerror occurs.
显示的范围单位为度。由于浮点运算,确切范围限制会略有偏差。Ranges shown are in degrees. The exact range limits deviate slightly due to floating-point arithmetic.
如果任何点参数没有 SRID 0 或 4326,将会发生
ER_SRS_NOT_FOUND错误。point参数的 SRID 有效性不会被检查。If any point argument does not have SRID 0 or 4326, anER_SRS_NOT_FOUNDerror occurs.pointargument SRID validity is not checked.如果任何 SRID 参数引用了未定义的空间参考系统 (SRS),将会发生
ER_SRS_NOT_FOUND错误。If any SRID argument refers to an undefined spatial reference system (SRS), anER_SRS_NOT_FOUNDerror occurs.如果任何 SRID 参数不在 32 位无符号整数的范围内,将会发生
ER_DATA_OUT_OF_RANGE错误。If any SRID argument is not within the range of a 32-bit unsigned integer, anER_DATA_OUT_OF_RANGEerror occurs.否则,返回值不为
NULL。Otherwise, the return value is non-NULL.
这些 geohash 函数可用:These geohash functions are available:
ST_GeoHash(longitude, latitude, max_length),ST_GeoHash(point, max_length)ST_GeoHash(,longitude,latitude,max_length)ST_GeoHash(point,max_length)返回连接字符集和排序规则中的地理哈希字符串。Returns a geohash string in the connection character set and collation.
对于第一种语法,
longitude必须是范围在 [−180, 180] 内的数字,latitude必须是范围在 [−90, 90] 内的数字。对于第二种语法,需要一个POINT值,其中 X 和 Y 坐标分别在经度和纬度的有效范围内。For the first syntax, thelongitudemust be a number in the range [−180, 180], and thelatitudemust be a number in the range [−90, 90]. For the second syntax, aPOINTvalue is required, where the X and Y coordinates are in the valid ranges for longitude and latitude, respectively.生成的字符串不会超过
max_length个字符,其上限为 100。字符串可能会短于max_length个字符,因为创建 geohash 值的算法会一直进行,直到生成的字符串要么是位置的精确表示,要么达到max_length个字符,以先到者为准。The resulting string is no longer thanmax_lengthcharacters, which has an upper limit of 100. The string might be shorter thanmax_lengthcharacters because the algorithm that creates the geohash value continues until it has created a string that is either an exact representation of the location ormax_lengthcharacters, whichever comes first.ST_GeoHash()按本节介绍中描述的方式处理其参数。ST_GeoHash()handles its arguments as described in the introduction to this section.mysql>
SELECT ST_GeoHash(180,0,10), ST_GeoHash(-180,-90,15);+----------------------+-------------------------+ | ST_GeoHash(180,0,10) | ST_GeoHash(-180,-90,15) | +----------------------+-------------------------+ | xbpbpbpbpb | 000000000000000 | +----------------------+-------------------------+ST_LatFromGeoHash(geohash_str)从地理哈希字符串值返回纬度,作为范围在 [−90, 90] 的双精度数字。Returns the latitude from a geohash string value, as a double-precision number in the range [−90, 90].
ST_LatFromGeoHash()解码函数从geohash_str参数中读取不超过 433 个字符。这表示坐标值内部表示中信息的上限。第 433 个字符之后的字符将被忽略,即使它们本身是非法的并且会产生错误。TheST_LatFromGeoHash()decoding function reads no more than 433 characters from thegeohash_strargument. That represents the upper limit on information in the internal representation of coordinate values. Characters past the 433rd are ignored, even if they are otherwise illegal and produce an error.ST_LatFromGeoHash()按本节介绍中描述的方式处理其参数。ST_LatFromGeoHash()handles its arguments as described in the introduction to this section.mysql>
SELECT ST_LatFromGeoHash(ST_GeoHash(45,-20,10));+------------------------------------------+ | ST_LatFromGeoHash(ST_GeoHash(45,-20,10)) | +------------------------------------------+ | -20 | +------------------------------------------+ST_LongFromGeoHash(geohash_str)从地理哈希字符串值返回经度,作为范围在 [−180, 180] 的双精度数字。Returns the longitude from a geohash string value, as a double-precision number in the range [−180, 180].
在
ST_LatFromGeoHash()描述中关于从geohash_str参数处理中最大字符数的说明,也同样适用于ST_LongFromGeoHash()。The remarks in the description ofST_LatFromGeoHash()regarding the maximum number of characters processed from thegeohash_strargument also apply toST_LongFromGeoHash().ST_LongFromGeoHash()按照本节介绍中描述的方式处理其参数。ST_LongFromGeoHash()handles its arguments as described in the introduction to this section.mysql>
SELECT ST_LongFromGeoHash(ST_GeoHash(45,-20,10));+-------------------------------------------+ | ST_LongFromGeoHash(ST_GeoHash(45,-20,10)) | +-------------------------------------------+ | 45 | +-------------------------------------------+ST_PointFromGeoHash(geohash_str,srid)返回一个包含解码后 geohash 值的
POINT值,给定一个 geohash 字符串值。Returns aPOINTvalue containing the decoded geohash value, given a geohash string value.该点的 X 和 Y 坐标分别是范围在 [−180, 180] 的经度和范围在 [−90, 90] 的纬度。The X and Y coordinates of the point are the longitude in the range [−180, 180] and the latitude in the range [−90, 90], respectively.
srid参数是一个 32 位无符号整数。Thesridargument is an 32-bit unsigned integer.在
ST_LatFromGeoHash()描述中关于从geohash_str参数处理的最大字符数的说明,也适用于ST_PointFromGeoHash()。The remarks in the description ofST_LatFromGeoHash()regarding the maximum number of characters processed from thegeohash_strargument also apply toST_PointFromGeoHash().ST_PointFromGeoHash()按本节介绍中描述的方式处理其参数。ST_PointFromGeoHash()handles its arguments as described in the introduction to this section.mysql>
SET @gh = ST_GeoHash(45,-20,10);mysql>SELECT ST_AsText(ST_PointFromGeoHash(@gh,0));+---------------------------------------+ | ST_AsText(ST_PointFromGeoHash(@gh,0)) | +---------------------------------------+ | POINT(45 -20) | +---------------------------------------+