使用 ST_ClosestPoint、ST_StartPoint 和 ST_EndPoint 查找距离线的起点和终点最近的对象

Using ST_ClosestPoint, ST_StartPoint, and ST_EndPoint to find closest object to beginning and end points of lines

我需要使用来自两个单独表格的几何图形找到最接近直线两端的点。到目前为止,我能够使用以下方法找到端点的几何形状:

SELECT "id", ST_StartPoint(dmp.geom) AS upstream,
       ST_EndPoint(dmp.geom) AS downstream
FROM sewers.pipes,
        LATERAL ST_Dump("geom") AS dmp
;

利用这个,我想用ST_ClosestPoint找到离管道端点最近的结构。这是我到目前为止想出的:

SELECT ST_ClosestPoint('POINT(SELECT ST_StartPoint(dmp.geom) AS upstream
FROM sewers.pipes,
        LATERAL ST_Dump("geom") AS dmp)',
                      ('LINESTRING(SELECT geom from sewers.pipes)'))

但是,这会产生以下错误:

ERROR: parse error - invalid geometry
Line 1: SELECT ST_ClosestPoint('POINT(SELECT ST_STartPoint(dmp.geom)...

HINT: "POINT(SE" <-- parse error at position 8 within geometry
SQL state: XX000
Character: 24

我在下面提供了一些示例数据。

sewers.pipes <-- 这些是行

| id       | geom |
| -------- | -------------- |
| 822      | 0105000020950B00000100000001020000000200000046243EC3282608418D28242D6C1B3D4128531BE88A2608418284B3EF561B3D41        |
| 6660     | 0105000020950B0000010000000102000000020000004ABF2CBC86B108413B93650696323D413C487924CCB10841925D490495323D41            |

sewers.structures <-- 这些是点数

| id        | geom |
| --------  | -------------- |
| 2014      | 0104000020950B00000100000001010000001026FA48113B07410D6A8412CF1D3D41            |
| 22979     | 0104000020950B0000010000000101000000BA1BF246E6DD0741D064CB58C2E43C41            |

我知道选择多个数据点可能会出错,例如。 more than one row returned by a subquery used as an expression,所以这也可能是一个问题。任何帮助将不胜感激。

尝试为 upstreamdownstream 使用不同的 LATERALs:

SELECT p.id,
  (dump_line).geom,
  ST_EndPoint((dump_line).geom) AS downstream,
  geom_closest_downstream,
  ST_StartPoint((dump_line).geom) AS upstream,
  geom_closest_upstream
FROM sewers.pipes p,
  LATERAL ST_Dump(p.geom) dump_line,
  LATERAL (SELECT s.geom 
           FROM sewers.structures s
           ORDER BY ST_EndPoint((dump_line).geom)<->s.geom 
           LIMIT 1) j (geom_closest_downstream),
  LATERAL (SELECT s.geom 
           FROM sewers.structures s
           ORDER BY ST_StartPoint((dump_line).geom)<->s.geom 
           LIMIT 1) i (geom_closest_upstream);
  • 要从 ST_Dump 访问几何图形,您必须将其输出用括号括起来,例如(ST_Dump(multiline)).geom
  • ORDER BY子句中的运算符<->对应距离。因此,将它与 LIMIT 1 结合使用,您只能获得最近的距离。

演示:db<>fiddle