文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 91%

14.16.10 空间 Geohash 函数14.16.10 Spatial Geohash Functions

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 is NULL, the return value is NULL.

  • 如果任何参数无效,将发生错误。If any argument is invalid, an error occurs.

  • 如果任何参数的经度或纬度超出范围,将发生错误:If any argument has a longitude or latitude that is out of range, an error 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, an ER_SRS_NOT_FOUND error occurs. point argument SRID validity is not checked.

  • 如果任何 SRID 参数引用了未定义的空间参考系统 (SRS),将会发生 ER_SRS_NOT_FOUND 错误。If any SRID argument refers to an undefined spatial reference system (SRS), an ER_SRS_NOT_FOUND error occurs.

  • 如果任何 SRID 参数不在 32 位无符号整数的范围内,将会发生 ER_DATA_OUT_OF_RANGE 错误。If any SRID argument is not within the range of a 32-bit unsigned integer, an ER_DATA_OUT_OF_RANGE error 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, the longitude must be a number in the range [−180, 180], and the latitude must be a number in the range [−90, 90]. For the second syntax, a POINT value 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 than max_length characters, which has an upper limit of 100. The string might be shorter than max_length characters because the algorithm that creates the geohash value continues until it has created a string that is either an exact representation of the location or max_length characters, 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 个字符之后的字符将被忽略,即使它们本身是非法的并且会产生错误。The ST_LatFromGeoHash() decoding function reads no more than 433 characters from the geohash_str argument. 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 of ST_LatFromGeoHash() regarding the maximum number of characters processed from the geohash_str argument also apply to ST_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 a POINT value 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 位无符号整数。The srid argument is an 32-bit unsigned integer.

    在 ST_LatFromGeoHash() 描述中关于从 geohash_str 参数处理的最大字符数的说明,也适用于 ST_PointFromGeoHash()。The remarks in the description of ST_LatFromGeoHash() regarding the maximum number of characters processed from the geohash_str argument also apply to ST_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)                         |
    +---------------------------------------+