Friday 14 April 2017

Intersection between line & polygon in PostGIS?


I have a linestrings consist of 2 vertices. I want to get all lines that don't intersects with any polygons on the way. In my example: blue line Not intersects - only touches some polygons, but black line although touches with some polygon, it also intersects another. So in conclusion the result is only blue line.


enter image description here



What I did:


 SELECT lines.geom
FROM lines, polygons
WHERE ST_Intersects(lines.geom, polygons.geom) = false
AND
ST_Touches(lines.geom, polygons.geom) = true;

The ST_Touches seems to work - I guess because the representation of the line-string consist of the vertex that really touches the polygon.


The ST_Intersects not working - The result is that there is no even 1 line that intersect any polygon.


So what I started to thinking: Maybe because in the space where the line intersect polygon - ain't any 'real' representation of the line-string: Because what is line-string? 2 vertices both outside the polygon...



Whats going on? I thought that GEO-DB should deal exactly with this kind of challenges and 'knows' what spatially intersection is...




Altough I don't know how to do this (because ST_Segmentize not worked for me from some reason) I prefer not to overcome this issue with just 'breaking' the line-string to more elements such that with high probability some vertex would be inside the polygon.



Answer



As user30184 noted, you have to use ST_Crosses() instead of ST_Intersects().


Additional, your expression works on one line compared to one polygon only: The black line touches Polygon 1 and doesn't cross Polygon 1. In more detail:


SELECT x FROM line, polygons WHERE ...


means: Build a case from every pair of a line and a polygon, so number of cases is n_lines x n_polygons. Do the WHERE clause for every case and keep cases where it is true. Result: blue line, P1: false, blue line, P2: true, black line, P1: true, black line, P2: false.


Maybe you want something like this:


SELECT lines.geom

FROM lines, polygons
WHERE ST_Touches(lines.geom, polygons.geom) AND
NOT EXISTS (SELECT 1 FROM polygons p2 WHERE ST_Crosses(lines.geom, p2.geom));

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...