在多边形中检查点

5
这段代码在没有错误或警告的情况下,硬编码点时选择表中的所有行:
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"来定义(无错误或警告)

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个回答

5
你提供了一个字符串,即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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接