简体   繁体   English

如何仅在特定(更大)多边形中创建多边形对并计算该对中包含的所有点?

[英]How to create pairs of polygons Only in a specific (bigger) polygon and count all that points that are included in that pairs?

I am trying to create pairs of polygons in a bigger polygon (a chosen island) and also I would like to count all the points that are included in each one of the smaller polygons.我正在尝试在更大的多边形(选定的岛屿)中创建多边形对,并且我想计算每个较小多边形中包含的所有点。 At first, I was trying the above and I get all the pairs, also from other islands.起初,我正在尝试上述方法,我得到了所有的对,也来自其他岛屿。 The tables that I am using are我正在使用的表是

 Table 1: polygon (polygon_id,polyg_geom, name) Table 2: polyg_island (polyg_island_id,polyg_island_geom,name_island) Table 3: pos (pos_id,point,address)

 SELECT P1.name, P2.name FROM polygon P1, polygon P2 WHERE ST_TOUCHES(P1.polyg_geom, P2.polyg_geom) AND P1.polyg_id < P2.polyg_id;

When I was trying to join the table of island polygon, in order to reduce the result to one island, the query returns empty.Getting that error, it was difficult to add the part of counting the points in each pair.当我试图加入岛多边形表时,为了将结果减少到一个岛,查询返回空。得到那个错误,很难添加计算每对中的点的部分。

 SELECT P1.name, P2.name FROM polygon P1, polygon P2 JOIN polygon_island ON ST_Overlaps(polyg_geom,polyg_island_geom) AND polygon_island.name_island='...' WHERE ST_TOUCHES(P1.polyg_geom, P2.polyg_geom) AND P1.polyg_id < P2.polyg_id ;
Is there any suggestion on what I am doing wrong at this stage. 在这个阶段我做错了什么有什么建议吗? Thank you! 谢谢!

Edited answer: The query that I have written below without (with) is running, except from when I am inserting that part.It returns empty.编辑后的答案:我在下面写的没有 (with) 的查询正在运行,除了当我插入那部分时。它返回空。

 WHERE ST_Overlaps(P1.polyg_geom,(select island_geom from polyg_island WHERE name='...'))AND ST_Overlaps(P2.polyg_geom,(select island_geom from polyg_island WHERE name='..'))AND

 SELECT P1.name AS P1_name, (SELECT count(id_pos) FROM pos WHERE ST_Contains(P1.polyg_geom,point))AS count1, P2.name as P2_name, (SELECT count(id_pos) FROM pos WHERE ST_Contains(P2.polyg_geom,point)) AS count2 FROM polygon P1, polygon P2,pos,polyg_island WHERE ST_Overlaps(P1.polyg_geom,(select island_geom from polyg_island WHERE name='...')) AND ST_Overlaps(P2.polyg_geom,(select island_geom from polyg_island WHERE name='..')) AND ST_TOUCHES(P1.polyg_geom, P2.polyg_geom) AND P1.polyg_id < P2.polyg_id;

Without any data or sample of an expected result it is quite hard to figure out what exactly your use case is.如果没有任何数据或预期结果的样本,很难弄清楚您的用例到底是什么。 But I believe you're just missing a single step.但我相信你只是错过了一步。 Maybe thisCTE will help you:也许这个CTE会帮助你:

WITH j AS (
  SELECT P1.polyg_geom AS poly_p1,
         P2.polyg_geom AS poly_p2
  FROM polygon P1, polygon P2
  WHERE ST_TOUCHES(P1.polyg_geom, P2.polyg_geom)
    AND P1.polygon_id < P2.polygon_id)
SELECT *, 
   (SELECT count(*) FROM pos
    WHERE ST_Contains(poly_p3.polyg_island_geom,point))
FROM polyg_island poly_p3,j
WHERE ST_Contains(j.poly_p1,polyg_island_geom) AND
      ST_Contains(j.poly_p2,polyg_island_geom);

Or maybe this或者这个

WITH j AS (
  SELECT P1.polyg_geom AS poly_p1,
         P2.polyg_geom AS poly_p2,
         P1.name AS p1_name, P2.name AS p2_name
  FROM polygon P1, polygon P2
  WHERE ST_TOUCHES(P1.polyg_geom, P2.polyg_geom)
    AND P1.polygon_id < P2.polygon_id)
SELECT 
  j.p1_name, (SELECT count(*) FROM pos WHERE ST_Contains(j.poly_p1,point)),
  j.p2_name, (SELECT count(*) FROM pos WHERE ST_Contains(j.poly_p2,point))
FROM j

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM