【问题标题】:Calculate the overlap between two unrelated tables in posgres/posgis计算postgres/postgis中两个不相关表之间的重叠
【发布时间】:2013-12-30 06:47:08
【问题描述】:

这既是一个基本的 SQL 问题,也是一个 postgres/postgis 问题。

我有两个表集,在同一个数据库中但来自不同的来源,提供相似但不同的地理数据。

例如,关于第一组的查询

select ST_Area(g.geog), g.geog area1 
from 
(--complicated joins on set1 tables---)
order by area1;

给出(仅显示第一列):

1449088812.57385
2209542976.77606
2419594400.94064
2680798195.72685
2921972342.3048
2936036972.01677
3420807650.41126
3426766723.91276
4098062224.01722
5082190891.49144
5137325111.41293
5726948701.13547

以及第二组的查询:

select ST_Area(r.geog), r.geog area2 
from 
(--some other compilcated joins on set2 tables----) 
order by area2;

给出(仅显示第一列):

1449087081.39959
2209560820.96132
2419587161.04685
2680790097.03592
2922282361.4318
2936033639.82268
3420802892.77572
3427452706.69087
4098057825.46861
5082187140.27675
5137291729.45781
5726925279.07641

它们都是 12 行。我想相应地并排比较它们。具体来说,我想使用两个查询结果中的“geog”列来计算重叠几何,可能使用 ST_Intersection() 或其他 postgis 方法。 (此处的区域仅用于演示目的。)

我该怎么做呢?我试过了: 行号() 与...作为... 创建临时表... 进入#temp ...

请指点我正确的方向!

【问题讨论】:

  • 你能用前几位或前三位数字比较它们吗?也许只是一个想法

标签: mysql sql postgresql postgis psql


【解决方案1】:

我没有您的完整查询,但根据您提供的结果,您可以:

WITH CTE1 AS
(
    SELECT ROW_NUMBER() OVER(ORDER BY col1) AS ROWNUM, col1 FROM tab1
),
CTE2 AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY col1) AS ROWNUM, col1 FROM tab2
)
SELECT CTE1.col1 AS RES_T1, CTE2.col1 AS RES_T2
FROM CTE1 FULL JOIN CTE2 ON CTE1.ROWNUM = CTE2.ROWNUM

sqlfiddle demo

希望您可以根据您的查询进行调整。

【讨论】:

    【解决方案2】:

    你可以使用类似的东西:

    SELECT DISTINCT ON (a.id)
      a.id as a_id,
      b.id as b_id,
      ST_Area(ST_Intersection(a.geom, b.geom)) as intersect_area
    FROM a, b
    ORDER BY a.id, ST_Area(ST_Intersection(a.geom, b.geom)) DESC
    

    它将为a 中的每一行找到最佳ST_Area(ST_Intersection(a.geom, b.geom))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2015-01-25
      • 2019-06-29
      • 2012-03-11
      • 1970-01-01
      相关资源
      最近更新 更多