<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.E-postmall17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="SV" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Hi, I’ve succeeded in creating the table as you suggested Martin (Thanks!). With some small modifications (CollectionExtract etc.)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">The problem is that I would like to transfer all attributes from the original file to the result file. I’ve tried so many ways but I can’t get
 it right.   <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Drop table if exists tmp;<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Drop table if exists tmp22;<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Create table tmp as
<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">WITH<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">data AS (<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">   SELECT * FROM "LayerToBeCut" AS t(id, geom)<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">),<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">cutter AS (<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">   SELECT * FROM "LayerToCutWith" AS t(geom)<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">)<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">SELECT id,
<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">    CASE WHEN cutting IS NULL THEN geom
<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">        ELSE st_collectionExtract(ST_Split( geom, cutting ),2) END AS geom
<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">FROM (SELECT id, d.geom geom,
<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">            (SELECT ST_Collect(c.geom) geom<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">                FROM cutter c WHERE ST_Intersects(d.geom, c.geom)<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">            ) AS cutting<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">    FROM data d) AS t;<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">CREATE TABLE "tmp2" AS SELECT *, (ST_Dump(geom)).geom AS the_geom FROM "tmp";<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">ALTER TABLE "tmp2" DROP COLUMN IF EXISTS geom;</span></b><b><span lang="EN-US"><o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">Från:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> postgis-users [mailto:postgis-users-bounces@lists.osgeo.org]
<b>För </b>Martin Davis<br>
<b>Skickat:</b> den 6 september 2019 00:02<br>
<b>Till:</b> PostGIS Users Discussion<br>
<b>Ämne:</b> Re: [postgis-users] breake lines<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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, <br>
    CASE WHEN cutting IS NULL THEN geom <br>
        ELSE ST_Split( geom, cutting ) END AS geom <br>
FROM (SELECT id, d.geom geom, <br>
            (SELECT ST_Collect(c.geom) geom<br>
                FROM cutter c WHERE ST_Intersects(d.geom, c.geom)<br>
            ) AS cutting<br>
    FROM data d) AS t<br>
;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">;<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Thu, Sep 5, 2019 at 2:44 PM Martin Davis <<a href="mailto:mtnclimb@gmail.com">mtnclimb@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">On Thu, Sep 5, 2019 at 6:38 AM <<a href="mailto:paul.malm@lfv.se" target="_blank">paul.malm@lfv.se</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><o:p> </o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Result<b><u>: all of the lines</u></b> from LayerToBeSplitted where copied into tmp2<b><u>,
 not just the ones NOT intersecting</u></b>, and I don't know how to</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">get the attributes to be transferred to tmp2 for those new lines.</span><o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">To get the lines from LayerToBeSplitted which don't intersect ANY lines from LayerToSplitWith, you need to use this SQL pattern:<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">SELECT d.* FROM LayerToBeSplitted d<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">LEFT JOIN LayerToSplitWith c ON ST_Intersects(d.geom, c.geom)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">WHERE c.geom IS NULL<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">(Your query is including lines if there is SOME cutting line which isn't intersected, which will generally always be the case)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Here's an updated example query which does both split and non-split together:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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 * FROM (SELECT id, ST_Split( d.geom, <br>
            (SELECT ST_Collect(c.geom) geom<br>
                FROM cutter c WHERE ST_Intersects(d.geom, c.geom)<br>
            )) geom<br>
    FROM data d) AS t WHERE geom IS NOT NULL<br>
UNION<br>
SELECT <a href="http://d.id" target="_blank">d.id</a>, d.geom<br>
    FROM data d<br>
    LEFT JOIN cutter c ON ST_Intersects(d.geom, c.geom)<br>
    WHERE c.geom IS NULL;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</body>
</html>