在两个或多个行政区域内有交点的格网怎么办?它是随机 Select 的,还是最大的区域,还是根据质心 Select 的?
随机地,由于update..from
的工作方式:
使用FROM
时,您应该确保联接最多为要修改的每一行生成一个输出行.换句话说,一个目标行不应该连接到另一个表(S)中的多行.如果是这样,那么只有一个联接行将用于更新目标行,但不能很容易地预测将使用哪一个.
即使您看到它最终使用的那些代码具有一定的可重复性,它也是不可靠的.也就是说,你可以纠正你的查询,根据你自己的标准,比如质心、重叠区域的接近程度,或者按字母顺序 Select the one with lower/earlier id个,明确地 Select 你想要的那个.
添加子查询distinct on
、order by..limit 1
或row_number()over()
.<<->>
bbox质心距离运算符可能很有用:demo
with cte as (select distinct on (tg.ctid) tg.ctid, data1
from table_grid tg
join table_administrative ta
on ST_Intersects(tg.geom,ta.geom)
order by tg.ctid, tg.geom <<->> st_centroid)
update table_grid as this
set data1 = cte.data1
from cte
where this.ctid=cte.ctid
你可能也会对ST_Subdivide()
个感兴趣.如果你用它来细分行政区域,所有这些join
(以及你的update
隐含地加入)将会快得多.为了提高性能,将ST_Intersects()
降级到普通边界框交叉点&&
也会更安全一些,以防您计划坚持"直接"更新,而不是考虑随机解析的纽带:
update __test.table_grid
set data1 = table_administrative.data1
from __test.table_administrative
where table_grid.geom && table_administrative.geom;