# Calculate weighted centroid of a raster layer using GRASS?

I'm trying to calculate the weighted centroid of some raster layers I have, which are basically patches of positive values in a sea of null values.

I know I could convert thoses patches to binary data (1 inside a patch, null outside), vectorize them, calculate the centroid of each patch and then the "centroid of the centroids". But I really need to keep a coefficient corresponding to the value of each pixel in the original raster layer (I want that the core of each patch with higher values influence the location of the centroid).

It is not a problem in my case that the centroid will be outside existing patches, but I simply don't know how to perform this calculation from the raster layer using GRASS. Does any one know how to to this?

If I understand correctly, you want a single center of gravity for the whole raster? If that's the case, then I suggest to convert the raster to a vector points map, then run the script `v.weighted.centroids` (accessible here ) that builds three vector outputs: the weighted center of gravity, the standard deviational ellipse and the standard distance.

Below is an example. To simulate your raster of clusters I simply clipped a DEM raster at a certain elevation, so all the peaks became clusters, then I exported the raster as vector points. Here are the steps (in a GRASS session):

``````r.to.vect clusters out=cluster_pts type=point column=weights --o
python v.weighted.centroid.py input=cluster_pts output=weighted weights_column=weights -d
``````

HTH, Micha • Thanks a lot, this is exactly what I was looking for! – Cademize Mar 4 '19 at 11:13