Calculate polygon centroid’s coordinates

EN | PT

I had the need to add columns with the coordinates of polygons centroids. I came up with the following expressions to calculate X e Y, respectively:

xmin(centroid($geometry))
ymin(centroid($geometry))

The expression seems quite simple, but it toke me some time before I realize that, not having a x(geometry) and y(geometry) functions, I could use the xmin() and ymin() to get the coordinates of the polygons centroids. Since this wasn’t the first time I had to use this expressions, this post will work as a reminder for the future.

6 thoughts on “Calculate polygon centroid’s coordinates

  1. This is a lot easier than using the “polygons centroids” then calculate X and Y and then performa a join by location. Thanks this saves a lot of time and you don’t end up with a lot of useless files.

    Like

  2. In QGIS, the correct fuction blend function with regex:

    lon = regexp_substr(geomToWKT(centroid($geometry)), ‘[(](.*)[\\s]’)
    lat = regexp_substr(geomToWKT(centroid($geometry)), ‘[\\s](.*)[)]’)

    Liked by 1 person

Leave a comment