在做的项目中需要,自己绘制区域图形,并存储起来,后面还有更新的需要,存文件不方面,想到现在数据库都支持空间数据库。
现在用的就是 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 DEFAULT CHARSET=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))‘));
原文地址:https://www.cnblogs.com/zhurong/p/9450930.html