I want to select lines that only touch both sides of a polygon. Is there a function in postgis that can do this? See image below. I only want to select lines from the circle on the left where the line touches both sides of the polygon.

enter image description here

  • Is it right to say that both the start point and the end point of the line must touch the outer ring of the polygon?
    – user30184
    Commented Oct 18, 2018 at 7:07
  • and the line must be otherwise totally inside the polygon?
    – Spacedman
    Commented Oct 18, 2018 at 7:15
  • so that ST_Split postgis.net/docs/ST_Split.html "split polygon by line" creates two polygons and "split line by polygon" does not split the line
    – user30184
    Commented Oct 18, 2018 at 7:21
  • ...and the larger of both possible great circle segments between start/end points does not intersect the interior?
    – geozelot
    Commented Oct 18, 2018 at 7:36

1 Answer 1


I would say that to fit your description, the line should be :

  • Completely inside the polygon
  • Have its start and end points touching the boundary of the polygon

SELECT line_id, geom
FROM line a
JOIN polygon b ON ST_Within(a.geom,b.geom)
AND ST_Intersects(ST_StartPoint(a.geom),ST_Boundary(b.geom))
AND ST_Intersects(ST_EndPoint(a.geom),ST_Boundary(b.geom))

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.