检查多边形中的点

时间:2014-08-16 13:33:19

标签: mysql sql polygon point

当硬编码点没有错误或警告时,此代码选择表中的所有行:

SELECT *
    FROM lat_lng 
    WHERE Contains(
            GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
            GeomFromText('Point(50 50)') )

由变量$ var =" 50 50"定义的OR。 (没有错误或警告)

SELECT *
    FROM lat_lng 
    WHERE Contains(
            GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
            GeomFromText('Point($var)') )

然而,当我使用名为" location"要定义该点,选择零行(没有错误或警告):

SELECT *
    FROM lat_lng 
    WHERE Contains(
            GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
            GeomFromText('Point(location)') )

基于这两行样本表:

id | location
1  |  50 50
2  |  500 500

为什么?

1 个答案:

答案 0 :(得分:4)

您提供的字符串为Point(location)(逐字)。您想要的是引用位置列,因此您需要构建字符串:

CONCAT('Point(',location,')')

此外,您可能希望将Point直接存储在另一个表(而不是文本)中,然后在没有GeomFromText

的情况下引用它

<强>更新

直接在您的数据库中存储Point

架构:

CREATE TABLE locations (id integer primary key auto_increment,location point);
INSERT INTO locations (location) VALUES (Point(50,50));
INSERT INTO locations (location) VALUES (Point(500,500));

请求:

SELECT id
    FROM locations
    WHERE Contains(
        GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
        location);

更新2

SQL小提琴:http://sqlfiddle.com/#!2/b5d1f/9