Home >Database >Mysql Tutorial >Mysql中判断一个点是否落在多边形内_MySQL

Mysql中判断一个点是否落在多边形内_MySQL

WBOY
WBOYOriginal
2016-06-01 13:38:491600browse

bitsCN.com


Mysql中判断一个点是否落在多边形内

 

关于地理空间数据,经常需要处理两个空间数据的关联关系。有很多种方法可以处理,通过编写程序算法,或者是调用数据库中对应的function。在mysql数据库中,http://dev.mysql.com/doc/refman/5.1/en/functions-for-testing-spatial-relations-between-geometric-objects.html做了详细的介绍,但是它没有以具体的工程实践为例,本文以判断一个点是否落在多边形内的主题,加以简单的扩展。

      首先,建立一张简单的地理数据表,

 

[sql] 

CREATE TABLE `ci_special_zone` (  

  `id` int(11) NOT NULL auto_increment,  

  `ploygongeo` text NOT NULL,  

  PRIMARY KEY  (`id`)  

) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

 

并插入几条数据

 

[sql] 

INSERT INTO ci_special_zone (ploygongeo) VALUES('POLYGON((113.547 22.186,113.549 22.186,113.549 22.188, 113.547 22.188,113.547 22.186))');  

INSERT INTO ci_special_zone (ploygongeo) VALUES('POLYGON((112.547 21.186,112.549 212.186,112.549 21.188, 112.547 212.188,112.547 21.186))');  

 

最后,执行如下的sql语句

 

[sql] 

SELECT substring(ploygongeo,10,length(ploygongeo)-11) from ci_special_zone   

where MBRContains(PolygonFromText(ploygongeo),PolygonFromText('Point(113.547 22.186)'))>0 limit 0,1  

 

坐标点113.547 22.186是经纬度,若有返回值,则表示坐标点落在所在的区间。

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn