# How to determine the centroid of polygons?

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.

• What format is your text document? – Nathan W Jan 6 '13 at 22:21
• It is a MS word document but really it could be anything. – David Jan 10 '13 at 16:05

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.

• 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.

• 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 5 '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 5 '13 at 22:09

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*)')) ```

Really, making a new shapefile is one of your easiest options. However, you don't need to copy and paste coordinates. Do this:

• Make your centroids file using the Polygon Centroids tool.
• Open the centroids attribute table and make it editable (pencil icon at the bottom).
• Open the field calculator (calculator icon at the bottom) and choose the Create New Field option and call it something meaningful (e.g. Easting).
• From the functions List expand the Geometry menu and choose \$x then repeat with a new column for \$y (calling it Northing, say).
• Save your edits and now you have all the coordinates as part of the attribute table complete with the original polygon ID and attributes if you used the centroids tool.

Alternatively you will have to import your data into PostGIS and then iterate over the polygons, using the ST_Centroid function and store the result in a new field. The work involved is about the same but the second option means you don't have a separate centroids point file.

• Thanks for the answer. I appreciate your help. It seems like Curlew's answer is a bit easier for me. Still both of these answers are a lot more work that I wanted. In the end you end up with various shapefiles that you have to name, delete, edit, etc. – David Jan 5 '13 at 20:28

How about using this in the 2.2 Field Calculator?

Long field = substr(geomToWKT( centroid( \$geometry )), 7, 12)

Lat field = substr(geomToWKT( centroid( \$geometry )), strpos(geomToWKT( centroid( \$geometry )), ' ')+2, 12)

Seems this is addressed in 2.6 with the xmin option xmin(centroid( \$geometry ))

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
6. Your polygon layer now contains a field with x,y centroid values
• simple and superb – Kazuhito Dec 9 '17 at 6: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

Came across this post and was hard to follow, suggested update:

Note: This will calculate the latitude and longitude in your projection coordinates (which may be meters). If you want the coordinates in decimal degrees (with I assume you do or they are mostly unintelligible) first save the layer in this CRS: EPSG: 4326, WGS 84.

1. Open field calculator
2. Create new field
3. Enter name (either Latitude or Longitude)
4. Select decimal as output field type
5. Enter this expression for latitude: y(\$geometry) and this expression for Longitude: x(\$geometry)
6. Press OK

Associate the centroid points table with the table of the polygons and then out put the coordinates with the polygon name and shape. Polygons don't have one lat long in and of themselves

• Thanks for your answer. Yes I realize polygons don't have one lat long in and of themselves it simply something that can be calculated. Unfortunately, it isn't easier than this in QGIS. Thanks again everyone for the answers. And so fast too. Thanks! – David Jan 5 '13 at 20:30

lon = ToReal(regexp_substr(geomToWKT(centroid(\$geometry)), '([\s]')) lat = ToReal(regexp_substr(geomToWKT(centroid(\$geometry)), '\s[)]'))

• This is no answer at all. Please visit the tour to get a better understanding how to answer a question... – Nightwatch Nov 20 '17 at 13:57