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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

14.16.13 空间便利函数14.16.13 Spatial Convenience Functions

本节中的函数提供对几何值的便利操作。The functions in this section provide convenience operations on geometry 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.

  • 如果任何几何参数不是语法上格式正确的几何体,则会发生ER_GIS_INVALID_DATA 错误。If any geometry argument is not a syntactically well-formed geometry, an ER_GIS_INVALID_DATA error occurs.

  • 如果任何几何参数是在未定义空间参考系统(SRS)中的语法上良构的几何,将会发生ER_SRS_NOT_FOUND错误。If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), an ER_SRS_NOT_FOUND error occurs.

  • 对于接受多个几何参数的函数,如果这些参数不在相同的 SRS 中,会出现ER_GIS_DIFFERENT_SRIDS错误。For functions that take multiple geometry arguments, if those arguments are not in the same SRS, an ER_GIS_DIFFERENT_SRIDS error occurs.

  • 否则,返回值不为 NULL。Otherwise, the return value is non-NULL.

这些便利函数包括:These convenience functions are available:

  • ST_Distance_Sphere(g1, g2 [, radius])

    返回球体上Point或MultiPoint参数之间的最小球面距离,单位为米。(对于一般用途的距离计算,请参见ST_Distance()函数。)可选的radius参数应以米为单位。Returns the minimum spherical distance between Point or MultiPoint arguments on a sphere, in meters. (For general-purpose distance calculations, see the ST_Distance() function.) The optional radius argument should be given in meters.

    如果两个几何参数都是有效的笛卡尔 Point 或 MultiPoint 值(SRID 为 0),则返回值是在提供的半径下两几何图形之间的最短球面距离。如果未提供,则默认半径为 6,370,986 米,Point 的 X 和 Y 坐标分别解释为经度和纬度,以度为单位。If both geometry parameters are valid Cartesian Point or MultiPoint values in SRID 0, the return value is shortest distance between the two geometries on a sphere with the provided radius. If omitted, the default radius is 6,370,986 meters, Point X and Y coordinates are interpreted as longitude and latitude, respectively, in degrees.

    如果两个几何参数都是有效的 Point 或 MultiPoint 值,且在地理空间参考系统(SRS)中,则返回值是在提供的半径下两几何图形之间的最短球面距离。如果未提供,则默认半径等于平均半径,定义为 (2a + b)/3,其中 a 为 SRS 的长半轴,b 为 SRS 的短半轴。If both geometry parameters are valid Point or MultiPoint values in a geographic spatial reference system (SRS), the return value is the shortest distance between the two geometries on a sphere with the provided radius. If omitted, the default radius is equal to the mean radius, defined as (2a+b)/3, where a is the semi-major axis and b is the semi-minor axis of the SRS.

    ST_Distance_Sphere() 根据本节介绍处理其参数,但有以下例外:ST_Distance_Sphere() handles its arguments as described in the introduction to this section, with these exceptions:

    • 支持的几何参数组合为 Point 和 Point,或 Point 和 MultiPoint(参数顺序不限)。如果至少有一个几何体既不是 Point 也不是 MultiPoint,并且其 SRID 为 0,则会发生 ER_NOT_IMPLEMENTED_FOR_CARTESIAN_SRS 错误。如果至少有一个几何体既不是 Point 也不是 MultiPoint,并且其 SRID 对应于地理 SRS,则会发生 ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS 错误。如果任何几何体对应于投影 SRS,则会发生 ER_NOT_IMPLEMENTED_FOR_PROJECTED_SRS 错误。Supported geometry argument combinations are Point and Point, or Point and MultiPoint (in any argument order). If at least one of the geometries is neither Point nor MultiPoint, and its SRID is 0, an ER_NOT_IMPLEMENTED_FOR_CARTESIAN_SRS error occurs. If at least one of the geometries is neither Point nor MultiPoint, and its SRID refers to a geographic SRS, an ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS error occurs. If any geometry refers to a projected SRS, an ER_NOT_IMPLEMENTED_FOR_PROJECTED_SRS error occurs.

    • 如果任何参数的经度或纬度超出范围,将发生错误: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.

    • 如果 radius 参数存在但不是正数,将会发生 ER_NONPOSITIVE_RADIUS 错误。If the radius argument is present but not positive, an ER_NONPOSITIVE_RADIUS error occurs.

    • 如果距离超过双精度数的范围,将会发生 ER_STD_OVERFLOW_ERROR 错误。If the distance exceeds the range of a double-precision number, an ER_STD_OVERFLOW_ERROR error occurs.

    mysql> SET @pt1 = ST_GeomFromText('POINT(0 0)');
    mysql> SET @pt2 = ST_GeomFromText('POINT(180 0)');
    mysql> SELECT ST_Distance_Sphere(@pt1, @pt2);
    +--------------------------------+
    | ST_Distance_Sphere(@pt1, @pt2) |
    +--------------------------------+
    |             20015042.813723423 |
    +--------------------------------+
    
  • ST_IsValid(g)

    如果参数在几何上有效,则返回 1;如果参数在几何上无效,则返回 0。几何有效性由 OGC 规范定义。Returns 1 if the argument is geometrically valid, 0 if the argument is not geometrically valid. Geometry validity is defined by the OGC specification.

    唯一有效的空几何表示为空几何集合值的形式。ST_IsValid() 在这种情况下返回 1。MySQL 不支持 GIS EMPTY 值,例如 POINT EMPTY。The only valid empty geometry is represented in the form of an empty geometry collection value. ST_IsValid() returns 1 in this case. MySQL does not support GIS EMPTY values such as POINT EMPTY.

    ST_IsValid() 按本节介绍中的描述处理其参数,但有以下例外:ST_IsValid() handles its arguments as described in the introduction to this section, with this exception:

    • 如果几何图形具有地理空间参考系统 (SRS),且经度或纬度超出范围,将会发生错误:If the geometry has a geographic SRS with 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.

    mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0,-0.00 0,0.0 0)');
    mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 0, 1 1)');
    mysql> SELECT ST_IsValid(@ls1);
    +------------------+
    | ST_IsValid(@ls1) |
    +------------------+
    |                0 |
    +------------------+
    mysql> SELECT ST_IsValid(@ls2);
    +------------------+
    | ST_IsValid(@ls2) |
    +------------------+
    |                1 |
    +------------------+
    
  • ST_MakeEnvelope(pt1, pt2)

    返回围绕两个点形成包络的矩形,可以是Point、LineString或Polygon。Returns the rectangle that forms the envelope around two points, as a Point, LineString, or Polygon.

    计算是使用笛卡尔坐标系进行的,而不是在球体、椭球体或地球上进行的。Calculations are done using the Cartesian coordinate system rather than on a sphere, spheroid, or on earth.

    给定两个点 pt1 和 pt2,ST_MakeEnvelope() 会在一个抽象平面上创建如下的结果几何体:Given two points pt1 and pt2, ST_MakeEnvelope() creates the result geometry on an abstract plane like this:

    • 如果 pt1 和 pt2 相等,结果就是点 pt1。If pt1 and pt2 are equal, the result is the point pt1.

    • 否则,如果 (pt1, pt2) 是一条垂直或水平的线段,结果就是线段 (pt1, pt2)。Otherwise, if (pt1, pt2) is a vertical or horizontal line segment, the result is the line segment (pt1, pt2).

    • 否则,结果是一个多边形,使用 pt1 和 pt2 作为对角点。Otherwise, the result is a polygon using pt1 and pt2 as diagonal points.

    结果几何的 SRID 为 0。The result geometry has an SRID of 0.

    ST_MakeEnvelope() 按本节介绍中描述的方式处理其参数,但有以下例外:ST_MakeEnvelope() handles its arguments as described in the introduction to this section, with these exceptions:

    mysql> SET @pt1 = ST_GeomFromText('POINT(0 0)');
    mysql> SET @pt2 = ST_GeomFromText('POINT(1 1)');
    mysql> SELECT ST_AsText(ST_MakeEnvelope(@pt1, @pt2));
    +----------------------------------------+
    | ST_AsText(ST_MakeEnvelope(@pt1, @pt2)) |
    +----------------------------------------+
    | POLYGON((0 0,1 0,1 1,0 1,0 0))         |
    +----------------------------------------+
    
  • ST_Simplify(g, max_distance)

    使用道格拉斯-普克算法简化几何图形,并返回相同类型的简化值。Simplifies a geometry using the Douglas-Peucker algorithm and returns a simplified value of the same type.

    几何图形可以是任何几何类型,尽管道格拉斯-普克算法可能无法实际处理每种类型。几何集合通过将其组成部分逐个传递给简化算法进行处理,返回的几何图形会作为结果放入几何集合中。The geometry may be any geometry type, although the Douglas-Peucker algorithm may not actually process every type. A geometry collection is processed by giving its components one by one to the simplification algorithm, and the returned geometries are put into a geometry collection as result.

    max_distance 参数是一个顶点到其他线段的距离(以输入坐标的单位为准),用于判断是否移除该顶点。距离简化后的折线在此范围内的顶点将被移除。The max_distance argument is the distance (in units of the input coordinates) of a vertex to other segments to be removed. Vertices within this distance of the simplified linestring are removed.

    根据 Boost.Geometry,几何图形可能因简化过程而变得无效,并且该过程可能生成自相交。要检查结果的有效性,请将其传递给 ST_IsValid()。According to Boost.Geometry, geometries might become invalid as a result of the simplification process, and the process might create self-intersections. To check the validity of the result, pass it to ST_IsValid().

    ST_Simplify() 会像本节介绍中所述那样处理其参数,唯一的例外是:ST_Simplify() handles its arguments as described in the introduction to this section, with this exception:

    • 如果 max_distance 参数不是正数,或者是 NaN,将会发生 ER_WRONG_ARGUMENTS 错误。If the max_distance argument is not positive, or is NaN, an ER_WRONG_ARGUMENTS error occurs.

    mysql> SET @g = ST_GeomFromText('LINESTRING(0 0,0 1,1 1,1 2,2 2,2 3,3 3)');
    mysql> SELECT ST_AsText(ST_Simplify(@g, 0.5));
    +---------------------------------+
    | ST_AsText(ST_Simplify(@g, 0.5)) |
    +---------------------------------+
    | LINESTRING(0 0,0 1,1 1,2 3,3 3) |
    +---------------------------------+
    mysql> SELECT ST_AsText(ST_Simplify(@g, 1.0));
    +---------------------------------+
    | ST_AsText(ST_Simplify(@g, 1.0)) |
    +---------------------------------+
    | LINESTRING(0 0,3 3)             |
    +---------------------------------+
    
  • ST_Validate(g)

    根据 OGC 规格验证几何图形。一个几何图形在语法上可以是良构的(WKB 值加 SRID),但在几何上可能无效。例如,这个多边形在几何上是无效的:POLYGON((0 0, 0 0, 0 0, 0 0, 0 0))Validates a geometry according to the OGC specification. A geometry can be syntactically well-formed (WKB value plus SRID) but geometrically invalid. For example, this polygon is geometrically invalid: POLYGON((0 0, 0 0, 0 0, 0 0, 0 0))

    ST_Validate() 如果几何是语法正确且几何上有效,则返回该几何;如果参数在语法上不正确、几何上无效或为 NULL,则返回 NULL。ST_Validate() returns the geometry if it is syntactically well-formed and is geometrically valid, NULL if the argument is not syntactically well-formed or is not geometrically valid or is NULL.

    ST_Validate() 可用于过滤无效几何数据,尽管会产生一定的开销。对于需要更精确结果且不受无效数据干扰的应用来说,这种开销可能是值得的。ST_Validate() can be used to filter out invalid geometry data, although at a cost. For applications that require more precise results not tainted by invalid data, this penalty may be worthwhile.

    如果几何参数有效,则按原样返回,唯一的例外是,如果输入的Polygon或MultiPolygon有顺时针环,则在检查有效性之前会将这些环反向。如果几何是有效的,则返回反向环的值。If the geometry argument is valid, it is returned as is, except that if an input Polygon or MultiPolygon has clockwise rings, those rings are reversed before checking for validity. If the geometry is valid, the value with the reversed rings is returned.

    唯一有效的空几何以空几何集合值的形式表示。在这种情况下,ST_Validate()会直接返回它,而不进行进一步检查。The only valid empty geometry is represented in the form of an empty geometry collection value. ST_Validate() returns it directly without further checks in this case.

    ST_Validate()按本节介绍中所述处理其参数,但有以下例外:ST_Validate() handles its arguments as described in the introduction to this section, with the exceptions listed here:

    • 如果几何图形具有地理空间参考系统 (SRS),且经度或纬度超出范围,将会发生错误:If the geometry has a geographic SRS with 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.

    mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0)');
    mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 0, 1 1)');
    mysql> SELECT ST_AsText(ST_Validate(@ls1));
    +------------------------------+
    | ST_AsText(ST_Validate(@ls1)) |
    +------------------------------+
    | NULL                         |
    +------------------------------+
    mysql> SELECT ST_AsText(ST_Validate(@ls2));
    +------------------------------+
    | ST_AsText(ST_Validate(@ls2)) |
    +------------------------------+
    | LINESTRING(0 0,1 1)          |
    +------------------------------+