将计算出的多边形面积百分比添加到 postgresql 中的 table

Add the computed percentage of area of polygon into the table in postgresql

我试图找到另一个多边形内多边形的百分比,并想将其写入现有 table。我已经计算了面积,但我无法将其添加到现有的 table.

SELECT 
  ST_Intersection(ST_MakeValid(r.geom),o.buffer) AS intersection,   
  ST_Area(ST_Intersection(ST_MakeValid(r.geom),o.buffer))/ST_Area(r.geom)*100 
FROM public."Residential" r, public."Operation" o 
WHERE ST_Intersects(o.buffer,ST_MakeValid(r.geom)); 

我试过将其添加到现有 table、

ALTER TABLE public."Operation" ADD COLUMN res_percent double precision;
UPDATE public."Operation"  
SELECT      
  ST_Intersection(ST_MakeValid(r.geom),o.buffer) AS intersection,   
  ST_Area(ST_Intersection(ST_MakeValid(r.geom),o.buffer))/ST_Area(r.geom)*100)) 
FROM public."Residential" r, public."Operation" o 
WHERE ST_Intersects(o.buffer,ST_MakeValid(r.geom));

我收到一个错误

ERROR: syntax error at or near "SELECT"

我只想将缓冲区内的住宅多边形(geom)的百分比添加到操作中的新列中table。

题型我在下方添加了fiddle,

dbfiddle

我这里做错了什么,如何纠正?

这修复了语法错误,但不一定进行所需的计算。

没有update . . . select这样的语法。我猜你打算:

UPDATE public."Operation" o
    SET res_percent = ST_Area(ST_Intersection(ST_MakeValid(r.geom), o.buffer)) / (ST_Area(r.geom)*100)
    FROM public."Residential" r
    WHERE ST_Intersects(o.buffer, ST_MakeValid(r.geom));

不幸的是,我们不能像那样组合 select 和更新语句。您宁愿必须在 update 查询中连接两个表来计算面积,例如

UPDATE public."Operation" o
SET res_percent = 
   ST_Area(ST_Intersection(ST_MakeValid(r.geom),o.buffer))/ST_Area(r.geom)*100
FROM public."Residential" r
WHERE ST_Intersects(o.buffer, ST_MakeValid(r.geom));