MySQL gis 判断某个点是否在多边形中

建表:

CREATE TABLE `test` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `pnt` point DEFAULT NULL,

  `pgn` polygon DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;

 

插入数据:

INSERT INTO `test` VALUES ('12', GeomFromText('POINT(1 1)'), null);

INSERT INTO `test` VALUES ('11', GeomFromText('POINT(1 1)'), null);

INSERT INTO `test` VALUES ('1', GeomFromText('POINT(1 1)'), null);

INSERT INTO `test` VALUES ('2', GeomFromText('POINT(1 5)'), null);

INSERT INTO `test` VALUES ('3', GeomFromText('POINT(2 2)'), null);

INSERT INTO `test` VALUES ('4', GeomFromText('POINT(2 3)'), null);

INSERT INTO `test` VALUES ('5', GeomFromText('POINT(3 2)'), null);

INSERT INTO `test` VALUES ('8', GeomFromText('POINT(2 3)'), null);

INSERT INTO `test` VALUES ('7', GeomFromText('POINT(2 2)'), null);

INSERT INTO `test` VALUES ('9', GeomFromText('POINT(12 12)'), null);

INSERT INTO `test` VALUES ('10', null, null);

INSERT INTO `test` VALUES ('13', null, GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 7 5, 7 7, 5 7, 5 5))'));

INSERT INTO `test` VALUES ('14', null, GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));

INSERT INTO `test` VALUES ('15', null, GeomFromText('POINT(4 4)'));

INSERT INTO `test` VALUES ('16', GeomFromText('POINT(4 4)'), null);

INSERT INTO `test` VALUES ('17', GeomFromText('POINT(4 4)'), null);

 

 

——————————————————————————————

插入点point:

INSERT INTO test VALUE ('17',GeomFromText('POINT(4 4)'),NULL);

插入点多边形:

INSERT INTO test VALUE ('14',NULL,GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0))'));

判断点在哪个多边形内:

select id from test where MBRContains(pgn, GeomFromText('Point(1 1)'));

你可能感兴趣的:(Gis)