postgresql - st_intersect() 在 postgresql 中不起作用

标签 postgresql geospatial postgis spatial-index spatial-query

我正在使用 postgresql 版本: “x86_64-unknown-linux-gnu 上的 PostgreSQL 9.3.1,由 gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) 编译,64 位”

我创建了 2 个表 A 和 B,数据类型为点和多边形。 现在我想知道点是否在多边形内。 为此,我正在尝试使用 ST_Intersect(A.point_LatLong, B.polygon_abc); 我的查询是:

SELECT A.id 
FROM A, B 
WHERE A.name = 'callifornia' 
AND ST_Intersect(A.point_LatLong , B.polygon_abc); 

这里的point_latLongpolygon_abc是A表和B表中数据类型为点和多边形的列名。

但是这个查询给出了一个错误:

ERROR: function st_intersect(point, polygon) does not exist
LINE 3: WHERE city.city_name = 'callifornia' AND ST_intersect(city.c...
HINT: No function matches the given name and argument types. You might need to add
explicit type casts.


我该如何解决这个问题?我什至无法在 postgresql 中使用任何其他空间方法,如 st_contains() 等,如果您有任何解决方案,请告诉我。

最佳答案

您正在尝试将 PostgreSQL 的内置(有限但有用)几何类型与 PostGIS 函数混合使用。听起来您也没有安装 PostGIS。您还打错了函数名称,它是 ST_Intersects 而不是 ST_Intersect

首先,如果您想使用 PostGIS,请确保已安装它,然后:

CREATE EXTENSION postgis;

接下来,您可能会发现实际上无法调用带有多边形ST_Intersects。 PostGIS 使用它自己的 geometry 类型。它有一些用于 PostgreSQL 内部类型的转换器,但它们只是有限的。因此,使用原始几何类型调用 PostGIS 函数可能会导致如下错误:

postgres=# SELECT ST_Intersects( polygon( box(point(0,0), point(10,10)) ), point(5,5) );
ERROR:  function st_intersects(polygon, point) does not exist
LINE 1: SELECT ST_Intersects( polygon( box(point(0,0), point(10,10))...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

您通常需要将它们转换为 PostGIS 自己的geometry 类型。 PostGIS 为大多数类型提供了一些显式转换,例如:

postgres=# SELECT ST_Intersects(
    polygon( box(point(0,0), point(10,10)) )::geometry,
    point(5,5)::geometry 
);
 st_intersects 
---------------
 t
(1 row)

所以在您的查询中,那将是:

ST_Intersects(A.point_LatLong::geometry , B.polygon_abc::geometry);

关于postgresql - st_intersect() 在 postgresql 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21302035/

相关文章:

sql - 跨表实现约束的最佳方式是什么?

postgresql - 将 "many-to-many"表行内连接为数组

c# - 通过 Massive ORM (ExecuteNonQuery)、UdtTypeName 错误更新带有空间数据类型的 SQL Server 2008 记录

MySQL - 地理空间对象

postgresql - 如何检查多线串是否真的是多线串?

mysql - 如何为多个连接编写子查询sql

PostgreSQL 修剪过多的尾随零 : type numeric but expression is of type text

r - 如何通过点之间的插值绘制不规则空间数据?

linux - 提示 : No function matches the given name and argument types. `json_build_object`

postgis - 如何使用 Geo 库创建有效的 Ecto 模型变更集?