如何将多线串拆分为重叠的 2 点子集(2 点线串)?

how to split multilinestring into overlapping 2-point subsets (2-point linestrings)?

我想将多线串或像 LINESTRING(0 0, 1 0, 1 1, 0 1) 这样的线串拆分成由两点组成的线串。

所以上面的线串会变成:

我也希望能够对多线串做同样的事情。

使用 LATERAL with ST_DumpPoints to dump the linestring's points and in the SELECT clause use ST_MakeLine with the point of the current row and the preceding row using the window function LAG():

SELECT * FROM (
  SELECT ST_MakeLine(LAG(j.geom) OVER w,j.geom) AS line
  FROM t,LATERAL ST_DumpPoints(geom) j(path,geom)
  WINDOW w AS (PARTITION BY gid ORDER BY j.path 
               ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)) i
WHERE line IS NOT NULL;

注意:框架ROWS BETWEEN 1 PRECEDING AND CURRENT ROW在这种情况下几乎是可选的,但保持window函数尽可能明确被认为是一个很好的做法可能,这样您就可以始终准确地知道发生了什么,而不必依赖您的记忆或在文档中查找。这也应该有效:

SELECT * FROM (
  SELECT ST_MakeLine(LAG(j.geom) OVER w,j.geom) AS line
  FROM t,LATERAL ST_DumpPoints(geom) j(path,geom)
  WINDOW w AS (PARTITION BY gid ORDER BY j.path)) i
WHERE line IS NOT NULL;

演示:db<>fiddle