I am looking of a postGIS function that will select the highlighted line in the picture below. Using st_intersection I can clip the lines to the polygon, but i'm looking to select the one line that breaks out of the polygon. The trouble is the line originates inside the polygon so st intersects, st crosses, st intersection both return all lines not just the one that breaks out of the polygon.
Asked
Active
Viewed 421 times
2 Answers
3
ST_Crosses is what you want. It will return true if the geometries cross each other. Using that in a join or where clause will return the row(s) that you want.
So you want a query along the following lines
SELECT l.*
FROM LineTable l
INNER JOIN PolygonTable p ON ST_Crosses(LINE,POLYGON)
WHERE p.ID = ??
or
SELECT l.*
FROM LineTable l
WHERE ST_Crosses(LINE,ST_GeomFromText('POLYGON(( ... ))', ST_SRID(LINE)))
Here's a small query that demonstrates a number of the relationship functions. Note that ST_Overlaps doesn't show a true for any of them. It appears to only return TRUE for like geometry types.
SELECT DESCRIPTION
, ST_Intersects(LINE, POLYGON) "Intersects"
, ST_Within(LINE, POLYGON) "Within"
, ST_Contains(LINE, POLYGON) "Contains"
, ST_Touches(LINE, POLYGON) "Touches"
, ST_Disjoint(LINE, POLYGON) "Disjoint"
, ST_Crosses(LINE, POLYGON) "Crosses"
, ST_Overlaps(LINE, POLYGON) "Overlaps"
FROM (VALUES
('Outside', ST_GeomFromText('LINESTRING(10 -10, 90 -10)', 0))
,('Touch', ST_GeomFromText('LINESTRING(10 0, 90 0)', 0))
,('Inside', ST_GeomFromText('LINESTRING(10 10, 40 10)', 0))
,('Pass Through', ST_GeomFromText('LINESTRING(-10 20, 60 20)', 0))
,('Inside to Outside', ST_GeomFromText('LINESTRING(10 30, 60 30)', 0))
,('Outside to Inside', ST_GeomFromText('LINESTRING(-10 40, 40 40)', 0))
) L(DESCRIPTION, LINE)
,(VALUES(ST_GeomFromText('POLYGON((0 0, 50 0, 50 50, 0 50, 0 0))')))P(POLYGON);
MickyT
- 3,430
- 15
- 19
-
Very nice illustration. – John Powell Aug 04 '16 at 09:24
