检查多边形中的点

nic*_*che 5 mysql sql point polygon

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

SELECT *
    FROM lat_lng 
    WHERE Contains(
            GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
            GeomFromText('Point(50 50)') )
Run Code Online (Sandbox Code Playgroud)

或由变量$ var =“ 50 50”定义(无错误或警告)

SELECT *
    FROM lat_lng 
    WHERE Contains(
            GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
            GeomFromText('Point($var)') )
Run Code Online (Sandbox Code Playgroud)

但是,当我使用称为“位置”的列来定义点时,选择了零行(没有错误或警告):

SELECT *
    FROM lat_lng 
    WHERE Contains(
            GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
            GeomFromText('Point(location)') )
Run Code Online (Sandbox Code Playgroud)

基于此两行样本表:

id | location
1  |  50 50
2  |  500 500
Run Code Online (Sandbox Code Playgroud)

为什么?

jca*_*ron 5

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

CONCAT('Point(',location,')')
Run Code Online (Sandbox Code Playgroud)

另外,您可能希望将Point直接存储在该其他表(而不是文本)中,然后在不使用GeomFromText

更新资料

Points直接存储在您的数据库中:

架构:

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));
Run Code Online (Sandbox Code Playgroud)

请求:

SELECT id
    FROM locations
    WHERE Contains(
        GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
        location);
Run Code Online (Sandbox Code Playgroud)

更新2

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