I have a load of transect data (in the form of a line shapefile) covering a very large area (approx. 11,900km2) that has been split into 20m x 20m grid squares on QGIS using:

"Vector -> Research Tools -> Create Grid..."

I would like to know how to calculate (without having to physically count) how many of these grid squares my line shapefile covers.

I can only really do simple things on QGIS and don't really use it extensively, so I'm seeking a step-by-step description.

  • 1
    That's one one the most commonly asked question ... did u search a bit on the site ?
    – Snaileater
    Apr 6, 2020 at 16:25
  • yes I did and I didn't understand, thanks. Apr 6, 2020 at 16:41

1 Answer 1


You can use the Field Calculator to create a new field in your line layer using one expression that counts the intersections.

Open the Attribute table of your line layer. Click in the Field Calculator icon

enter image description here

This will open a new window: here you can use create a new field (numeric, Whole integer, 10 of length) and use the following expression

filter:=intersects(($geometry), geometry(@parent)))

enter image description here This will create a new field with the count of how many quadrants of the grid each line is intersecting.

enter image description here

Not completely sure about your request: if you want to know the total of the grid quadrants intersected by the lines you can use the same system with this slightly different expression:

aggregate(layer:='Grid', aggregate:='count', expression:=$id, filter:=intersects(($geometry), geometry(@parent))))

This is the result of the second expression:

enter image description here

Probably not the best visualisation to show but I hope it is a good example to show you the result.

  • I'm getting this error, "couldn't add a new field to the provider". Can you tell me why?
    – Strange
    Apr 24, 2020 at 7:38
  • It is a completely different question. You can solve re-uploading the layer in the layer manager.
    – Val P
    Apr 24, 2020 at 7:51
  • Hey @val-p, Can we find the time taken to count these boxes, with the method you've explained? Using some time functions can we extract the time taken. Please help me out with this
    – Strange
    Jun 6, 2020 at 4:36

Your Answer

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

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