开发者

Casting a Geography type in PostGIS

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

CREATE TABLE test_geog (
    id SERIAL PRIMARY KEY,
    boundary GEOGRAP开发者_运维知识库HY(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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜