# How to calculate how many grid squares a line shapefile covers

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.

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

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 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

``````aggregate(layer:='Grid',
aggregate:='count',
expression:=\$id,
filter:=intersects((\$geometry), geometry(@parent)))
`````` This will create a new field with the count of how many quadrants of the grid each line is intersecting. 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:

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

``````

This is the result of the second expression: 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? Apr 24, 2020 at 7:38
• It is a completely different question. You can solve re-uploading the layer in the layer manager. 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 Jun 6, 2020 at 4:36