使用PostGis在Postgres中触摸功能

时间:2013-05-09 20:29:43

标签: sql function postgresql postgis spatial

我在PostGis上安装了PostgreSQL,我尝试运行以下命令:

SELECT N1.edname AS "Borders Royal Exchange A"
FROM eds_census2011 N1, eds_census2011 N2
WHERE Touch(N1.the_geom, N2.the_geom)
AND N2 = 'Royal Exchange A'

我收到错误(如下)是否有任何我必须添加到Postgres或启用某些内容?

ERROR:  function touch(geometry, geometry) does not exist
LINE 3: WHERE Touch(N1.the_geom, N2.the_geom)
              ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

********** Error **********

ERROR: function touch(geometry, geometry) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 96

2 个答案:

答案 0 :(得分:2)

运行这两个查询(在您尝试上述查询的同一个会话中)来确定问题:

函数touch()在哪个模式中存在?

SELECT p.proname, n.nspname
FROM   pg_proc p
JOIN   pg_namespace n ON n.oid = p.pronamespace
WHERE  proname = 'touch';

我的角色的当前架构search_path是什么:

SHOW search_path;

如果函数存在,则架构必须位于当前search_path中,以便Postgres可以找到它。如何调整search_path
How does the search_path influence identifier resolution and the "current schema"

BTW,我在Postgis手册的函数参考中找不到函数Touch()。有一个叫ST_Touches()。你意味着那个机会吗?

请注意,此查询的成本为O(N²),因为它会计算eds_census2011中任意两个符合条件的行的每个组合的值。如果您的条件N2.edname = 'Royal Exchange A'足够有选择性,那么这不会有问题。

此外,您可能希望使用其他WHERE项来排除加入自己的行,例如:

AND N1.pk_id <> N2.pk_id 

更新后出错

您更新的查询更有意义:

SELECT N1.edname AS "Borders Royal Exchange A"
FROM   eds_census2011 N1, eds_census2011 N2
WHERE  ST_Touches(N1.the_geom, N2.the_geom)=1
AND    N2.edname = 'Royal Exchange A';

但是ST_Touches()返回boolean,所以Where子句应该只是:

WHERE  ST_Touches(N1.the_geom, N2.the_geom)

答案 1 :(得分:0)

知道了 - 感谢大家,使用了以下内容:

SELECT N1.edname AS "Borders Royal Exchange A"
FROM eds_census2011 N1, eds_census2011 N2
WHERE ST_Touches(N1.the_geom, N2.the_geom)
AND N2.edname = 'Royal Exchange A'