[postgis-users] How to calculate the symmetric difference of linestrings and polygons (i.e. linestrings which do not intersect polygons)

Stefan Keller sfkeller at gmail.com
Mon Jan 31 08:01:35 PST 2011


Just FYI:

I defined some test queries over at postgis-dev mailinglist with the
thread "Behavior of LineString-LineString ST_Intersection and
LineString-Polygon ST_Difference".

Yours, S.

2011/1/30 Stefan Keller <sfkeller at gmail.com>:
> Hi,
>
> Given streets linestrings and forest polygons, think of all streets
> segments which are *not* inside forest areas.
> How can I calculate all segments from linestrings which do not
> intersect polygons?
>
> Any ideas?
>
> - S.
>
> Attempt 1: ST_SymDifference sounds good! But even this equivalent
> produces nonsense:
> # SELECT ST_AsText(
> ST_CollectionExtract(ST_Difference(ST_Union(ls.way,po.way),
> ST_Intersection(ls.way,po.way)),2) )
> FROM linestring ls, polygon po
>
> Attempt 2:
> # SELECT ST_AsText(ST_Intersection(ls.way, po.way)) FROM linestring
> ls, polygon po
> gives me all streets *inside* forests. But I'd like get the
> "remainder" in between...
>
> Attempt 3:
> # SELECT ST_AsText(ST_Difference(ls.way, po.way)) FROM linestring ls, polygon po
> gives me two displaced lines for each street (because the resultse
> contains the difference between one line and one polygon), Thesse two
> could be intersected - but ST_Intersection of two overlapping lines
> (with same nodes except end nodes) does'nt seem to work.
>
> Attempt 4:
> In a very desperate idea, I calculcated the extent of both tables in
> order generate a huge boundary polygon with has as many wholes as
> there are polygons. Then I could do an ST_Intersect of this big swiss
> cheese polygon with the lines - but no luck.
>
> Attempt 5:
> I finally tried to dump all points from Attempt 2 (intersection) to
> get hold of the boarder points where lines and polygons cross.
> But when tried to split a line at a point and to sort out all lines
> outside polygons things got again complicated..
>



More information about the postgis-users mailing list