<div dir="ltr"><div dir="ltr">You have to love how SQL always provides multiple ways of expressing things (and the options increase with each new release of the standard). </div><div dir="ltr"><br></div><div dir="ltr">So here's Solution #3 to this problem. This one uses the nifty Postgres JOIN LATERAL functionality. I think it's the simplest of the 3 alternatives, but I'm not sure how it's performance will compare. Perhaps Paul could provide some performance numbers for comparison? (And of course confirm that it works in the same way as the other queries)<div><br></div><div>WITH<br>data AS (<br> SELECT * FROM (VALUES<br> ( 1, 'LINESTRING (100 100, 400 100)'::geometry ),<br> ( 2, 'LINESTRING (100 300, 400 300)'::geometry ),<br> ( 3, 'LINESTRING (250 400, 400 400)'::geometry ),<br> ( 4, 'LINESTRING (-10 -10, -20 -20)'::geometry ),<br> ( 5, 'LINESTRING (-30 -30, -40 -40)'::geometry )<br> ) AS t(id, geom)<br>),<br>cutter AS (<br> SELECT * FROM (VALUES<br> ( 'LINESTRING (150 50, 150 350)'::geometry ),<br> ( 'LINESTRING (200 50, 200 150)'::geometry ),<br> ( 'LINESTRING (250 350, 250 250)'::geometry ),<br> ( 'LINESTRING (300 350, 300 50)'::geometry ),<br> ( 'LINESTRING (350 250, 350 450)'::geometry )<br> ) AS t(geom)<br>)<br>SELECT id, CASE WHEN cut.geom IS NULL THEN d.geom <br> ELSE ST_Split( d.geom, cut.geom ) END AS geom <br> FROM data d<br> CROSS JOIN LATERAL <br> (SELECT ST_Collect(c.geom) geom<br> FROM cutter c WHERE ST_Intersects(d.geom, c.geom)<br> ) AS cut;<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Sep 5, 2019 at 3:01 PM Martin Davis <<a href="mailto:mtnclimb@gmail.com">mtnclimb@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Here's a slightly different way to do the same thing. Instead of using a LEFT JOIN to find the non-split lines, it checks whether the result of the ST_Collect of intersecting lines, and then either computes the split or keeps the original input line. </div>
</blockquote></div></div>