PostGIS 中 ST_CONTAINS 和 ST_WITHIN 的问题

Issue with ST_CONTAINS and ST_WITHIN in PostGIS

我的环境:PostgreSQL 11.4 和 PostGIS 2.5.2

CREATE TABLE m_polygon (id SERIAL PRIMARY KEY, bounds POLYGON);
INSERT INTO m_polygon(bounds) VALUES( 
  '(0.0, 0.0),  (0.0, 10.0), (10.0, 0.0), (10.0, 10.0), (0,0)' 
);

SELECT ST_WITHIN(m_polygon.bounds , m_polygon.bounds ) FROM m_polygon;

我收到上述 SELECT 语句的错误消息:

ERROR:  function st_within(polygon, polygon) does not exist 
HINT:  No function matches the given name and argument types. You might 
need to add explicit type casts

我在想错误的原因是什么:ST_WITHIN 参数类型应该是 GEOMETRY,但我传递的是 POLYGON。

但是下面的方法有效:

SELECT ST_WITHIN(ST_MakePoint(1,1), ST_MakePoint(1,1) ) ;

POLYGON 是 Postgres 原生类型。 Geometry 是 PostGIS 中使用的类型。 ST_... 函数是 Postgis 函数。

请注意,您可以将 PostGIS 几何图形限制为特定子类型 (geometry(POLYGON))

如果您不需要 PostGIS,则需要使用 native geometry operators

如果您要使用空间数据,并且由于您已经拥有 PostGIS,最好切换到真实几何:

CREATE TABLE m_polygon (id SERIAL PRIMARY KEY, bounds geometry(POLYGON));
INSERT INTO m_polygon(bounds) VALUES( 
  st_geomFromText('POLYGON((0.0 0.0, 0.0 10.0, 10.0 10.0, 10.0 0.0, 0.0 0.0))') 
);

SELECT ST_WITHIN(m_polygon.bounds , m_polygon.bounds ) FROM m_polygon;