I am using QGIS 3.1. I am wondering if there is a way to count points in polygons but by using both the weight and class.

For example, I have distributions of a number of species, each with a unique 'score'. I want to count each occurrence of a unique species (class - species), then weight each occurrence by its score (weight - score) in a grid.

Doing each of these is very simple using the count points in polygon feature but I can't do both at once.


1 Answer 1


You can achieve this using a virtual layer.

It would join the grid layer to the point layer based on spatial intersection, then sum the weights and divide by the count for each grid polygon.

Go to the menu Layer > Add Layer > Add/Edit Virtual Layer... and enter the following query. Replace the layer name for yours

select g.*, sum(p.weight) / count(*)
from grid_layer g 
  join points p 
    on st_intersects(p.geometry,g.geometry)
where  p.class = 'species_abc'
group by g.id
  • I think it should be a double GROUP BY g.id, p.class, therefore each unique point class will be distinguished within a unique polygon
    – Taras
    Jun 22, 2020 at 11:49
  • @NiBau To use other layers, change from grid_layer g join points p ==> from my_grid_layer g join my_point_layer p. If you have a different goal, feel free to ask a new question with enough details!
    – JGH
    Jun 22, 2020 at 12:08

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.