MySQL 空間數據 簡單操作
在做的項目中需要,自己繪制區域圖形,並存儲起來,後面還有更新的需要,存文件不方面,想到現在數據庫都支持空間數據庫。
現在用的就是 MySQL ,就繼續用 MySQL 來存儲、管理空間數據。下面就做一些簡單的記錄。
創建空間數據
CREATE TABLE `t_pot` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `pot` point DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULTCHARSET=utf8;
上面是建的一個包含點數據的表
CREATE TABLE `t_polygon` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `pgn` polygon DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
上面是建的一個包含面數據的表
MySQL 中的空間數據類型:point、linestring、polygon、geometry、multipoint、multilinestring、multipolygon、geometrycollection
空間數據操作
下面是對數據的一些查詢、分析等操作語句
先插入一些示例數據
INSERT INTO `t_polygon` VALUES (‘1‘, ‘ABCD‘, GeomFromText(‘POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))‘)); INSERT INTO `t_polygon` VALUES (‘2‘, ‘AEGF‘, GeomFromText(‘POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))‘)); INSERT INTO `t_polygon` VALUES (‘3‘, ‘CEGFBD‘, GeomFromText(‘POLYGON((0 2, 0 4, 4 4, 4 0, 2 0, 2 2, 0 2))‘)); INSERT INTO `t_polygon` VALUES (‘4‘, ‘AHJIK‘, GeomFromText(‘POLYGON((0 0, 0 6, 6 6, 6 0, 0 0), (2 2, 4 4, 4 2, 2 2))‘)); INSERT INTO `t_polygon` VALUES (‘5‘, ‘DGK‘, GeomFromText(‘POLYGON((2 2, 4 4, 4 2, 2 2))‘)); INSERT INTO `t_polygon` VALUES (‘6‘, ‘GKJ‘, GeomFromText(‘POLYGON((4 4, 4 2, 6 6, 4 4))‘)); INSERT INTO `t_polygon` VALUES (‘7‘, ‘ADF‘, GeomFromText(‘POLYGON((0 0, 2 2, 4 0, 0 0))‘)); INSERT INTO `t_polygon` VALUES (‘8‘, ‘LDK‘, GeomFromText(‘POLYGON((1 1, 2 2, 4 2, 1 1))‘)); INSERT INTO `t_pot` VALUES (‘1‘, ‘A‘, GeomFromText(‘POINT(0 0)‘)); INSERT INTO `t_pot` VALUES (‘2‘, ‘B‘, GeomFromText(‘POINT(0 2)‘)); INSERT INTO `t_pot` VALUES (‘3‘, ‘C‘, GeomFromText(‘POINT(2 0)‘)); INSERT INTO `t_pot` VALUES (‘4‘, ‘D‘, GeomFromText(‘POINT(2 2)‘));
一些空間對象方法
-- 包含,MBRCONTAINS,第一個參數是包含的要素,第二個是被包含的要素,第一個包含第二個返回1,否則返回0 SELECT py.`name`,p.`name`,MBRCONTAINS(py.pgn,p.pot) FROM t_polygon py,t_pot p; -- 被包含,MBRWITHIN,和上面一個參數相反,返回結果一樣 SELECT py.`name`,p.`name`,MBRWITHIN(p.pot,py.pgn) FROM t_polygon py,t_pot p; -- 不相交,MBRDISJOINT,參數沒有先後分,不相交返回1,否則返回1 SELECT py.`name`,p.`name`,MBRDISJOINT(p.pot,py.pgn) FROM t_polygon py,t_pot p; -- 相等,MBREQUAL,兩個要素是否相等,相等返回1,否則返回0 SELECT py.`name`,p.`name`,MBREQUAL(p.pot,py.pgn) FROM t_polygon py,t_pot p; -- 相交,MBREQUAL,兩個要素是否相交,相交返回1,否則返回0 SELECT py.`name`,p.`name`,MBRINTERSECTS(p.pot,py.pgn) FROM t_polygon py,t_pot p; -- 重疊,MBROVERLAPS,兩個要素是否重疊,重疊返回1,否則返回0 SELECT py.`name`,p.`name`,MBROVERLAPS(p.pot,py.pgn) FROM t_polygon py,t_pot p; -- 相切,MBREQUAL,兩個要素是否相切,相切返回1,否則返回0 SELECT py.`name`,p.`name`,MBREQUAL(p.pot,py.pgn) FROM t_polygon py,t_pot p;
INSERT INTO `t_polygon` VALUES (‘1‘, ‘ABCD‘, GeomFromText(‘POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))‘));INSERT INTO `t_polygon` VALUES (‘2‘, ‘AEGF‘, GeomFromText(‘POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))‘));INSERT INTO `t_polygon` VALUES (‘3‘, ‘CEGFBD‘, GeomFromText(‘POLYGON((0 2, 0 4, 4 4, 4 0, 2 0, 2 2, 0 2))‘));INSERT INTO `t_polygon` VALUES (‘4‘, ‘AHJIK‘, GeomFromText(‘POLYGON((0 0, 0 6, 6 6, 6 0, 0 0), (2 2, 4 4, 4 2, 2 2))‘));INSERT INTO `t_polygon` VALUES (‘5‘, ‘DGK‘, GeomFromText(‘POLYGON((2 2, 4 4, 4 2, 2 2))‘));INSERT INTO `t_polygon` VALUES (‘6‘, ‘GKJ‘, GeomFromText(‘POLYGON((4 4, 4 2, 6 6, 4 4))‘));INSERT INTO `t_polygon` VALUES (‘7‘, ‘ADF‘, GeomFromText(‘POLYGON((0 0, 2 2, 4 0, 0 0))‘));INSERT INTO `t_polygon` VALUES (‘8‘, ‘LDK‘, GeomFromText(‘POLYGON((1 1, 2 2, 4 2, 1 1))‘));
MySQL 空間數據 簡單操作