【问题标题】:Issue with ST_CONTAINS and ST_WITHIN in PostGISPostGIS 中的 ST_CONTAINS 和 ST_WITHIN 问题
【发布时间】:2019-08-06 01:33:32
【问题描述】:

我的环境: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) ) ;

【问题讨论】:

    标签: postgis


    【解决方案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;
    

    【讨论】:

    • 你认为我可以使用这里的转换来解决这个问题吗:SELECT ST_WITHIN( CAST(bounds as GEOMETRY) , CAST(bounds as GEOMETRY) ) FROM m_polygon;
    • 选择你的战斗。它可能有效,但您将无法使用空间索引,并且每次您想使用 Postgis 函数时都必须进行强制转换......只需在第一个位置使用正确的数据类型!
    猜你喜欢
    • 1970-01-01
    • 2016-06-28
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多