I am trying to count how many times a polyline with a specific attribute intersects a polygon in QGIS. For example, I have a polyline with a feature class called "size", this contains a text value i.e. small, medium & large. I have also made a grid of 100m x 100m squares.

I would like to be able to count how many "large" polylines intersect each grid square polygon, and then repeat this for "medium" etc.


In database/DB manager/Virtual Layers/Qgis layers you can try the following query :

select count(*), line_id, grid_id from line_layer, grid_layer
where st_intersects(line.geometry, grid_geometry)
and line.width='large'
group by grid_id

You'll have to adapt the attributes according to your data structure.

  • Thanks. I would also like to populate a new attribute field in the polygon grid shapefile with a count for each large, medium and small. For example, the "count_large" field would contain the value of how many lines with "size" attribute "large" intersect each grid polygon. – Geodatsci Aug 29 '18 at 11:02
  • 1
    The previous query almost does the job. It gives u your count_large and your grid_id. All you have to do is joining the result to your original grid table. You will have to base the join on "grid_id". After the join is made you can save the final result to create a new table containing the new columns you added. – Snaileater Aug 29 '18 at 11:37

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.