After you have created spatial columns, you can populate them with spatial data.创建空间列后,可以使用空间数据填充这些列。
Values should be stored in internal geometry format, but you can convert them to that format from either Well-Known Text (WKT) or Well-Known Binary (WKB) format. 值应以内部几何图形格式存储,但您可以将它们从已知文本(WKT)或已知二进制(WKB)格式转换为该格式。The following examples demonstrate how to insert geometry values into a table by converting WKT values to internal geometry format:以下示例演示了如何通过将WKT值转换为内部几何图形格式将几何图形值插入表中:
Perform the conversion directly in the 直接在INSERT
statement:INSERT
语句中执行转换:
INSERT INTO geom VALUES (ST_GeomFromText('POINT(1 1)')); SET @g = 'POINT(1 1)'; INSERT INTO geom VALUES (ST_GeomFromText(@g));
Perform the conversion prior to the 在INSERT
:INSERT
之前执行转换:
SET @g = ST_GeomFromText('POINT(1 1)'); INSERT INTO geom VALUES (@g);
The following examples insert more complex geometries into the table:以下示例将更复杂的几何图形插入到表中:
SET @g = 'LINESTRING(0 0,1 1,2 2)'; INSERT INTO geom VALUES (ST_GeomFromText(@g)); SET @g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))'; INSERT INTO geom VALUES (ST_GeomFromText(@g)); SET @g = 'GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'; INSERT INTO geom VALUES (ST_GeomFromText(@g));
The preceding examples use 前面的示例使用ST_GeomFromText()
to create geometry values. ST_GeomFromText()
创建几何体值。You can also use type-specific functions:您还可以使用特定类型的函数:
SET @g = 'POINT(1 1)'; INSERT INTO geom VALUES (ST_PointFromText(@g)); SET @g = 'LINESTRING(0 0,1 1,2 2)'; INSERT INTO geom VALUES (ST_LineStringFromText(@g)); SET @g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))'; INSERT INTO geom VALUES (ST_PolygonFromText(@g)); SET @g = 'GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'; INSERT INTO geom VALUES (ST_GeomCollFromText(@g));
A client application program that wants to use WKB representations of geometry values is responsible for sending correctly formed WKB in queries to the server. There are several ways to satisfy this requirement. For example:想要使用几何值的WKB表示的客户端应用程序负责将查询中正确形成的WKB发送到服务器。有几种方法可以满足这一要求。例如
Inserting a 使用十六进制文字语法插入POINT(1 1)
value with hex literal syntax:POINT(1 1)
值:
INSERT INTO geom VALUES (ST_GeomFromWKB(X'0101000000000000000000F03F000000000000F03F'));
An ODBC application can send a WKB representation, binding it to a placeholder using an argument of ODBC应用程序可以发送WKB表示,并使用BLOB
type:BLOB
类型的参数将其绑定到占位符:
INSERT INTO geom VALUES (ST_GeomFromWKB(?))
Other programming interfaces may support a similar placeholder mechanism.其他编程接口可以支持类似的占位符机制。
In a C program, you can escape a binary value using 在C程序中,可以使用mysql_real_escape_string_quote()
and include the result in a query string that is sent to the server. mysql_real_eescape_string_quote()
转义二进制值,并将结果包含在发送到服务器的查询字符串中。See mysql_real_escape_string_quote().请参见mysql_real_sescape_string_quote()
。