【问题标题】:Touch function in Postgres with PostGis使用 PostGis 在 Postgres 中触摸功能
【发布时间】:2013-05-04 10:42:11
【问题描述】:

我已经安装了带有 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

【问题讨论】:

  • AND N2 = 'eds_census2011' 看起来也很可疑(您不能将表(别名)与字符串常量进行比较)
  • 抱歉,我输入的时候打错了,应该是:Royal Exchange A。这不是我得到的错误
  • 你真的有一个名为N2的列,就像表别名一样?那么你应该对它进行表限定!否则这仍然是句法废话:AND N2 = 'Royal Exchange A'

标签: sql function postgresql postgis spatial


【解决方案1】:

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

函数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"

顺便说一句,我在 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)

【讨论】:

  • 我做了一些更改:codeSELECT 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 = '皇家交易所 A'code
  • 但仍然出现错误:错误:运算符不存在:布尔 = 整数第 3 行:WHERE ST_Touches(N1.the_geom, N2.the_geom)=1 ^ 提示:没有运算符与给定名称匹配并且参数类型。您可能需要添加显式类型转换。 ********** 错误 ********** 错误:运算符不存在:布尔 = 整数 SQL 状态:42883 提示:没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。字符:132
  • @AndreiIvanov:我添加了一个答案。
【解决方案2】:

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

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'

【讨论】:

  • 因为我的回答涵盖了它,所以这个是多余的。一个简短的评论就可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2016-02-28
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
相关资源
最近更新 更多