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.

enter image description here

2 Answers 2


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

FROM LineTable l
    INNER JOIN PolygonTable p ON ST_Crosses(LINE,POLYGON)
WHERE p.ID = ??


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.

    , 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"
    ('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))
    ,(VALUES(ST_GeomFromText('POLYGON((0 0, 50 0, 50 50, 0 50, 0 0))')))P(POLYGON);
  • Very nice illustration. Aug 4, 2016 at 9:24

ST_Within should do.

ST_Within — Returns true if the geometry A is completely inside geometry B

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.