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.

### Like this:

Like Loading...

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.

LikeLike

e para obter as colunas da tabela com coordenadas DMS?

LikeLike

In QGIS, the correct fuction blend function with regex:

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

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

LikeLiked by 1 person