本节中的函数提供对几何值的便利操作。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 isNULL, the return value isNULL.如果任何几何参数不是语法上格式正确的几何体,则会发生
ER_GIS_INVALID_DATA错误。If any geometry argument is not a syntactically well-formed geometry, anER_GIS_INVALID_DATAerror occurs.如果任何几何参数是在未定义空间参考系统(SRS)中的语法上良构的几何,将会发生
ER_SRS_NOT_FOUND错误。If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), anER_SRS_NOT_FOUNDerror occurs.对于接受多个几何参数的函数,如果这些参数不在相同的 SRS 中,会出现
ER_GIS_DIFFERENT_SRIDS错误。For functions that take multiple geometry arguments, if those arguments are not in the same SRS, anER_GIS_DIFFERENT_SRIDSerror 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 betweenPointorMultiPointarguments on a sphere, in meters. (For general-purpose distance calculations, see theST_Distance()function.) The optionalradiusargument should be given in meters.如果两个几何参数都是有效的笛卡尔
Point或MultiPoint值(SRID 为 0),则返回值是在提供的半径下两几何图形之间的最短球面距离。如果未提供,则默认半径为 6,370,986 米,Point 的 X 和 Y 坐标分别解释为经度和纬度,以度为单位。If both geometry parameters are valid CartesianPointorMultiPointvalues 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 validPointorMultiPointvalues 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 arePointandPoint, orPointandMultiPoint(in any argument order). If at least one of the geometries is neitherPointnorMultiPoint, and its SRID is 0, anER_NOT_IMPLEMENTED_FOR_CARTESIAN_SRSerror occurs. If at least one of the geometries is neitherPointnorMultiPoint, and its SRID refers to a geographic SRS, anER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRSerror occurs. If any geometry refers to a projected SRS, anER_NOT_IMPLEMENTED_FOR_PROJECTED_SRSerror 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.
显示的范围以度为单位。如果 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 theradiusargument is present but not positive, anER_NONPOSITIVE_RADIUSerror occurs.如果距离超过双精度数的范围,将会发生
ER_STD_OVERFLOW_ERROR错误。If the distance exceeds the range of a double-precision number, anER_STD_OVERFLOW_ERRORerror 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 | +--------------------------------+如果参数在几何上有效,则返回 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 不支持 GISEMPTY值,例如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 GISEMPTYvalues such asPOINT 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:
如果经度值不在范围(−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.
显示的范围以度为单位。如果 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 | +------------------+返回围绕两个点形成包络的矩形,可以是
Point、LineString或Polygon。Returns the rectangle that forms the envelope around two points, as aPoint,LineString, orPolygon.计算是使用笛卡尔坐标系进行的,而不是在球体、椭球体或地球上进行的。Calculations are done using the Cartesian coordinate system rather than on a sphere, spheroid, or on earth.
给定两个点
pt1和pt2,ST_MakeEnvelope()会在一个抽象平面上创建如下的结果几何体:Given two pointspt1andpt2,ST_MakeEnvelope()creates the result geometry on an abstract plane like this:如果
pt1和pt2相等,结果就是点pt1。Ifpt1andpt2are equal, the result is the pointpt1.否则,如果
(pt1, pt2)是一条垂直或水平的线段,结果就是线段(pt1, pt2)。Otherwise, if(is a vertical or horizontal line segment, the result is the line segmentpt1,pt2)(.pt1,pt2)否则,结果是一个多边形,使用
pt1和pt2作为对角点。Otherwise, the result is a polygon usingpt1andpt2as 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:如果参数不是
Point值,则会发生ER_WRONG_ARGUMENTS错误。If the arguments are notPointvalues, anER_WRONG_ARGUMENTSerror occurs.如果两个点的任意坐标值为无限大或
NaN,则会发生额外的ER_GIS_INVALID_DATA错误。AnER_GIS_INVALID_DATAerror occurs for the additional condition that any coordinate value of the two points is infinite orNaN.如果任何几何体具有地理空间参考系统(SRS)的 SRID 值,将会发生
ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS错误。If any geometry has an SRID value for a geographic spatial reference system (SRS), anER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRSerror occurs.
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)) | +----------------------------------------+使用道格拉斯-普克算法简化几何图形,并返回相同类型的简化值。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参数是一个顶点到其他线段的距离(以输入坐标的单位为准),用于判断是否移除该顶点。距离简化后的折线在此范围内的顶点将被移除。Themax_distanceargument 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 toST_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 themax_distanceargument is not positive, or isNaN, anER_WRONG_ARGUMENTSerror 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) | +---------------------------------+根据 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,NULLif the argument is not syntactically well-formed or is not geometrically valid or isNULL.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 inputPolygonorMultiPolygonhas 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:
如果经度值不在范围(−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.
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) | +------------------------------+