In QGIS I create some lines that cross three polygons. By using intersection, it will create a new shapefile that gives me as a result the lines inside the three polygons.

If I open the attribute of the intersection layer I can see that different line ID have same polygon ID (obviously because multiple lines cross the same polygon). By looking I can tell how many lines are inside a polygon but how can I do this automatically?

I would like to modify the polygon layer attributes by adding a new column with the number of lines crossing each polygon (so the polygon layer will have only two column: "ID" and "number of lines").


1 Answer 1


If you store the result of the intersection into a sql database (SQLite is sufficient), you can use "group by" and "count" to query how many there are.

select polygon_id, count(*) as number_of_crossing_lines
from line_crosses_poly
group by polygon_id

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.