46

I have a polygon vector and I want to quickly attain the centroid of this polygon for insertion in a text document. I am working with QGIS. I searched through other questions and although some are somewhat close they don't quite answer my question.

Ideally, the centroid coordinates would be in the attribute table of the polygon and I could just copy and paste the coordinates.

I don't want to have to create a separate point vector file that represents the centroid and then find these coordinates and copy and paste them.

Taras
  • 32,823
  • 4
  • 66
  • 137
David
  • 761
  • 2
  • 6
  • 8

10 Answers10

47

A centroid is per definition a point layer and not a polygon. Therefor you need to create a new layer, which is easy as pie in QGIS 1.8 and higher versions.

  • Load in your polygon
  • Go to the menu -> Vector -> Geometry tools -> polygon centroid and create a centroid point layer
  • Export the coordinates of the created centroid to the attribute table by clicking on -> vector Menu -> Geometry tools -> Export/Add geometry columns.

Afterwards you could make a spatial join to add the centroids columns to the polygons and delete the point layer again.

Curlew
  • 8,152
  • 5
  • 36
  • 72
  • Thanks for the reply I appreciate the help. Still, this is a lot of work simple to know what the coordinates of the centroid is. Too bad there isn't a simpler way in QGIS. – David Jan 05 '13 at 20:26
  • If you still want it easier you might take a look at PostGIS and the ST_Centroid function mentioned below. This way you don't need to create temporary files. Alternatively you could write yourself a python script for QGIS or use the SEXTANTE Modeler. – Curlew Jan 05 '13 at 22:09
15

A more robust soulution to mike's answer: long = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*')) lat = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '-?\\d+\\.?\\d* (-?\\d+\\.?\\d*)'))

SZIEBERTH Ádám
  • 383
  • 3
  • 12
12

I just came across this post nearly 5 years late (!), but here's what I do to calculate polygon centroids at version 2.18.14:

  1. Begin editing the polygon layer
  2. Create a new text field, call it centroidxy
  3. Using the field calculator, calculate centroidxy = x($geometry) || ' , ' || y($geometry)
  4. The result looks something like: 397640.915545362 , 2126924.53637653
  5. Save your edits.
  6. Your polygon layer now contains a field with x,y centroid values
Stu Smith
  • 8,252
  • 8
  • 35
  • 84
  • simple and superb – Kazuhito Dec 09 '17 at 06:19
  • 1
  • I was wondering if that answer was already here. 2. I would make two new columns with separate x and y, but that's personal choice. It's just as copyable to excel when it's in two cols. 3. No matter what your map units are, you're calculating the coordinates of the centroid molecule or atom with that many decimals. Two or three are usually more than enough. :D
  • – thymaro Apr 27 '20 at 21:59