14

In QGIS I would like to update two columns with the centroid easting and northing of my polygons. I am using the field calculator. I have tried using $x and $y but this only seems to work for points.

I have checked the help but it does not seem to mention being able to do this.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
James S
  • 4,264
  • 2
  • 32
  • 46

3 Answers3

21

Not sure if it worked 5 years ago, but now the solution looks like this:

x(centroid($geometry))

and

y(centroid($geometry))

Results are the same as while using the "Join" option mentioned above:

polygons' centroids m

Taras
  • 32,823
  • 4
  • 66
  • 137
Mykola Kozyr
  • 752
  • 6
  • 11
15

A fast way (without field calculator) would be to

  1. create a centroid layer: Vector - Geometry Tools - Polygon Centroids
  2. join the centroid layer to the polygon layer: (Polygon) Layer Properties - Join tab
underdark
  • 84,148
  • 21
  • 231
  • 413
  • 1
    Thanks for the answer. I had figured out I could do it this way, I just wondered if there was a way to do it through field calculator like for points. – James S Jul 01 '11 at 10:37
0

If you would consider doing it in workaround method here's how I did it:

Export your polygon as MapInfo file

Calculate centroids with Vector > Geometry Tools > Polygon Centroids

Export centroids as MapInfo File

Remove all unneeded info from Centroid MIF file, leave just coordinates

In your polygon MIF file add information that you have 2 more columns (x and y/lat and long) don't forget to update column count information.

In your polygon MID file copy all columns from centroid MIF file (excel will help here)

There might be a shorter/direct way to do this, but maybe my answer will help you.

Mykolas Simutis
  • 776
  • 1
  • 6
  • 15