我有两个表,table_a
有多边形和这些多边形的质心。table_b
有另一组点与 中的几何重叠table_a
。
对于每一行,table_a
我需要找到table_b
离该行质心最近的点。
INSERT INTO nearest_node (nearest_drive_node)
SELECT osmid FROM london_drive_nodes
ORDER BY london_drive_nodes.geom <-> nearest_node.lsoa_centroid
LIMIT 1;
这返回
SQL Error [42P01]: ERROR: invalid reference to FROM-clause entry for table "nearest_node"
Hint: There is an entry for table "nearest_node", but it cannot be referenced from
this part of the query.
我不确定如何将值table_a
用作ORDER BY
查询部分中的点。我发现的示例是将单个点的最近邻居作为文本字符串而不是一列点来查找。
将最近的节点作为表中的新行插入,没有任何其他属性,似乎是错误的。您肯定希望更新现有记录。
您必须为输入表的每一行计算最近的节点,这可以通过子查询来实现。
UPDATE nearest_node
SET nearest_drive_node = (
SELECT london_drive_nodes.osmid
FROM london_drive_nodes
ORDER BY nearest_node.geom <-> london_drive_nodes.geom
LIMIT 1
);
如果您只是选择(并最终将此信息插入另一个表中),您将依赖横向连接:
select a.osmid,closest_pt.osmid, closest_pt.dist
from tablea a
CROSS JOIN LATERAL
(SELECT
osmid ,
a.geom <-> b.geom as dist
FROM tableb b
ORDER BY a.geom <-> b.geom
LIMIT 1) AS closest_pt;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句