mysql - 检查多边形中的点

标签 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)') )

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

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

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

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

为什么?

最佳答案

您提供的字符串是 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 fiddle :http://sqlfiddle.com/#!2/b5d1f/9

关于mysql - 检查多边形中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340664/

相关文章:

javascript - 谷歌地图 : Finding polygon by Id

mysql - 不同域但相同用户的两个相似应用程序

PHP:WAITING时间,因此函数直到 x 分钟后才能执行

SQL-SQLite 计算连续数字

R:使用条件颜色绘制多边形

Java 2D 多边形 - 多边形碰撞检测

php - Laravel - 在 View 中使用 Eloquent where 方法

mysql - 使用 MySQL 分块删除

mysql - SQL 查询不过滤正确的数据

php - 按结果搜索排序?