MySQL空间数据查询

时间:2015-09-18 13:49:18

标签: mysql spatial

我正在尝试在其中一个字段中实现一个带多边形的区域表。我稍后会用它来检查多边形中是否包含某个点。

据我所知,我已经实现了它,但经过测试后发现,当它们不应该返回时,我会发现某些点(靠近,但在多边形之外)。

CREATE TABLE `zones` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `geom` geometry NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO zones (created_at, updated_at, name, `geom`)
VALUES (NOW(),NOW(), "Purmerend", PolygonFromText('POLYGON(( 
52.37811421553942 4.98779296875,
52.36637614192118 5.002899169921875,
52.381467377903476 5.04547119140625,
52.46688700596046 5.156707763671875,
52.62306005822956 5.04547119140625,
52.64223014513886 4.84222412109375,
52.61555643344044 4.769439697265625,
52.51287794429004 4.7186279296875,
52.45684622754481 4.833984375,
52.37811421553942 4.98779296875))') );

#1 outside
SELECT * FROM zones WHERE CONTAINS(geom, POINT(52.306024, 4.913726)); 
#2 **-- false positive --**
SELECT * FROM zones WHERE CONTAINS(geom, POINT(52.434471, 4.817571));
#3 inside
SELECT * FROM zones WHERE CONTAINS(geom, POINT(52.501924, 4.964426));

Graphical representation

我正在使用MySQL 5.5并想知道升级到5.6是否会帮助我或者是否有其他解决方案?

1 个答案:

答案 0 :(得分:0)

MySql 5.5 CONTAINS在计算POINT是否在POLYGON中时使用有界rect。 MySql 5.6 ST_CONTAINS函数不会这样做并正确计算2的位置

McAdam331证明了这一点:5.6 ST_CONTAINS works in this [SQL Fiddle][1]