views:

197

answers:

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.

+1  A: 

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.

bvmou