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?

1 Answer 1


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

weighted center of gravity example

  • Thanks a lot, this is exactly what I was looking for!
    – Cademize
    Commented Mar 4, 2019 at 11:13

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.