SQL select 个多边形内的要素

SQL select features within a polygon

我有以下代码可以正常工作:

select vissoort, count(1), ST_Buffer(ST_GeomFromText('POINT(5.341248 51.615590)',4326):: geography, 2500)
from   visvangsten
where  st_intersects(visvangsten.locatie,
ST_Buffer(ST_GeomFromText('POINT(5.3412480 51.615590)',4326):: geography, 2500))
group by vissoort
order by 2 desc

现在我想要相同的功能,但选择多边形内的要素而不是 circle/buffer。

我尝试过类似的方法,但没有任何效果:

select vissoort, count(1), ST_asText( ST_Polygon('LINESTRING(5.303 51.629, 5.387 51.626, 5.393 51.588, 5.281 51.592)'::geometry, 4326) )
from   visvangsten
where  st_contains(ST_asText( ST_Polygon('LINESTRING(5.303 51.629, 5.387 51.626, 5.393 51.588, 5.281 51.592)'::geometry, 4326) ), visvangsten.locatie);
group by vissoort
order by 2 desc limit 1

数据库 table 如下所示:

id ([PK]bigint) datum(date) vissoort(character varying) locatie(geometry)
15729 2007-06-23 Blankvoorn 0101000...etc.
etc. etc. etc. etc.

有人知道答案吗?

请记住,要将 LineString 转换为多边形,您需要有一个闭合环 - 换句话说,第一个和最后一个坐标对 必须相同 。也就是说,您可以使用函数 ST_MakePolygon 将 LineString 转换为 Polygon。以下示例可能是您要查找的内容:

WITH j (geom) AS (
  VALUES 
    (ST_MakePolygon('SRID=4326;LINESTRING(-4.59 54.19,-4.55 54.23,-4.52 54.19,-4.59 54.19)'::geometry)),
    (ST_Buffer('SRID=4326;LINESTRING(-4.59 54.19,-4.55 54.23,-4.52 54.19,-4.59 54.19)'::geometry,0.1))
)
SELECT ST_Contains(geom,'SRID=4326;POINT(-4.5541 54.2043)'::geometry) FROM j;

 st_contains 
-------------
 t
 t
(2 Zeilen)