0

我正在使用 Postgres 9.1 和 PostGIS 2。

不起作用的示例查询:

SELECT
  *
FROM measurement m
JOIN (SELECT unnest(array['002000000100000000404ff25e353f7cee401d01da7b0b3919']) AS p) AS ps
  ON ST_DWithin(ps.p, m.groundtruth, 5)

有效的示例查询:

SELECT
  *
FROM measurement m
JOIN (SELECT 1) AS foo
  ON ST_DWithin('002000000100000000404ff25e353f7cee401d01da7b0b3919', m.groundtruth, 5)

也有效

SELECT unnest(array['002000000100000000404ff25e353f7cee401d01da7b0b3919'])

但是,我当然需要数组工作的查询。

我从自动插入 WKB 格式的 Rails 调用此查询(意味着:如果这很重要,我不知道如何将其更改为 WKT)。

完整的示例 Rails 代码如下所示:

x = [RGeo::Geos.factory.point(63.8935, 6.25181)]
@measurement = Measurement.find_by_sql([radius_search_query, x])

def radius_search_query
  SELECT
    *
  FROM measurement m
  JOIN (SELECT unnest(array[?]) AS p) AS ps
    ON ST_DWithin(ps.p, m.groundtruth, 5)
end

但我也测试了上面的查询(不使用?),它一直失败并显示相同的错误消息:

ERROR:  function st_dwithin(text, geometry, integer) is not unique
LINE 5:       ON ST_DWithin(ps.p, m.groundtruth, 5)
                 ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.

我想我可能必须告诉数组它是类型geometry而不是类型text(如错误消息中所示)。但我还没有找到一种方法来做到这一点。

谢谢!!


更多信息:

这不起作用:

SELECT
  *
FROM measurement m
JOIN (SELECT '002000000100000000404ff25e353f7cee401d01da7b0b3919' as bar) AS foo
  ON ST_DWithin(foo.bar, m.groundtruth, 5)

错误信息:failed to find conversion function from unknown to geometry

这有效:

SELECT
  *
FROM measurement m
JOIN (SELECT '002000000100000000404ff25e353f7cee401d01da7b0b3919'::geometry(Point) as bar) AS foo
  ON ST_DWithin(foo.bar, m.groundtruth, 5)
4

1 回答 1

0

在我刚刚找到一个使查询非常慢的解决方案之后(因为转换值):

SELECT
  *
FROM measurement m
JOIN (SELECT unnest(array['002000000100000000404ff25e353f7cee401d01da7b0b3919']) AS p) AS ps
  ON ST_DWithin(ps.p::geometry(Point), m.groundtruth, 5)

我环顾四周,偶然发现了 PostGIS' linestring。现在一切正常!

SELECT
  *
FROM ST_DumpPoints('00200000020000000000000002404ff25e353f7cee401901da7b0b3919404ff25e353f7cee401d01da7b0b3919') AS ps
JOIN measurement m
  ON ST_DWithin(ps.geom, m.groundtruth, 5)

耶!:-)

于 2013-02-27T20:07:45.103 回答