<div dir="ltr">Hi,<div><br></div><div>You could add a distinct clause after st_difference to filter out duplicate results.</div><div>It may be enough to compare geometries bbox instead of real geometries, depending on your dataset:</div>

<div><br></div><div><span style="font-family:arial,sans-serif;font-size:12.727272033691406px"><div>create table roadsdifference as (</div><div>with diff as (</div><div>    select r.osm_id as osm_id, ST_Difference(r.geom,c.geom) geom</div>

<div>    from</div><div>    roads r join localities c on ST_Crosses(c.geom,r.geom))</div><div>) select distinct on (geom::box2d) geom, osm_id</div><div>from diff;</div><div><br></div><div>Avoid unioning all geometries: index will not work well in this case.</div>

<div><br></div><div>Nicolas</div></span></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 20 February 2014 14:49, mdomnita <span dir="ltr"><<a href="mailto:mdomnita@gmail.com" target="_blank">mdomnita@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have two sets of data: a line table (roads) and a polygon table<br>
(localities)<br>
<br>
Both sets are very large and contain thousands of geometries.<br>
<br>
I need all portions of geometries from the line table that lay outside the<br>
polygon table (in my case all the parts of roads outside localities);<br>
<br>
I tried something like:<br>
<br>
create table roadsdifference as (select<br>
 r.osm_id as osm_id, ST_Difference(r.geom,c.geom) geom<br>
 from<br>
roads r join localities c on ST_Crosses(c.geom,r.geom));<br>
<br>
<br>
And it works if each road joins only one locality. In case I have roads<br>
crossing two or more localities I get partly overlapping multigeometries,<br>
one for each locality (I know this is how a join works).<br>
<br>
I also tried:<br>
<br>
create table localitiesunion as select st_union(geom) geom from localities;<br>
<br>
create table roadsdifference as (select<br>
 r.osm_id as osm_id, ST_Difference(r.geom,c.geom) geom<br>
 from<br>
roads r join localitiesunion c on ST_Crosses(c.geom,r.geom));<br>
<br>
<br>
But this option either takes forever or throws a topology error and crashes.<br>
<br>
As I said, I have about 50k locality polygons and 5 mil. road linestrings.<br>
<br>
Using the Erase function in ArcGIS does the job ok but I have to convert the<br>
tables to shapefiles, process them, convert them back etc. and this is a lot<br>
of time-consuming manual work. I would like an automated solution that I can<br>
include in a script (or an .sql file) and that does not assume buying an<br>
expensive software.<br>
<br>
Any other ways or ideas on how to do this?<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://postgis.17.x6.nabble.com/Erase-from-PostGIS-line-table-using-polygon-table-like-Erase-in-ArcGIS-tp5005691.html" target="_blank">http://postgis.17.x6.nabble.com/Erase-from-PostGIS-line-table-using-polygon-table-like-Erase-in-ArcGIS-tp5005691.html</a><br>


Sent from the PostGIS - User mailing list archive at Nabble.com.<br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><br>
</blockquote></div><br></div>