11.4.3 Supported Spatial Data Formats支持的空间数据格式

Two standard spatial data formats are used to represent geometry objects in queries:两种标准空间数据格式用于表示查询中的几何图形对象:

Internally, MySQL stores geometry values in a format that is not identical to either WKT or WKB format. (Internal format is like WKB but with an initial 4 bytes to indicate the SRID.)在内部,MySQL以与WKT或WKB格式不同的格式存储几何体值。(内部格式类似于WKB,但最初有4个字节来指示SRID。)

There are functions available to convert between different data formats; see Section 12.17.6, “Geometry Format Conversion Functions”.有一些功能可用于在不同的数据格式之间进行转换;参见第12.17.6节,“几何格式转换函数”

The following sections describe the spatial data formats MySQL uses:以下部分介绍MySQL使用的空间数据格式:

Well-Known Text (WKT) Format众所周知的文本(WKT)格式

The Well-Known Text (WKT) representation of geometry values is designed for exchanging geometry data in ASCII form. The OpenGIS specification provides a Backus-Naur grammar that specifies the formal production rules for writing WKT values (see Section 11.4, “Spatial Data Types”).几何值的众所周知的文本(WKT)表示是为交换ASCII形式的几何数据而设计的。OpenGIS规范提供了一种Backus-Naur语法,该语法指定了写入WKT值的正式生成规则(参见第11.4节,“空间数据类型”)。

Examples of WKT representations of geometry objects:几何图形对象的WKT表示示例:

  • A Point:

    POINT(15 20)

    The point coordinates are specified with no separating comma. 点坐标的指定不使用分隔逗号。This differs from the syntax for the SQL Point() function, which requires a comma between the coordinates. 这与SQL Point()函数的语法不同,后者需要在坐标之间使用逗号。Take care to use the syntax appropriate to the context of a given spatial operation. 注意使用适合给定空间操作上下文的语法。For example, the following statements both use ST_X() to extract the X-coordinate from a Point object. The first produces the object directly using the Point() function. 例如,以下语句都使用ST_X()Point对象提取X坐标。第一个直接使用Point()函数生成对象。The second uses a WKT representation converted to a Point with ST_GeomFromText().第二个使用转换为Point的WKT表示,该表示使用ST_GeomFromText()

    mysql> SELECT ST_X(Point(15, 20));
    +---------------------+
    | ST_X(POINT(15, 20)) |
    +---------------------+
    |                  15 |
    +---------------------+
    
    mysql> SELECT ST_X(ST_GeomFromText('POINT(15 20)'));
    +---------------------------------------+
    | ST_X(ST_GeomFromText('POINT(15 20)')) |
    +---------------------------------------+
    |                                    15 |
    +---------------------------------------+
  • A LineString with four points:LineString具有四个点:

    LINESTRING(0 0, 10 10, 20 25, 50 60)

    The point coordinate pairs are separated by commas.点坐标对用逗号分隔。

  • A Polygon with one exterior ring and one interior ring:Polygon具有一个外环和一个内环:

    POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
  • A MultiPoint with three Point values:MultiPoint具有三个Point值:

    MULTIPOINT(0 0, 20 20, 60 60)

    Spatial functions such as ST_MPointFromText() and ST_GeomFromText() that accept WKT-format representations of MultiPoint values permit individual points within values to be surrounded by parentheses. For example, both of the following function calls are valid:空间函数,如ST_MPointFromText()ST_GeomFromText()接受MultiPoint值的WKT格式表达允许值中的各个点用括号括起来。例如,以下两个函数调用都是有效的:

    ST_MPointFromText('MULTIPOINT (1 1, 2 2, 3 3)')
    ST_MPointFromText('MULTIPOINT ((1 1), (2 2), (3 3))')
  • A MultiLineString with two LineString values:具有两个LineString值的MultiLineString

    MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
  • A MultiPolygon with two Polygon values:具有两个Polygon值的MultiPolygon

    MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
  • A GeometryCollection consisting of two Point values and one LineString:由两个Point值和一个LineString组成的GeometryCollection

    GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))

Well-Known Binary (WKB) Format众所周知的二进制(WKB)格式

The Well-Known Binary (WKB) representation of geometric values is used for exchanging geometry data as binary streams represented by BLOB values containing geometric WKB information. 几何值的公知二进制(WKB)表示用于将几何数据交换为由包含几何WKB信息的BLOB值表示的二进制流。This format is defined by the OpenGIS specification (see Section 11.4, “Spatial Data Types”). It is also defined in the ISO SQL/MM Part 3: Spatial standard.该格式由OpenGIS规范定义(参阅第11.4节,“空间数据类型”)。它也在ISO SQL/MM第3部分:空间标准中进行了定义。

WKB uses 1-byte unsigned integers, 4-byte unsigned integers, and 8-byte double-precision numbers (IEEE 754 format). A byte is eight bits.WKB使用1字节无符号整数、4字节无符号整型和8字节双精度数字(IEEE 754格式)。一个字节是八位。

For example, a WKB value that corresponds to POINT(1 -1) consists of this sequence of 21 bytes, each represented by two hexadecimal digits:例如,对应于POINT(1-1)的WKB值由21个字节的序列组成,每个字节由两个十六进制数字表示:

0101000000000000000000F03F000000000000F0BF

The sequence consists of the components shown in the following table.该序列由下表所示的组件组成。

Table 11.2 WKB Components Example表11.2 WKB组件示例

ComponentSizeValue
Byte order1 byte01
WKB type4 bytes01000000
X coordinate8 bytes000000000000F03F
Y coordinate8 bytes000000000000F0BF

Component representation is as follows:组件表示如下:

  • The byte order indicator is either 1 or 0 to signify little-endian or big-endian storage. 字节顺序指示符是1或0,表示小端或大端存储。The little-endian and big-endian byte orders are also known as Network Data Representation (NDR) and External Data Representation (XDR), respectively.小端字节顺序和大端字节顺序也分别称为网络数据表示(NDR)和外部数据表示(XDR)。

  • The WKB type is a code that indicates the geometry type. WKB类型是指示几何图形类型的代码。MySQL uses values from 1 through 7 to indicate Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.MySQL使用从1到7的值来表示PointLineStringPolygonMultiPointMultiLineStringMultiPolygonGeometryCollection

  • A Point value has X and Y coordinates, each represented as a double-precision value.Point值具有X和Y坐标,每一个坐标都表示为双精度值。

WKB values for more complex geometry values have more complex data structures, as detailed in the OpenGIS specification.更复杂的几何图形值的WKB值具有更复杂的数据结构,如OpenGIS规范中所述。

Internal Geometry Storage Format内部几何存储格式

MySQL stores geometry values using 4 bytes to indicate the SRID followed by the WKB representation of the value. For a description of WKB format, see Well-Known Binary (WKB) Format.MySQL使用4个字节存储几何体值,以指示SRID和值的WKB表示形式。有关WKB格式的描述,请参阅众所周知的二进制(WKB)格式

For the WKB part, these MySQL-specific considerations apply:对于WKB部分,以下MySQL特定注意事项适用:

  • The byte-order indicator byte is 1 because MySQL stores geometries as little-endian values.字节顺序指示符字节是1,因为MySQL将几何图形存储为小端值。

  • MySQL supports geometry types of Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. Other geometry types are not supported.MySQL支持PointLineStringPolygonMultiPointMultiLineStringMultiPolygonGeometryCollection等几何图形类型。不支持其他几何图形类型。

  • Only GeometryCollection can be empty. Such a value is stored with 0 elements.只有GeometryCollection可以为空。这样的值与0个元素一起存储。

  • Polygon rings can be specified both clockwise and counterclockwise. MySQL flips the rings automatically when reading data.多边形环既可以按顺时针方向指定,也可以按逆时针方向指定。MySQL在读取数据时会自动翻转环。

Cartesian coordinates are stored in the length unit of the spatial reference system, with X values in the X coordinates and Y values in the Y coordinates. Axis directions are those specified by the spatial reference system.笛卡尔坐标存储在空间参考系的长度单位中,X值在X坐标中,Y值在Y坐标中。轴方向是由空间参照系统指定的方向。

Geographic coordinates are stored in the angle unit of the spatial reference system, with longitudes in the X coordinates and latitudes in the Y coordinates. Axis directions and the meridian are those specified by the spatial reference system.地理坐标存储在空间参考系的角度单位中,经度在X坐标中,纬度在Y坐标中。轴方向和子午线是由空间参考系统指定的方向。

The LENGTH() function returns the space in bytes required for value storage. Example:LENGTH()函数返回值存储所需的空间(以字节为单位)。例如:

mysql> SET @g = ST_GeomFromText('POINT(1 -1)');
mysql> SELECT LENGTH(@g);
+------------+
| LENGTH(@g) |
+------------+
|         25 |
+------------+
mysql> SELECT HEX(@g);
+----------------------------------------------------+
| HEX(@g)                                            |
+----------------------------------------------------+
| 000000000101000000000000000000F03F000000000000F0BF |
+----------------------------------------------------+

The value length is 25 bytes, made up of these components (as can be seen from the hexadecimal value):值长度为25字节,由以下组成(从十六进制值中可以看出):

  • 4 bytes for integer SRID (0)4字节用于整数SRID(0)

  • 1 byte for integer byte order (1 = little-endian)1字节表示整数字节顺序(1=little-endian)

  • 4 bytes for integer type information (1 = Point)4字节用于整数类型信息(1=Point

  • 8 bytes for double-precision X coordinate (1)8字节用于双精度X坐标(1)

  • 8 bytes for double-precision Y coordinate (−1)8字节用于双精度Y坐标(-1)