# Clustering points and summing up attributes per cluster in QGIS

We want to find out the ideal location for sheds and the required dimensions of each shed. As input, we have a point layer with locations (each representing one arable field) and the estimated yield per point (per arable field).

What we now try to do is first to create four clusters for these fields. This should serve as an approximation of the ideal location for a new shed. We've done this by simply using the `"point cluster"` option in the `"symbology"` section and adjusting the distance until only four cluster points remained. These cluster points were then saved as new point shapefile.

How do we determine what points are actually clustered in each of the cluster points?

With this information, we can then sum up the estimated yields for all the single points per cluster. This would allow us to calculate whether the shed has to be designed for 10 tons or 100 tons of grain.

I would try K-means clustering algorithm in the QGIS Processing Toolbox (under `Vector analysis` group).

Just by setting the `Number of clusters` as 4, it will produce a new `Clusters` layer with an attribute field CLUSTER_ID (values= `0, 1, 2, 3`).

Then an expression like `SUM("yield", "CLUSTER_ID")` in the Field Calculator will return the total yield for each cluster. (E.G. the `Sum_per_Cluster` in the below example).

[Update]

To obtain center point per the group (cluster), please try Mean coordinate(s) geoalgorithm in `Processing Toolbox > Vector analysis`.

Mean coordinates dialog window will show an option Unique ID field. Select `CLUSTER_ID` field.

• Thank you very much for the quick and helpful reply! How did you create the center points of the clusters? When I use the method I described in my initial post (using `"Symbology"` and `"Cluster"`), I get center points completely off the actual cluster center (see [ibb.co/VmkhS0x]). This may stem from the different calculation methods. What approach did you use in your example? Furthermore, I get a lot of "NULL" results when I do the K-means clustering (see [ibb.co/VNdQ7bS]). Do you have a solution for this issue? Thank you very much!
– cbr
Commented Mar 29, 2019 at 14:16
• Since I only get error pages when trying to access the uploaded images but cannot edit the comment anymore (>5 min), here other links: Cluster center -> imgur.com/a/fs3R1K1 ; NULL -> imgur.com/a/Cv7IuCr
– cbr
Commented Mar 29, 2019 at 14:23
• @cbr To create center point for each cluster, please use `Centroids` geoalgorithm. I will update my post. As to the center points (red circles) in my example, they were `Point cluster` symbology just for comparison. Commented Mar 29, 2019 at 21:47
• @cbr Your upperleft (north western) cluster in the provided image has only two locations in that cluster, which does not seem right. (You would not build shed just for those two). I am not sure what happened with locations with NULL outputs; they may be outliers. Perhaps I would check their locations visually, and manually assign most appropriate cluster id. Commented Mar 29, 2019 at 22:03
• Thanks very much for your reply! The `centroids` algorithm only returns the same location for each of the selected points (so the output is a layer with the same amount of points as the input layer). I assume that the center point for each point was calculated but not one single point for the whole cluster... Is there another intermediate step necessary I made have missed?
– cbr
Commented Mar 30, 2019 at 7:33