Calculate polygon centroid’s coordinates


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:


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.


  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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s