从一个 table 最接近另一个 table 的给定行中的点获取最近邻点
Get nearest neighbor point from a table closest to the point in a given row of another table
我有两个表,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;
这个returns
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
部分中的点。我发现的示例是将单个点的最近邻点作为文本字符串,而不是一列点。
问题似乎是您在查询中引用了 nearest_node
,但在 FROM
子句中没有引用,但总的来说,您的查询无论如何都无法工作 "for each row"。尝试将 st_distance
和常规 min
与 group by
组合以获得最小距离,然后将其包装在 CTE 或子查询中以确定它实际上是哪个节点:
WITH distances AS (
SELECT nn.id, nn.lsoa_centroid, min(st_distance(ldn.geom, nn.lsoa_centroid)) as min_distance
FROM london_drive_nodes ldn, nearest_node nn
GROUP BY nn.id
)
INSERT INTO nearest_node (nearest_drive_node)
SELECT ldn.osmid
FROM distances
JOIN london_drive_nodes ldn ON distances.min_distance = st_distance(distances.wgs84_centroid, ldn.wgs84_coordinates)
插入最近的节点作为table中的新行,没有任何其他属性,似乎是错误的。您肯定想要更新现有记录。
您必须为输入的每一行计算最近的节点table,这可以通过子查询来实现。
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(并最终将此信息插入另一个 table),您将依赖横向连接:
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;
我有两个表,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;
这个returns
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
部分中的点。我发现的示例是将单个点的最近邻点作为文本字符串,而不是一列点。
问题似乎是您在查询中引用了 nearest_node
,但在 FROM
子句中没有引用,但总的来说,您的查询无论如何都无法工作 "for each row"。尝试将 st_distance
和常规 min
与 group by
组合以获得最小距离,然后将其包装在 CTE 或子查询中以确定它实际上是哪个节点:
WITH distances AS (
SELECT nn.id, nn.lsoa_centroid, min(st_distance(ldn.geom, nn.lsoa_centroid)) as min_distance
FROM london_drive_nodes ldn, nearest_node nn
GROUP BY nn.id
)
INSERT INTO nearest_node (nearest_drive_node)
SELECT ldn.osmid
FROM distances
JOIN london_drive_nodes ldn ON distances.min_distance = st_distance(distances.wgs84_centroid, ldn.wgs84_coordinates)
插入最近的节点作为table中的新行,没有任何其他属性,似乎是错误的。您肯定想要更新现有记录。
您必须为输入的每一行计算最近的节点table,这可以通过子查询来实现。
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(并最终将此信息插入另一个 table),您将依赖横向连接:
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;