QGIS 3 Tutorial – Getting String Value Percentages for Point Layer within Another Polygon

qgis-3spatial statisticsstring

I have two layers. One with polygons of areas of a city. Another one – with points corresponding to buildings. In point layer there is a field with the names of management companies of that buildings. I need to calculate the percentage of management companies (unique strings) in each area (polygon).

I tried to use Join attributes by location (summary) and v.vect.stats, but both wants my attributes to be numerical, not strings. Any suggestions?

enter image description here

Best Answer

You can calculate the number of points with the same management company within each of the polygons using this expression with the aggregate function count, where company is the field containing the management company and polygon is the name of your polygon layer:

count ($id, group_by:= company || array_first(overlay_within('polygon', $id)))

It calculates the number of features, grouped by a string concatenated from the company's name and the id of the polygon it lies within. Simply leave away the company || part to get the whole number of points per polygon and thus calculate the percentage using this expression:

count (
    $id, 
    group_by:= company || array_first (overlay_within('polygon', $id))
) /
count (
    $id, 
    group_by:= array_first(overlay_within('polygon', $id))
) *100
Related Question