# Merge lines that intersect postgis

These look like three straight lines that could have two endpoints at the extreme vertices

But in reality they are each longer linestrings that have endpoints that are on the interior of the line, basically the line folds in on itself

They started out as separate lines that were parallel, and I tried to join the ones that intersected by using this function:

``````ST_CollectionExtract(unnest(ST_ClusterIntersecting(nl)))
``````

where nl is the column of individual lines.

How can I change this query so I get a linestring made of 2 points where the endpoints are the expected endpoints from looking at the picture (the extreme vertices)

Alternatively, how can I take what I have after this point and turn it into what I want?

Here is an example line that I have: "LINESTRING(-101.47751335594317 31.489023992956714,-101.46871182786654 31.488981396096023,-101.47866855724968 31.489029536221512,-101.46986702785048 31.488987023468102)"

and here is how it should be: "LINESTRING(-101.46871182786654 31.488981396096023,-101.47866855724968 31.489029536221512)" (before these endpoints were on the inside of the line)

• This would be nicely solved by a `ST_MaximumDiameter` function - except that doesn't exist in PostGIS yet. Sep 13, 2022 at 19:56
• I think I solved it by using ST_LongestLine, about to test and then I'll post an answer if it works.
– tbob
Sep 13, 2022 at 20:19
• Right, `ST_LongestLine(A, A)` computes the maximum diameter of A Sep 13, 2022 at 20:26

``````ST_LongestLine((ST_CollectionExtract(unnest(ST_ClusterIntersecting(nl)))), (ST_CollectionExtract(unnest(ST_ClusterIntersecting(nl)))))
``````ST_CollectionExtract(unnest(ST_ClusterIntersecting(nl)))