Documentation Home
MySQL 9.4 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 41.2Mb
PDF (A4) - 41.3Mb
Man Pages (TGZ) - 262.8Kb
Man Pages (Zip) - 368.8Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.4 Reference Manual  / ...  / Data Types  / Spatial Data Types  /  Supported Spatial Data Formats

13.4.3 Supported Spatial Data Formats

Two standard spatial data formats are used to represent geometry objects in queries:

  • Well-Known Text (WKT) format

  • Well-Known Binary (WKB) format

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.)

There are functions available to convert between different data formats; seeSection 14.16.6, “Geometry Format Conversion Functions”.

The following sections describe the spatial data formats MySQL uses:

Well-Known Text (WKT) Format

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 (seeSection 13.4, “Spatial Data Types”).

Examples of WKT representations of geometry objects:

  • APoint:

    POINT(15 20)

    The point coordinates are specified with no separating comma. This differs from the syntax for the SQLPoint() function, which requires a comma between the coordinates. Take care to use the syntax appropriate to the context of a given spatial operation. For example, the following statements both useST_X() to extract the X-coordinate from aPoint object. The first produces the object directly using thePoint() function. The second uses a WKT representation converted to aPoint withST_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 |+---------------------------------------+
  • ALineString with four points:

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

    The point coordinate pairs are separated by commas.

  • APolygon with one exterior ring and one interior ring:

    POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
  • AMultiPoint with threePoint values:

    MULTIPOINT(0 0, 20 20, 60 60)

    Spatial functions such asST_MPointFromText() andST_GeomFromText() that accept WKT-format representations ofMultiPoint values permit individual points within values to be surrounded by parentheses. For example, both of the following function calls are valid:

    ST_MPointFromText('MULTIPOINT (1 1, 2 2, 3 3)')ST_MPointFromText('MULTIPOINT ((1 1), (2 2), (3 3))')
  • AMultiLineString with twoLineString values:

    MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
  • AMultiPolygon with twoPolygon values:

    MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
  • AGeometryCollection consisting of twoPoint values and oneLineString:

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

Well-Known Binary (WKB) Format

The Well-Known Binary (WKB) representation of geometric values is used for exchanging geometry data as binary streams represented byBLOB values containing geometric WKB information. This format is defined by the OpenGIS specification (seeSection 13.4, “Spatial Data Types”). It is also defined in the ISOSQL/MM Part 3: Spatial standard.

WKB uses 1-byte unsigned integers, 4-byte unsigned integers, and 8-byte double-precision numbers (IEEE 754 format). A byte is eight bits.

For example, a WKB value that corresponds toPOINT(1 -1) consists of this sequence of 21 bytes, each represented by two hexadecimal digits:

0101000000000000000000F03F000000000000F0BF

The sequence consists of the components shown in the following table.

Table 13.2 WKB Components Example

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. The little-endian and big-endian byte orders are also known as Network Data Representation (NDR) and External Data Representation (XDR), respectively.

  • The WKB type is a code that indicates the geometry type. MySQL uses values from 1 through 7 to indicatePoint,LineString,Polygon,MultiPoint,MultiLineString,MultiPolygon, andGeometryCollection.

  • APoint value has X and Y coordinates, each represented as a double-precision value.

WKB values for more complex geometry values have more complex data structures, as detailed in the OpenGIS specification.

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, seeWell-Known Binary (WKB) Format.

For the WKB part, these MySQL-specific considerations apply:

  • The byte-order indicator byte is 1 because MySQL stores geometries as little-endian values.

  • MySQL supports geometry types ofPoint,LineString,Polygon,MultiPoint,MultiLineString,MultiPolygon, andGeometryCollection. Other geometry types are not supported.

  • OnlyGeometryCollection can be empty. Such a value is stored with 0 elements.

  • Polygon rings can be specified both clockwise and counterclockwise. MySQL flips the rings automatically when reading data.

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.

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.

TheLENGTH() function returns the space in bytes required for value storage. Example:

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):

  • 4 bytes for integer SRID (0)

  • 1 byte for integer byte order (1 = little-endian)

  • 4 bytes for integer type information (1 =Point)

  • 8 bytes for double-precision X coordinate (1)

  • 8 bytes for double-precision Y coordinate (−1)