I have a points layer and a polygon layer. The points are placed randomly. Some are within a polygon, some are outside. I would like to write the amount of points inside each polygon as virtual field into it's attribute table. I know, the aggregate function should do the job, but it don't:


I get 0 as result in every (multi-)polygon.

enter image description here

  • 1
    Convert your original geometry from multi to non multi ... Feb 24, 2019 at 15:04
  • 1
    Will you see if changing 'id' -> "id" work?
    – Kazuhito
    Feb 24, 2019 at 21:43
  • Just tested your code 1to1 and its working fine (with single-polygons). Its also working with multipolygons, but of course, in your case if thats one single multipolygon, it will count 6 dots. So the issue must be something else..
    – MrXsquared
    Feb 24, 2019 at 21:58
  • converting the multi to a single polygon was working. No other changes were needed!
    – MartinMap
    Feb 26, 2019 at 21:24

1 Answer 1


The expression works well with multipart polygons, whenever:

  • The geometries are valid.

  • Both layers have the same CRS.

Therefore, you need to check both.

About the 'id' subexpression, the aggregate() function expects a field reference of the joined layer there, so "id" seems to be the correct one if you want to count not null dots ids. However, anything that is written in single quotes is going to be considered valid and all rows being counted regardless of the values of any field.

Your Answer

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

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