QGIS Calculation – Calculate Percentage of Vector Point Attributes Within a Polygon in QGIS

qgisvector-layer

I have a vector point layer with an attribute Customer with boolean values true and false. I want to aggregate this data by polygons, such that for a given polygon, it shows that Customer is XX% true and YY% false. I can't achieve this with join attributes by location (summary); I can only get Customer_COUNT from this.

Attached an example for reference — I basically want to get the % blue and green points within a polygon.

Example

Best Answer

You can use field calculator with overlay_contains() (or eventually overlay_intersects() depending on your needs) expression on your polygons for this. To get the count for each category simply set a filter to your needs, for example:

array_length(overlay_contains('points',$id,filter:="cat"=0))

respectively for the other category

array_length(overlay_contains('points',$id,filter:="cat"=1))

Then calculate the percentage from these. So all in one:

array_length(overlay_contains('points',$id,filter:="cat"=1))
/
array_length(overlay_contains('points',$id))

Just replace points with the name of your points layer and "cat"=1 with your field containing the category and the desired value of this category.

Related Question