INSERT INTO `t_geo_test` (ID,NAME,SHAPE) VALUES (5, 'POLYGON_1', geomfromtext('POLYGON((121.474243 31.234504, 121.471775 31.233348, 121.470724 31.23155, 121.471603 31.230229, 121.472655 31.230357, 121.475777 31.232045, 121.474243 31.234504))'));
5.3 获取数据
AsText(): 此函数能将几何对象的内部存储格式转换为WKT格式
6. 常用函数
6.1 获取几何对象属性的函数
6.1.1 Geometry
-->Dimension(g)
返回对象g的维数
-->Envelope(g)
返回对象g的最小边界矩形(MBR)。结类型为Polygon值。
-->GeometryType(g)
以字符串形式返回几何类型的名称,如POINT,LINESTRING
-->IsClosed(g)
返回对象g是否封闭
-->IsSimple(g)
返回对象g是否简单
6.1.2 Point
-->X(p)
以双精度数值返回点p的X坐标值(经度)。
-->Y(p)
以双精度数值返回点p的Y坐标值(纬度)。
6.1.3 LineString
-->EndPoint(line)
返回对象line的最后一个点Point
-->StartPoint(line)
返回对象line的第一个点Point
-->PointN(line, N)
返回对象line中第N个点,N从1开始
6.1.4 Polygon
-->ExteriorRing(poly)
返回对象poly的外环,类型为LineString
-->InteriorRingN(poly, N)
返回对象poly的第N个内环,N从1开始
-->NumInteriorRings(poly)
返回对象poly的neihuan个数
6.2 从现成几何对象创建新的对象
6.2.1 st_union(g1, g2)
将g1和g2合并为一个集合类对象
SET @g1 = geomFromText('POLYGON((121.474243 31.234504,121.471775 31.233348,121.470724 31.23155,121.471603 31.230229,121.472655 31.230357,121.475777 31.232045,121.474243 31.234504))');
SET @g2 = geomFromText('POLYGON((121.474243 31.234804,121.471775 31.233948,121.471724 31.23155,121.471903 31.230229,121.472655 31.230157,121.475777 31.231045,121.474243 31.234804))');
SELECT st_union(@g1, @g2);
结果:
POLYGON((121.472655 31.230157, 121.471903 31.230229, 121.471898134093 31.2302649098516, 121.471603 31.230229, 121.470724 31.23155, 121.471761757556 31.2333253454665, 121.471775 31.233948, 121.474243 31.234804, 121.474597 31.2339365384615, 121.475777 31.232045, 121.475442678789 31.2318642395248, 121.475777 31.231045, 121.472655 31.230157))
6.2.2 st_difference(g1, g2)
返回几何对象,该对象表示了几何值g1与g2的点集合差异
SET @g1 = geomFromText('POLYGON((121.474243 31.234504,121.471775 31.233348,121.470724 31.23155,121.471603 31.230229,121.472655 31.230357,121.475777 31.232045,121.474243 31.234504))');
SET @g2 = geomFromText('POLYGON((121.474243 31.234804,121.471775 31.233948,121.471724 31.23155,121.471903 31.230229,121.472655 31.230157,121.475777 31.231045,121.474243 31.234804))');
SELECT st_difference(@g1,@g2);
��� 结果:
MULTIPOLYGON(((121.471603 31.230229, 121.470724 31.23155, 121.471761757556 31.2333253454665, 121.471724 31.23155, 121.471898134093 31.2302649098516, 121.471603 31.230229)), ((121.475442678789 31.2318642395248, 121.474597 31.2339365384615, 121.475777 31.232045, 121.475442678789 31.2318642395248)))
6.2.3 st_intersection(g1,g2)
返回几何对象,该对象表示了几何值g1与g2的点集合交集
SET @g1 = geomFromText('POLYGON((121.474243 31.234504,121.471775 31.233348,121.470724 31.23155,121.471603 31.230229,121.472655 31.230357,121.475777 31.232045,121.474243 31.234504))');
SET @g2 = geomFromText('POLYGON((121.474243 31.234804,121.471775 31.233948,121.471724 31.23155,121.471903 31.230229,121.472655 31.230157,121.475777 31.231045,121.474243 31.234804))');
SELECT st_intersection(@g1,@g2);
结果:
POLYGON((121.471898134093 31.2302649098516, 121.471724 31.23155, 121.471761757556 31.2333253454665, 121.471775 31.233348, 121.474243 31.234504, 121.474597 31.2339365384615, 121.475442678789 31.2318642395248, 121.472655 31.230357, 121.471898134093 31.2302649098516))
6.3 几何对象之间空间关系的函数
6.3.1 st_contains(g1, g2)
返回1: g1完全包含g2;返回0: g1未包含g2
6.3.2 st_crosses(g1, g2), st_intersects(g1, g2)
返回1: g1与g2相交;返回0:g1与g2未相交
6.3.3 st_disjoint(g1, g2)
是st_crosses的反函数
6.3.4 st_within(g1, g2)
g1在g2内则返回1,否则返回0
7. 空间索引
对表中的geometry类型的字段进行索引可以优化搜索,MySQL中通过对Geometry对象的MBR创建索引
创建:
CREATE SPATIAL INDEX i_shape ON `t_geo_test`(SHAPE);
删除:
DROP INDEX i_shape ON `t_geo_test`;
8.一些注意事项