在 PostGIS 中转换地理类型

发布于 2024-11-16 17:47:46 字数 1277 浏览 3 评论 0原文

我定义了下表,它使用 PostGIS 地理。

CREATE TABLE test_geog (
    id SERIAL PRIMARY KEY,
    boundary GEOGRAPHY(Polygon)
);

我将以下测试多边形添加到表中:

INSERT INTO test_geog VALUES (ST_GeographyFromText('SRID=4326;POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'));

我试图确定一个点是否位于该表中的任何多边形内。我有这个查询:

SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'), area)
FROM (SELECT boundary FROM test_geog) AS area;

这会产生以下错误:

ERROR:  function st_containsproperly(geography, record) does not exist
LINE 1: SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'...
            ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

如何将此“记录”转换为POLYGON?我很困惑,因为似乎该列已被声明为仅保存 POLYGON 类型,但由于某种原因,这不是我从数据库中提取的内容。

我尝试将记录转换为 POLYGON ,如下所示:

SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'), CAST (boundary AS POLYGON))
FROM (SELECT boundary FROM source_imagery) AS nitf_area;

但这给了我这个错误:

ERROR:  cannot cast type record to polygon
LINE 1: ...tainsProperly(ST_GeographyFromText('Point(2 2)'), CAST (boun...

我在这里不明白什么?

I have the following table defined, which uses PostGIS geography.

CREATE TABLE test_geog (
    id SERIAL PRIMARY KEY,
    boundary GEOGRAPHY(Polygon)
);

I added the following test polygon to the table:

INSERT INTO test_geog VALUES (ST_GeographyFromText('SRID=4326;POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'));

I am trying to determine whether a point lies within any of the polygons in this table. I have this query:

SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'), area)
FROM (SELECT boundary FROM test_geog) AS area;

This yields the following error:

ERROR:  function st_containsproperly(geography, record) does not exist
LINE 1: SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'...
            ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

How do I convert this "record" into a POLYGON? I'm confused because it seems like the column has already been declared to only hold POLYGON types, but for some reason that is not what I am pulling out of the database.

I attempted to cast the record to a POLYGON like this:

SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'), CAST (boundary AS POLYGON))
FROM (SELECT boundary FROM source_imagery) AS nitf_area;

But that gives me this error:

ERROR:  cannot cast type record to polygon
LINE 1: ...tainsProperly(ST_GeographyFromText('Point(2 2)'), CAST (boun...

What am I not understanding here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-11-23 17:47:46

我的问题在 GIS 论坛上得到了解答。我犯了一个愚蠢的错误,认为 ST_ContainsProperly 可以用于地理,而实际上它仅支持几何。我也不需要我想要做的多余的子查询。

这是我的解决方案。我转而使用 ST_Covers,它可以满足我的需求并且支持地理。这可以避免转换为几何图形。这是有效的代码:

SELECT ST_Covers(
    boundary, 
    ST_GeographyFromText('Point(2 2)')
) 
FROM source_imagery

My question got answered over on the GIS forum. I made the silly mistake of thinking ST_ContainsProperly could be used for Geography, when it is actually only supported for Geometry. I also didn't need the superfluous sub-query I was trying to do.

So here is my solution. I switched over to use ST_Covers instead, which does what I wanted and supports Geography. This avoids casting to Geometry. Here's the code that works:

SELECT ST_Covers(
    boundary, 
    ST_GeographyFromText('Point(2 2)')
) 
FROM source_imagery
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文