Given a "road", represented by a line, that crosses a "state", represented by a polygon:

What is the SQL query I can use to find the segment of the road that is within the state?

A concrete example.

I have a single line representing Route 66 which traverses the USA. I also have polygons for each state. I want to find the segment of Route 66 that lies within Texas.


To create an intersection object, or for this case the segment of a line that lies within a polygon, you would use the following PostGIS query:

SELECT a.ID, b.ID, ST_Intersection(a.geom, b.geom)
FROM   thePolygons a
       theLines b
       ON ST_Intersects(a.geom, b.geom) = 1

This would most likely be the results you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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