[GIS] How to count the number of polygons within a grid

qgisvector-gridvector-layer

I have two vector layers: 1) with polygons representing landcover types and 2) a grid, overlapped with the first one.

I want to obtain, for each square within the grid, the number of land cover types. For example:
enter image description here

In the red square it would have 2 landcover types (2 and 7), and the left square to that one only 1 land cover type (2).

How do I obtain this values, using QGIS?

Best Answer

Add a virtual layer (layer > add layer > add/edit virtual layer)

Import both grid- and landcoverlayer

In the query-window you can add following code. In the code you will have to change grid, landcover and type to what your layers and attributes are named of.

select grid.*, group_concat(landcover.type) as types from grid, landcover where st_intersects(grid.geometry, landcover.geometry)

Now you should get a new layer with a grid extended by an attribute called 'types'

Related Question