MySql 地理空间错误..?
这个问题是针对 Mysql 地理空间扩展专家的。
以下查询不是我期望的结果:
create database test_db;
use test_db;
create table test_table (g polygon not null);
insert into test_table (g) values (geomfromtext('Polygon((0 5,5 10,7 8,2 3,0 5))'));
insert into test_table (g) values (geomfromtext('Polygon((2 3,7 8,9 6,4 1,2 3))'));
select
X(PointN(ExteriorRing(g),1)), Y(PointN(ExteriorRing(g),1)),
X(PointN(ExteriorRing(g),2)), Y(PointN(ExteriorRing(g),2)),
X(PointN(ExteriorRing(g),3)), Y(PointN(ExteriorRing(g),3)),
X(PointN(ExteriorRing(g),4)), Y(PointN(ExteriorRing(g),4))
from test_table where MBRContains(g,GeomFromText('Point(3 6)'));
基本上我们正在创建 2 个多边形,并且我们尝试使用 MBRContains 来确定一个点是否在两个多边形中的任何一个内。
令人惊讶的是,它返回两个多边形! 点 3,6 只能存在于第一个插入的多边形中。
请注意,两个多边形都是倾斜的(一旦您在纸上绘制多边形,您就会看到)
MySql 如何返回两个多边形? 我正在使用 MySql 社区版 5.1。
This question is for Mysql geospatial-extension experts.
The following query doesn't the result that I'm expecting:
create database test_db;
use test_db;
create table test_table (g polygon not null);
insert into test_table (g) values (geomfromtext('Polygon((0 5,5 10,7 8,2 3,0 5))'));
insert into test_table (g) values (geomfromtext('Polygon((2 3,7 8,9 6,4 1,2 3))'));
select
X(PointN(ExteriorRing(g),1)), Y(PointN(ExteriorRing(g),1)),
X(PointN(ExteriorRing(g),2)), Y(PointN(ExteriorRing(g),2)),
X(PointN(ExteriorRing(g),3)), Y(PointN(ExteriorRing(g),3)),
X(PointN(ExteriorRing(g),4)), Y(PointN(ExteriorRing(g),4))
from test_table where MBRContains(g,GeomFromText('Point(3 6)'));
Basically we are creating 2 Polygons, and we are trying to use MBRContains to determine whether a Point is within either of the two polygons.
Surprisingly, it returns both polygons! Point 3,6 should only exist in the first inserted polygon.
Note that both polygons are tilted (once you draw the polygons on a piece of paper, you will see)
How come MySql returns both polygons? I'm using MySql Community Edition 5.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:希望这个答案已被 MySQL 废弃:http://forge。 mysql.com/wiki/GIS_Functions! 将留下作为较旧的 MBRContains 实现的参考,但解释这些更改的新答案将受到欢迎,并且应该在该答案之上“接受”。
开始过时的答案
您正在使用(必要时)最小边界矩形 ,而不是多边形本身。 出于查询的目的,要比较的形状相当于:
'POLYGON(0 2,0 10,7 10,7 2,0 2)'
第一个形状的
和:
' POLYGON(2 1,9 1,9 9,2 9,2 1)'
第二个为 。 (3,6) 两者皆有。
MBR 是一种粗略的估计方法,将矩形想象为在 min(x) 和 max(x) 处具有垂直线,在 max(y) 和 min(y) 处具有水平线。 这些形状更容易数据库计算,MySQL 不支持所有多边形函数。 (区域是,线串通常是,
包含
还没有。更多详细信息此处。)。 如果您目前想要更精确的地理空间几何,开源方面的最佳选择是 PostGIS。UPDATE: Hopefully this answer has been obsoleted by MySQL: http://forge.mysql.com/wiki/GIS_Functions! Will leave as reference for older MBRContains implementations but a new answer explaining the changes would be welcome and should be "accepted" above this one.
begin obsolete answer
You are using (by necessity) minimum bounding rectangles, not the polygons as such. For the purpose of your query the shape being compared is equivalent to:
'POLYGON(0 2,0 10,7 10,7 2,0 2)'
for the first shape and:
'POLYGON(2 1,9 1,9 9,2 9,2 1)'
for the second. (3,6) is in both.
MBR is a rough way of estimation, picture the rectangle as having vertical lines at min(x) and max(x), and horizontal lines at max(y) and min(y). Those shapes are easier for the database to calculate, MySQL does not support all polygon functions. (Area yes, linestrings generally yes,
contains
not yet. More details here.). If you want more precise geospatial geometry at the moment the best choice on the open source side is PostGIS.