【问题标题】:check for point in a polygon检查多边形中的点
【发布时间】:2014-10-10 00:43:14
【问题描述】:

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

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

为什么?

【问题讨论】:

    标签: mysql sql polygon point


    【解决方案1】:

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

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

    您可能还想将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));
    

    请求:

    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

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 2020-04-07
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      相关资源
      最近更新 更多