<div dir="ltr"><div><div><div><div><div><div><div>Hello,<br><br></div>I am not very experienced in PostGIS and I have a problem which seems to take forever to solve.<br><br></div>I have a line layer (roads) and a polygon layer(localities) and I want to select all parts of the roads that are outside localities.<br>
<br></div>First, I removed all the roads that are completely inside or completely outside my polygons so the only ones remaining are the ones that cross the polygons.<br><br></div>Than I tried two approaches.<br><br></div>
First: <br>create table polyunion as select ST_Multi(ST_Collect(geom)) from poly; (ST_Union gave topology errors or took forever)<br><br>create table rddifference as (select<br> ST_Difference(r.geom,c.geom) as geom<br> from roa r join polyunion c on ST_Intersects(c.geom,r.geom));<br>
<br></div>This one is running for three days now and does not seem to end soon.<br><br><br></div>The second one, without doing the union on the polygons:<br><br><div><div>create table rddifference2 as (select<br> ST_Difference(r.geom,c.geom) as geom<br>
 from roadstoprocess r join localities c on ST_Intersects(c.geom,r.geom));<br><br></div><div>This one joins each of the lines with each intersecting polygon and returns the difference between the line and each intersecting polygon. Therefore when the line intersects ten polygons I get ten geometries containing different parts from the same input line.<br>
<br></div><div>Do you have any suggestions on how I could do this thing in a faster/better way?<br><br></div><div>Thanks<br></div></div></div>