[英]Show points that are within several overlapped/intersected polygons using ST_Within in PostGIS
I have a separate table for points and polygons.我有一个单独的点和多边形表。 The polygons are overlapped or intersect with each other.
多边形相互重叠或相交。 The points are within several polygons at the same time.
这些点同时位于多个多边形内。 I need to develop an SQL query which should show the points that are in the polygons.
我需要开发一个 SQL 查询,它应该显示多边形中的点。 If I use an SQL query like this:
如果我使用这样的 SQL 查询:
SELECT point_table.geom, polygon_table.name FROM point_table
INNER JOIN polygon_table on ST_Within(point_table.geom, polygon_table.geom);
It does not iterate through all polygons, instead it evaluates the point geometry with the innermost polygon and does not evalute the point geometry with other overlapped or intersected polygons.它不会遍历所有多边形,而是使用最里面的多边形评估点几何,并且不评估具有其他重叠或相交多边形的点几何。 What I need is to evaulate all points with all polygons.
我需要的是用所有多边形评估所有点。 Becuase points can be within several polygons at the same time.
因为点可以同时在多个多边形内。
For instance, if 10 points are within 3 overlapped polygons, it should return 30 records and each record/row should have the corresponding polygon_table.name in it.例如,如果 10 个点在 3 个重叠的多边形内,它应该返回 30 条记录,并且每条记录/行中应该有相应的 polygon_table.name。 And in this situation, the above query is returning 10 records with polygon_table.name from the innermost polygon, ie once it finds the match, it is not evaluating the points against other polygons.
在这种情况下,上述查询从最里面的多边形返回 10 条带有 polygon_table.name 的记录,即一旦找到匹配项,它就不会根据其他多边形评估这些点。
Any help on this matter appreciated.对此问题的任何帮助表示赞赏。
Thanks.谢谢。
Your query looks just fine.您的查询看起来很好。 The error is most probably somewhere else.
错误很可能在其他地方。 To make my point clearer, I will add an example showing two polygons and four points, where three points are within the inner polygon and one point is within the outer one:
为了让我的观点更清楚,我将添加一个显示两个多边形和四个点的示例,其中三个点在内部多边形内,一个点在外部多边形内:
WITH pol(id,geom) AS (
VALUES (1,'SRID=4326;POLYGON((-4.53 54.21000000000001,-4.51 54.21000000000001,-4.51 54.19,-4.53 54.19,-4.53 54.21000000000001))'::GEOMETRY),
(2,'SRID=4326;POLYGON((-4.537478046647734 54.213934294785226,-4.502287464372343 54.213934294785226,-4.502287464372343 54.18506473763151,-4.537478046647734 54.18506473763151,-4.537478046647734 54.213934294785226))'::GEOMETRY)
), poi(id,geom) AS (
VALUES (1,'SRID=4326;POINT(-4.524700698880288 54.20613765421879)'::GEOMETRY),
(2,'SRID=4326;POINT(-4.517588984076674 54.20219317274976)'::GEOMETRY),
(3,'SRID=4326;POINT(-4.524762007263298 54.19627574559112)'::GEOMETRY),
(4,'SRID=4326;POINT(-4.507098941702792 54.211414844419664)'::GEOMETRY))
SELECT poi.id AS point_id, pol.id AS polygon_id FROM poi
JOIN pol ON ST_Within(poi.geom,pol.geom);
point_id | polygon_id
----------+------------
1 | 1
1 | 2
2 | 1
2 | 2
3 | 1
3 | 2
4 | 2
In the resultset you can see that three points are joined with all polygons and one point is joined with a single one, as you can also see in the image above.在结果集中,您可以看到三个点与所有多边形相连,一个点与一个点相连,如上图所示。
Try it yourself: db<>fiddle
自己试试:
db<>fiddle
If it still does not work, provide some sample data and we can test it.如果还是不行,提供一些样本数据,我们可以测试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.