本节介绍用于在 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 geometryg. The object string has the connection character set and collation.如果任何参数为
NULL,则返回值为NULL。如果任何非NULL参数无效,将会发生错误。If any argument isNULL, the return value isNULL. If any non-NULLargument 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 ifoptionsis not specified.1 在输出中添加一个边界框。Add a bounding box to the output. 2 在输出中添加短格式 CRS URN。默认格式为短格式( EPSG:)。Add a short-format CRS URN to the output. The default format is a short format (sridEPSG:).srid4 添加长格式 CRS URN( urn:ogc:def:crs:EPSG::)。此标志会覆盖标志 2。例如,选项值为 5 和 7 的意思相同(添加边界框和长格式 CRS URN)。Add a long-format CRS URN (sridurn:ogc:def:crs:EPSG::). 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).sridmysql>
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 stringstrrepresenting a GeoJSON object and returns a geometry.如果任何参数为
NULL,则返回值为NULL。如果任何非NULL参数无效,将会发生错误。If any argument isNULL, the return value isNULL. If any non-NULLargument 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 permittedoptionsvalues.选项值Option Value 意义Meaning 1 拒绝该文档并产生一个错误。如果未指定 options,这是默认行为。Reject the document and produce an error. This is the default ifoptionsis not specified.2, 3, 4 接受文档并去除高维坐标的坐标信息。Accept the document and strip off the coordinates for higher coordinate dimensions. options值 2、3 和 4 目前会产生相同的效果。如果将来支持坐标维度大于 2 的几何体,你可以预期这些值会产生不同的效果。optionsvalues 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。Thesridargument, 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错误。Ifsridrefers to an undefined spatial reference system (SRS), anER_SRS_NOT_FOUNDerror occurs.对于地理 SRS 几何参数,如果任何参数的经度或纬度超出范围,将会发生错误:For geographic SRS geometry arguments, if any argument has a longitude or latitude that is out of range, an error occurs:
如果经度值不在 (−180, 180] 范围内,将会发生
ER_LONGITUDE_OUT_OF_RANGE错误。If a longitude value is not in the range (−180, 180], anER_LONGITUDE_OUT_OF_RANGEerror occurs.如果纬度值不在 [−90, 90] 范围内,将会发生
ER_LATITUDE_OUT_OF_RANGE错误。If a latitude value is not in the range [−90, 90], anER_LATITUDE_OUT_OF_RANGEerror 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::和sridEPSG:命名空间中的命名 CRS URN,但不能解析以链接对象形式给出的 CRS。此外,sridurn:ogc:def:crs:OGC:1.3:CRS84被识别为 SRID 4326。如果对象具有无法识别的 CRS,将会发生错误,但有一个例外情况:如果提供了可选的srid参数,则即使 CRS 无效也会被忽略。GeoJSON geometry, feature, and feature collection objects may have acrsproperty. The parsing function parses named CRS URNs in theurn:ogc:def:crs:EPSG::andsridEPSG:namespaces, but not CRSs given as link objects. Also,sridurn:ogc:def:crs:OGC:1.3:CRS84is recognized as SRID 4326. If an object has a CRS that is not understood, an error occurs, with the exception that if the optionalsridargument is given, any CRS is ignored even if it is invalid.如果在 GeoJSON 文档的较低层级中发现一个指定了与顶层对象 SRID 不同的
crs成员,则会发生ER_INVALID_GEOJSON_CRS_NOT_TOP_LEVEL错误。If acrsmember that specifies an SRID different from the top-level object SRID is found at a lower level of the GeoJSON document, anER_INVALID_GEOJSON_CRS_NOT_TOP_LEVELerror occurs.如 GeoJSON 规范中所述,解析 GeoJSON 输入的
type成员 (Point、LineString等) 时是区分大小写的。该规范对其他解析的大小写敏感性未作说明,而在 MySQL 中,这些解析是不区分大小写的。As specified in the GeoJSON specification, parsing is case-sensitive for thetypemember 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) | +-------------------------------------------------+