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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

14.16.11 空间 GeoJSON 函数14.16.11 Spatial GeoJSON Functions

本节介绍用于在 GeoJSON 文档与空间值之间进行转换的函数。GeoJSON 是一种用于编码几何/地理特征的开放标准。更多信息,请参见 http://geojson.org。此处讨论的函数遵循 GeoJSON 规范修订版 1.0。This section describes functions for converting between GeoJSON documents and spatial values. GeoJSON is an open standard for encoding geometric/geographical features. For more information, see http://geojson.org. The functions discussed here follow GeoJSON specification revision 1.0.

GeoJSON 支持与 MySQL 相同的几何/地理数据类型。Feature 和 FeatureCollection 对象不被支持,除非从中提取几何对象。CRS 支持仅限于标识 SRID 的值。GeoJSON supports the same geometric/geographic data types that MySQL supports. Feature and FeatureCollection objects are not supported, except that geometry objects are extracted from them. CRS support is limited to values that identify an SRID.

MySQL 还支持原生的 JSON 数据类型和一组 SQL 函数,用于对 JSON 值进行操作。更多信息,请参见 第 13.5 节,“JSON 数据类型”,以及 第 14.17 节,“JSON 函数”。MySQL also supports a native JSON data type and a set of SQL functions to enable operations on JSON values. For more information, see Section 13.5, “The JSON Data Type”, and Section 14.17, “JSON Functions”.

  • ST_AsGeoJSON(g [, max_dec_digits [, options]])

    从几何对象 g 生成一个 GeoJSON 对象。该对象字符串使用连接的字符集和排序规则。Generates a GeoJSON object from the geometry g. The object string has the connection character set and collation.

    如果任何参数为 NULL,则返回值为 NULL。如果任何非 NULL 参数无效,将会发生错误。If any argument is NULL, the return value is NULL. If any non-NULL argument is invalid, an error occurs.

    max_dec_digits,如果指定,会限制坐标的小数位数并导致输出四舍五入。如果未指定,此参数默认为其最大值 232 − 1。最小值为 0。max_dec_digits, if specified, limits the number of decimal digits for coordinates and causes rounding of output. If not specified, this argument defaults to its maximum value of 232 − 1. The minimum is 0.

    options,如果指定,是一个位掩码。下表显示了允许的标志值。如果几何参数的 SRID 为 0,即使请求 CRS 对象的标志值,也不会生成 CRS 对象。options, if specified, is a bitmask. The following table shows the permitted flag values. If the geometry argument has an SRID of 0, no CRS object is produced even for those flag values that request one.

    标志值Flag Value 意义Meaning
    0 没有选项。如果未指定options,则使用此默认值。No options. This is the default if options is not specified.
    1 在输出中添加一个边界框。Add a bounding box to the output.
    2 在输出中添加短格式 CRS URN。默认格式为短格式(EPSG:srid)。Add a short-format CRS URN to the output. The default format is a short format (EPSG:srid).
    4 添加长格式 CRS URN(urn:ogc:def:crs:EPSG::srid)。此标志会覆盖标志 2。例如,选项值为 5 和 7 的意思相同(添加边界框和长格式 CRS URN)。Add a long-format CRS URN (urn:ogc:def:crs:EPSG::srid). This flag overrides flag 2. For example, option values of 5 and 7 mean the same (add a bounding box and a long-format CRS URN).
    mysql> SELECT ST_AsGeoJSON(ST_GeomFromText('POINT(11.11111 12.22222)'),2);
    +-------------------------------------------------------------+
    | ST_AsGeoJSON(ST_GeomFromText('POINT(11.11111 12.22222)'),2) |
    +-------------------------------------------------------------+
    | {"type": "Point", "coordinates": [11.11, 12.22]}            |
    +-------------------------------------------------------------+
    
  • ST_GeomFromGeoJSON(str [, options [, srid]])

    解析表示 GeoJSON 对象的字符串 str 并返回一个几何对象。Parses a string str representing a GeoJSON object and returns a geometry.

    如果任何参数为 NULL,则返回值为 NULL。如果任何非 NULL 参数无效,将会发生错误。If any argument is NULL, the return value is NULL. If any non-NULL argument is invalid, an error occurs.

    options,如果提供,描述了如何处理包含高于 2 维坐标的几何图形的 GeoJSON 文档。下表显示了允许的options值。options, if given, describes how to handle GeoJSON documents that contain geometries with coordinate dimensions higher than 2. The following table shows the permitted options values.

    选项值Option Value 意义Meaning
    1 拒绝该文档并产生一个错误。如果未指定options,这是默认行为。Reject the document and produce an error. This is the default if options is not specified.
    2, 3, 4 接受文档并去除高维坐标的坐标信息。Accept the document and strip off the coordinates for higher coordinate dimensions.

    options 值 2、3 和 4 目前会产生相同的效果。如果将来支持坐标维度大于 2 的几何体,你可以预期这些值会产生不同的效果。options values of 2, 3, and 4 currently produce the same effect. If geometries with coordinate dimensions higher than 2 are supported in the future, you can expect these values to produce different effects.

    如果提供了 srid 参数,它必须是一个 32 位无符号整数。如果未提供,则几何体返回值的 SRID 为 4326。The srid argument, if given, must be a 32-bit unsigned integer. If not given, the geometry return value has an SRID of 4326.

    如果 srid 指向未定义的空间参考系统(SRS),将会发生 ER_SRS_NOT_FOUND 错误。If srid refers to an undefined spatial reference system (SRS), an ER_SRS_NOT_FOUND error occurs.

    对于地理 SRS 几何参数,如果任何参数的经度或纬度超出范围,将会发生错误:For geographic SRS geometry arguments, if any argument has a longitude or latitude that is out of range, an error occurs:

    显示的范围以度为单位。如果 SRS 使用其他单位,则范围使用其单位对应的数值。由于浮点运算,精确的范围限制会有轻微偏差。Ranges shown are in degrees. If an SRS uses another unit, the range uses the corresponding values in its unit. The exact range limits deviate slightly due to floating-point arithmetic.

    GeoJSON 几何对象、要素和要素集合对象可以有一个 crs 属性。解析函数可以解析 urn:ogc:def:crs:EPSG::srid 和 EPSG:srid 命名空间中的命名 CRS URN,但不能解析以链接对象形式给出的 CRS。此外,urn:ogc:def:crs:OGC:1.3:CRS84 被识别为 SRID 4326。如果对象具有无法识别的 CRS,将会发生错误,但有一个例外情况:如果提供了可选的 srid 参数,则即使 CRS 无效也会被忽略。GeoJSON geometry, feature, and feature collection objects may have a crs property. The parsing function parses named CRS URNs in the urn:ogc:def:crs:EPSG::srid and EPSG:srid namespaces, but not CRSs given as link objects. Also, urn:ogc:def:crs:OGC:1.3:CRS84 is recognized as SRID 4326. If an object has a CRS that is not understood, an error occurs, with the exception that if the optional srid argument is given, any CRS is ignored even if it is invalid.

    如果在 GeoJSON 文档的较低层级中发现一个指定了与顶层对象 SRID 不同的 crs 成员,则会发生 ER_INVALID_GEOJSON_CRS_NOT_TOP_LEVEL 错误。If a crs member that specifies an SRID different from the top-level object SRID is found at a lower level of the GeoJSON document, an ER_INVALID_GEOJSON_CRS_NOT_TOP_LEVEL error occurs.

    如 GeoJSON 规范中所述,解析 GeoJSON 输入的 type 成员 (Point、LineString 等) 时是区分大小写的。该规范对其他解析的大小写敏感性未作说明,而在 MySQL 中,这些解析是不区分大小写的。As specified in the GeoJSON specification, parsing is case-sensitive for the type member of the GeoJSON input (Point, LineString, and so forth). The specification is silent regarding case sensitivity for other parsing, which in MySQL is not case-sensitive.

    此示例显示了一个简单 GeoJSON 对象的解析结果。请注意,坐标的顺序取决于使用的 SRID。This example shows the parsing result for a simple GeoJSON object. Observe that the order of coordinates depends on the SRID used.

    mysql> SET @json = '{ "type": "Point", "coordinates": [102.0, 0.0]}';
    mysql> SELECT ST_AsText(ST_GeomFromGeoJSON(@json));
    +--------------------------------------+
    | ST_AsText(ST_GeomFromGeoJSON(@json)) |
    +--------------------------------------+
    | POINT(0 102)                         |
    +--------------------------------------+
    mysql> SELECT ST_SRID(ST_GeomFromGeoJSON(@json));
    +------------------------------------+
    | ST_SRID(ST_GeomFromGeoJSON(@json)) |
    +------------------------------------+
    |                               4326 |
    +------------------------------------+
    mysql> SELECT ST_AsText(ST_SRID(ST_GeomFromGeoJSON(@json),0));
    +-------------------------------------------------+
    | ST_AsText(ST_SRID(ST_GeomFromGeoJSON(@json),0)) |
    +-------------------------------------------------+
    | POINT(102 0)                                    |
    +-------------------------------------------------+